Use tags to run only specific tasks in a playbook.
Outcomes
Tag specific tasks in a playbook to run or skip them.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
[student@workstation ~]$ lab start task-tagging
Procedure 6.3. Instructions
Clone the https://git.lab.example.com/student/task-tagging.git repository to the /home/student/git-repos directory and then create a branch for this exercise.
From a terminal, create the /home/student/git-repos directory if it does not already exist, and then change into it.
[student@workstation ~]$mkdir -p ~/git-repos/[student@workstation ~]$cd ~/git-repos/
Clone the https://git.lab.example.com/student/task-tagging.git repository and then change directory to the cloned repository:
[student@workstation git-repos]$git clone \>https://git.lab.example.com/student/task-tagging.gitCloning into 'task-tagging'... ...output omitted... [student@workstation git-repos]$cd task-tagging
Create the exercise branch and check it out.
[student@workstation task-tagging]$ git checkout -b exercise
Switched to a new branch 'exercise'Create a new playbook to test the successful deployment of the web application.
Open a new test_webapp.yml file for editing.
Add a task that verifies that the content of the web application is accessible.
Use the uri module to retrieve the contents.
The file should consist of the following content:
---
- name: Web application smoke test
hosts: web_servers
gather_facts: false
tasks:
- name: Verify content of http://localhost
ansible.builtin.uri:
url: http://localhost
return_content: true
register: test_url
failed_when: "'Hello from' not in test_url['content']"
tags:
- testsAdd the new playbook to the site.yml file.
The file should consist of the following content:
- name: Deploy HAProxy ansible.builtin.import_playbook: deploy_haproxy.yml - name: Deploy Web Server ansible.builtin.import_playbook: deploy_apache.yml - name: Deploy Web App ansible.builtin.import_playbook: deploy_webapp.yml -name: Test deployed Web Appansible.builtin.import_playbook: test_webapp.yml
Use the site.yml playbook to only test the web application deployment.
After the test fails, run the playbook to configure the environment, skipping all tests.
Use the tests tag to test the web application before it is set up.
[student@workstation task-tagging]$ansible-navigator run \>-m stdout site.yml --tags tests...output omitted... TASK [Verify content of http://localhost] **********************************...fatal: [serverc.lab.example.com]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"}, "changed": false, "content": "", "elapsed": 0, "failed_when_result": true, "msg": "Status code was -1 and not [200]:Request failed: <urlopen error [Errno 111] Connection refused>", "redirected": false, "status": -1, "url": "http://localhost"}fatal: [serverb.lab.example.com]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"}, "changed": false, "content": "", "elapsed": 0, "failed_when_result": true, "msg": "Status code was -1 and not [200]:Request failed: <urlopen error [Errno 111] Connection refused>", "redirected": false, "status": -1, "url": "http://localhost"} PLAY RECAP *****************************************************************... serverb.lab.example.com : ok=0 changed=0 unreachable=0failed=1... serverc.lab.example.com : ok=0 changed=0 unreachable=0failed=1...
This command ran only the task tagged with the tests tag; because the server was not set up, the test failed.
Run the playbook, skipping the tests to set up the web application.
[student@workstation task-tagging]$ansible-navigator run \>-m stdout site.yml --skip-tags tests...output omitted... PLAY RECAP *****************************************************************... servera.lab.example.com : ok=6changed=6unreachable=0 failed=0 ... serverb.lab.example.com : ok=6changed=6unreachable=0 failed=0 ... serverc.lab.example.com : ok=6changed=6unreachable=0 failed=0 ...
Use the tests tag again to test the web application.
[student@workstation task-tagging]$ansible-navigator run \>-m stdout site.yml --tags tests...output omitted... TASK [Verify content of http://localhost] **********************************...ok: [serverc.lab.example.com]ok: [serverb.lab.example.com]PLAY RECAP *****************************************************************... serverb.lab.example.com : ok=1 changed=0 unreachable=0failed=0... serverc.lab.example.com : ok=1 changed=0 unreachable=0failed=0...
Now that the web application has been deployed, the test is successful.