Tags are useful when we want to run specific part of the playbook. If we tagged with specific keywords, only tagged part would run.
This scenario shows:
- how to add tags and run according to specific tags
- You should have a look following lab, nodes that are created in that LAB, are using in ansible commands
- Tags are added with "tags" keyword. "tags: Always" run always, other tags could be custom defined (e.g. apache, mariadb, etc.)
---
- hosts: all
become: true
pre_tasks:
- name: install updates (CentOS)
tags: always
dnf:
update_only: yes
update_cache: yes
when: ansible_distribution == "CentOS"
- name: install updates (Ubuntu)
tags: always
apt:
upgrade: dist
update_cache: yes
when: ansible_distribution == "Ubuntu"
- hosts: web_servers
become: true
tasks:
- name: install apache and php (CentOS)
tags: centos,apache,httpd
dnf:
name:
- httpd
- php
state: latest
when: ansible_distribution == "CentOS"
- name: install apache and php (Ubuntu)
tags: ubuntu,apache,apache2
apt:
name:
- apache2
- libapache2-mod-php
state: latest
when: ansible_distribution == "Ubuntu"
- hosts: database_servers
become: true
tasks:
- name: install MariaDB (CentOS)
tags: centos,db,mariadb
dnf:
name: mariadb
state: latest
when: ansible_distribution == "CentOS"
- name: install MariaDB (Ubuntu)
tags: ubuntu,db,mariadb-server
apt:
name: mariadb-server
state: latest
when: ansible_distribution == "Ubuntu"
- To see the defined tags, run following:
ansible-playbook --list-tags site.yml
- When we want to run only tasks which contains 'Ubuntu' tag, run following:
ansible-playbook --tags ubuntu --ask-become-pass site.yml
- It can be seen below, only 'ubuntu' tagged tasks and 'always' tagged tasks were run. "Gathering Facts" always run to collect server data.
- When we want to run only tasks which contains 'db' tag, run following:
ansible-playbook --tags db --ask-become-pass site.yml
- It can be seen below, only 'db' tagged tasks and 'always' tagged tasks were run.
- When we want to run only tasks which contains 'always' tag, run following:
ansible-playbook --tags always --ask-become-pass site.yml