The Ansible best-practices documentation has this example code:
---
# file: roles/common/tasks/main.yml
- name: be sure ntp is installed
yum: name=ntp state=installed
tags: ntp
- name: be sure ntp is configured
template: src=ntp.conf.j2 dest=/etc/ntp.conf
notify:
- restart ntpd
tags: ntp
- name: be sure ntpd is running and enabled
service: name=ntpd state=running enabled=yes
tags: ntp
I'm looking to avoid duplicating the tags: ntp
line. Is it possible for each of these instructions to inherit a tag?
ansible-playbook offers five tag-related command-line options: --tags all - run all tasks, ignore tags (default behavior) --tags [tag1, tag2] - run only tasks with either the tag tag1 or the tag tag2. --skip-tags [tag3, tag4] - run all tasks except those with either the tag tag3 or the tag tag4.
Along with ansible-playbook command, use --tags or -t flag and pass the tag name to run only that particular task. You can also pass more than one tags to --tags flag by separating the values with commas. To skip particular tags and run all other tags, you can use --skip-tags flag.
Instead of running an entire Ansible playbook, use tags to target the specific tasks you need to run. As a frequent Ansible user, I'm always looking at ways to simplify my playbooks and save time during playbook debugging. One of my favorite features for writing robust Ansible playbooks is its support for tags.
You could work with - block:
➜ ~ cat become.yml
---
- hosts: localhost
user: vagrant
tasks:
- block:
- shell: whoami
register: result
- debug: var=result.stdout
- name: become_root_user
become: true
become_user: root
shell: whoami
register: sudo_test_result
- debug: var=sudo_test_result.stdout
tags:
- block1
- block:
- name: creating_new_app_user
become: true
become_user: root
become_method: sudo
user: name=app_user password=Bzs310di86b6E groups="adm,sudo" system=yes state=present
- name: become_app_user
become: true
become_user: app_user
become_method: sudo
shell: whoami
register: app_user_test_result
- debug: var=app_user_test_result.stdout
tags:
- block2
~ ansible-playbook -i realtime-automation/hosts-slaves become.yml --tags "block1"
In your specific case:
---
- block:
- name: be sure ntp is installed
yum: name=ntp state=installed
- name: be sure ntp is configured
template: src=ntp.conf.j2 dest=/etc/ntp.conf
notify:
- restart ntpd
- name: be sure ntpd is running and enabled
service: name=ntpd state=running enabled=yes
tags: ntp
Before v2 this could be achieved assigning a tag to an 'include'
Move this task to a different file, say ntp.yml
---
# file: roles/common/tasks/ntp.yml
- name: be sure ntp is installed
yum: name=ntp state=installed
- name: be sure ntp is configured
template: src=ntp.conf.j2 dest=/etc/ntp.conf
notify:
- restart ntpd
- name: be sure ntpd is running and enabled
service: name=ntpd state=running enabled=yes
And then include it in main.yml
---
# file: roles/common/tasks/main.yml
- include: ntp.yml
tags: ntp
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