The easiest way to run only one task in Ansible Playbook is using the tags statement parameter of the “ansible-playbook” command. The default behavior is to execute all the tags in your Playbook with --tags all .
Ansible run_once parameter is used with a task, which you want to run once on first host. When used, this forces the Ansible controller to attempt execution on first host in the current hosts batch, then the result can be applied to the other remaining hosts in current batch.
If you assign the never tag to a task or play, Ansible will skip that task or play unless you specifically request it ( --tags never ).
You should use tags:
as documented in https://docs.ansible.com/ansible/latest/user_guide/playbooks_tags.html
If you have a large playbook it may become useful to be able to run a specific part of the configuration without running the whole playbook.
Both plays and tasks support a “tags:” attribute for this reason.
Example:
tasks:
- yum: name={{ item }} state=installed
with_items:
- httpd
- memcached
tags:
- packages
- template: src=templates/src.j2 dest=/etc/foo.conf
tags:
- configuration
If you wanted to just run the “configuration” and “packages” part of a very long playbook, you could do this:
ansible-playbook example.yml --tags "configuration,packages"
On the other hand, if you want to run a playbook without certain tasks, you could do this:
ansible-playbook example.yml --skip-tags "notification"
You may also apply tags to roles:
roles:
- { role: webserver, port: 5000, tags: [ 'web', 'foo' ] }
And you may also tag basic include statements:
- include: foo.yml tags=web,foo
Both of these have the function of tagging every single task inside the include statement.
There is a way, although not very elegant:
ansible-playbook roles/hadoop_primary/tasks/hadoop_master.yml --step --start-at-task='start hadoop jobtracker services'
Perform task: start hadoop jobtracker services (y/n/c)
y
Ctrl-C
FWIW with Ansible 2.2 one can use include_role:
playbook test.yml
:
- name: test
hosts:
- 127.0.0.1
connection: local
tasks:
- include_role:
name: test
tasks_from: other
then in roles/test/tasks/other.yml
:
- name: say something else
shell: echo "I'm the other guy"
And invoke the playbook with: ansible-playbook test.yml
to get:
TASK [test : say something else] *************
changed: [127.0.0.1]
I would love the ability to use a role as a collection of tasks such that, in my playbook, I can choose which subset of tasks to run. Unfortunately, the playbook can only load them all in and then you have to use the --tags
option on the cmdline to choose which tasks to run. The problem with this is that all of the tasks will run unless you remember to set --tags
or --skip-tags
.
I have set up some tasks, however, with a when:
clause that will only fire if a var is set.
e.g.
# role/stuff/tasks/main.yml
- name: do stuff
when: stuff|default(false)
Now, this task will not fire by default, but only if I set the stuff=true
$ ansible-playbook -e '{"stuff":true}'
or in a playbook:
roles:
- {"role":"stuff", "stuff":true}
are you familiar with handlers? I think it's what you are looking for. Move the restart from hadoop_master.yml
to roles/hadoop_primary/handlers/main.yml
:
- name: start hadoop jobtracker services
service: name=hadoop-0.20-mapreduce-jobtracker state=started
and now call use notify
in hadoop_master.yml
:
- name: Install the namenode and jobtracker packages
apt: name={{item}} force=yes state=latest
with_items:
- hadoop-0.20-mapreduce-jobtracker
- hadoop-hdfs-namenode
- hadoop-doc
- hue-plugins
notify: start hadoop jobtracker services
This can be easily done using the tags
The example of tags is defined below:
---
hosts: localhost
tasks:
- name: Creating s3Bucket
s3_bucket:
name: ansiblebucket1234567890
tags:
- createbucket
- name: Simple PUT operation
aws_s3:
bucket: ansiblebucket1234567890
object: /my/desired/key.txt
src: /etc/ansible/myfile.txt
mode: put
tags:
- putfile
- name: Create an empty bucket
aws_s3:
bucket: ansiblebucket12345678901234
mode: create
permission: private
tags:
- emptybucket
to execute the tags we use the command
ansible-playbook creates3bucket.yml --tags "createbucket,putfile"
See my answer here: Run only one task and handler from ansible playbook
It is possible to run separate role (from roles/
dir):
ansible -i stage.yml -m include_role -a name=create-os-user localhost
and separate task file:
ansible -i stage.yml -m include_tasks -a file=tasks/create-os-user.yml localhost
If you externalize tasks from role to root tasks/
directory (reuse is achieved by import_tasks: ../../../tasks/create-os-user.yml
) you can run it independently from playbook/role.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With