Question:
The Ansible best-practices documentation has this example code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
--- # 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?
Answer:
You could work with – block:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
➜ ~ 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
--- - 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 |