Question:
Is there a way to only run one task in ansible playbook?
For example, in roles/hadoop_primary/tasks/hadoop_master.yml
. I have "start hadoop job tracker services"
task. Can I just run that one task?
hadoop_master.yml file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Playbook for Hadoop master servers - 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 - name: start hadoop jobtracker services service: name=hadoop-0.20-mapreduce-jobtracker state=started tags: debug |
Answer:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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:
1 2 |
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:
1 2 |
ansible-playbook example.yml --skip-tags "notification" |
You may also apply tags to roles:
1 2 3 |
roles: - { role: webserver, port: 5000, tags: [ 'web', 'foo' ] } |
And you may also tag basic include statements:
1 2 |
- include: foo.yml tags=web,foo |
Both of these have the function of tagging every single task inside the include statement.