Ansible – run specific task of a playbook
Posted by danielJan 7
Ansible – How to run a portion of a playbook using tags.
If you have a large playbook it may become useful to be able to run a specific part of it or only a single task without running the whole playbook. Both plays and tasks support a “tags:” attribute for this reason.
In this specific scenario, I have a playbook which configures all productions servers from the moment the servers boot till they start taking traffic. While testing the plays in dev environment, I was debugging an issue on the parts which does dns configuration. This is where the “tags” attributes comes handy –
1. Tag the task –
1 2 3 4 5 6 7 | ... - name: Configure resolv.conf template: src=resolv.conf.j2 dest= /etc/resolv .conf when: ansible_hostname != "ns1" tags: - dnsconfig ... |
2. Run only the tasks tagged with a specific name –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | root@linubuvma: /etc/ansible # ansible-playbook -i dc1/hosts dc1/site.yml --tags "dnsconfig" --check PLAY [Setup data center 1 servers] ***************************************************** TASK: [common | Configure resolv.conf] **************************************** skipping: [ns1] changed: [docker] ok: [ns2] ok: [whitehat] ok: [mail] ok: [www] ok: [ ftp ] PLAY RECAP ******************************************************************** whitehat : ok=1 changed=0 unreachable=0 failed=0 docker : ok=1 changed=1 unreachable=0 failed=0 ns1 : ok=0 changed=0 unreachable=0 failed=0 ns2 : ok=1 changed=0 unreachable=0 failed=0 mail : ok=1 changed=0 unreachable=0 failed=0 www : ok=1 changed=0 unreachable=0 failed=0 ftp : ok=1 changed=0 unreachable=0 failed=0 |
Ansible playbook will run only the task with the specified tag, it will skip the rest of the tasks in the playbook. Use the ‘–list-tags’ flag to view all the tags.
References –
http://docs.ansible.com/playbooks_tags.html
https://www.percona.com/live/mysql-conference-2015/sites/default/files/slides/Ansible.pdf
5 comments
You must be logged in to post a comment.