Ansible playbook can be a construct of multiple plays or each play may contains multiple tasks. This is where we may have situation where you need to add new task to the existing play or playbook, and we need to test many times that newly added task.
While testing multiple times we many don't want to execute certain tasks such as a task 'Send email notification' when you preparing a 'Reboot of server' or 'Restart of Service' or 'Deployment of a service'. During the testing time you may want to exclude these notification tasks.
There are situations where we might want to run a particular task as per the input at the run time of a playbook. This may be from AWX/Tower UI select them.
Ansible tags - to control the tasks of a Playbook |
I will be explaining in this post, How to run or not to run a particular task in given playbook.
Important concepts about Ansible tags
- Ansible tags are keys to identify and control the tasks for execution or exclude from the playbook that could have multiple tasks
- Ansible tags are references or aliases to a task inside a play selection can be done with -t or --tags options in the command line
- Selecting a tag for exclude can be defined with --skip-tags option
- A task may have multiple tags such as sending email task could have tags as email_notify, notifications
- There can be same tag that can be associated with multiple tasks such as notifications can be associated with sending email and sending slack notices as well
Prerequisites
I've Ansible Controller machine and wanted to install the 'Apache2' webserver on the managed nodes. I've a sample playbook for it.
How to be specific during the execution of Ansible Playbook?
Now let me create a playbook for install and control a Apache webserver. All my managed nodes are with CentOS so I'm using yum package module in this.
The logic for the below playbook is build with two modules: yum, service
staate attribute of yum package module |
service module parameter state and values |
You can see the YAML using tags which is our main objective of this post:
--- - name: Install and control Apache Webservers hosts: "{{ targets | default('localhost') }}" become: yes tasks: - name: Install httpd server yum: name: httpd state: present tags: install_httpd, boot_web - name: start httpd server service: name: httpd state: started tags: start_httpd, boot_web - name: stop httpd server service: name: httpd state: stopped tags: stop_httpd, destroy_web - name: uninstall httpd server yum: name: httpd state: absent tags: uninstall_httpd, destroy_webThe Package state – parameter values can be found from the ansible documentation The execution can be specific to start the webserver, that means there should be Apache webserver must be installed and then start the service. To install webserver on two nodes.
ansible-playbook httpd-tags.yml -t install_httpd -e targets=ansible-node-1,ansible-nodes-2To start the webserver on two remote hosts
ansible-playbook httpd-tags.yml -t start_httpd -e targets=ansible-node-1,ansible-nodes-2
Ansible tags passed in the command line with -t option |
ansible-playbook httpd-tags.yml --tags boot_webTo stop the webserver on two remote hosts
ansible-playbook httpd-tags.yml -t stop_httpd -e targets=ansible-node-1,ansible-nodes-2To uninstall the webserver on two remote hosts
ansible-playbook httpd-tags.yml -t uninstall_httpd -e targets=ansible-node-1,ansible-nodes-2We can also select multiple tags to run the playbook. To stop the webserver on two remote hosts
ansible-playbook httpd-tags.yml -t stop_httpd,uninstall_httpd -e targets=ansible-node-1,ansible-nodes-2
Ansible tags using multiple tags |
ansible-playbook httpd-tags.yml --skip-tag install_httpd,start_httpd -e targets=ansible-node-1,ansible-nodes-2We can also use common tag instead of using two tags used for stop and uninstall the webserver as destroy_web single tag.
ansible-playbook httpd-tags.yml --tags destroy_webHey is there any other alternative way without tags can I say from here onwards execute the playbook?
Yes, We can also use task-name as an input for a special option –start-at-task for the ansible-playbook command. See this example that tells 'stop httpd server' onwards means two tasks will be executed that is stop_httpd, uninstall_httpd tasks.
ansible-playbooks httpd_tags.yml --start-at-task "stop httpd server"There are two newly introduced from Ansible 2.5 version onwards they are important and very special tags in Ansible playbooks are always and never.
--- - name: Understanding always, never tags hosts: localhost become: true tasks: - name: Tasks execute always command: echo "this is from always tag" tags: - always - name: Install nginx server apt: name: "nginx" state: "present" tags: - install - never - name: Deploy webpage to website copy: src: index.html dest: /var/www/html/ tags: - deployWhen you run this playbook it will always task will be executed always! The install task will be never executed. What is the command to list all tags present in a Playbook?
ansible-playbook mytags.yml --list-tagsTerminology: AWX is free open-source project that enables us to manage Ansible from a web interface.
No comments:
Post a Comment