Skip to content

Ansible variables and tags

Ansible variables and tags
Share

Reading Time: 4 minutes

This tutorial will guide you on How to use Ansible variables and tags with examples. You can use Ansible variables to manage differences between hosts. Using Ansible, you can execute tasks and playbooks on multiple different systems with a single command. To represent the variations among those different systems, you can create variables with standard YAML syntax, including lists and dictionaries. You must define these variables in your playbooks, in your, Inventory in reusable files or roles, or at the command line. You can also create variables during a playbook run by registering the return value or values of a task as a new variable.

After you create variables, either by defining them in a file, passing them at the command line, or registering the return value or values of a task as a new variable, you can use those variables in module arguments, in conditional “when” statements, in templates, and in loops. The ansible-examples github repository contains many examples of using variables in Ansible.

Once you understand the concepts and examples on this page, read about Ansible facts, which are variables you retrieve from remote systems.

If you have not installed Ansible yet, Install “How to install Ansible

Ansible variables and tags

Playbook

Create playbook file test.yml
$ vi test.yml
You can create a playbook like below and set two variables var1 and var2. There are two tasks 1. “Print 1st variable” 2. “Print 2nd Variable”.

debug – It is used to print variables.
tags – We have assigned tags to each task and given the same name as the variable.

Playbook Code

- hosts: localhost
  vars:
   var1: "This is 1st variable"
   var2: "This is 2nd variable"
  tasks:
   - name: Print 1st Variable
     debug:
      msg: "{{ var1 }}"
     tags: var1

   - name: Print 2nd Variable
     debug:
      msg: "{{ var2 }}"
     tags: var2

Playbook Output

Print both variables : $ ansible-playbook test.yml -bv

PLAY [localhost] ***********************************************************************************************************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Print 1st Variable] **************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "This is 1st variable"
}

TASK [Print 2nd Variable] **************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "This is 2nd variable"
}

PLAY RECAP *****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Print the first variable using tags:
$ ansible-playbook test.yml --tags var1 -bv

PLAY [localhost] ***********************************************************************************************************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Print 1st Variable] **************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "This is 1st variable"
}

PLAY RECAP *****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Print both variables using both tags:
$ ansible-playbook test.yml --tags var1,var2 -bv

PLAY [localhost] ***********************************************************************************************************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Print 1st Variable] **************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "This is 1st variable"
}

TASK [Print 2nd Variable] **************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "This is 2nd variable"
}

PLAY RECAP *****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

How to skip Ansible tags

You can also skip tags and run the remaining tasks of playbook. In this example, we have skipped tag var1.
$ ansible-playbook test.yml --skip-tags var1 -bv
You can continue to follow below steps mentioned in the tutorial Ansible variables and tags

PLAY [localhost] ***********************************************************************************************************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Print 2nd Variable] **************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "This is 2nd variable"
}

PLAY RECAP *****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible-playbook test.yml --skip-tags var1,var2  -bv
PLAY [localhost] ***********************************************************************************************************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

PLAY RECAP *****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Playbook file with ignore_errors

In Ansible you can also continue to run the next tasks by using “ignore_errors: yes” option.

- hosts: localhost
  vars:
   var1: "This is 1st variable"
   var2: "This is 2nd variable"
  tasks:
   - name: Print 1st Variable
     ignore_errors: yes
     #ignore_unreachable: yes
     debug:
      msg: "{{ var12 }}"
     tags: var1

   - name: Print 2nd Variable
     debug:
      msg: "{{ var2 }}"
     tags: var2

In this example w have assigned va1 and var2 variables however in tasks print we used an undefined variable name var12, It means var12 is undefined so if we run this playbook it will throw an error and exit hence we used “ignore_errors: yes” option so the other tasks will be picked up.

$ ansible-playbook test.yml --skip-tags var1 -bv

PLAY [localhost] ***********************************************************************************************************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Print 1st Variable] **************************************************************************************************************************************************************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'var12' is undefined\n\nThe error appears to be in '/local/deploy-ansible/playbooks/test.yml': line 6, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  tasks:\n   - name: Print 1st Variable\n     ^ here\n"}
...ignoring

TASK [Print 2nd Variable] **************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "This is 2nd variable"
}

PLAY RECAP *****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=1

How to list all tags

ansible-playbook test.yml  --list-tags -bv
Ansible variables and tags
playbook: test.yml

  play #1 (localhost): localhost	TAGS: []
      TASK TAGS: [var1, var2]

You can read more about Ansible tags here. If you have any questions about Ansible variables and tags, Please reach out to us. We hope you are able to use ansible variables and tags and also able to list out all tags and skip tasks using –skips-tags options. This will certainly help you if you want to debug long playbooks and roles.


Share

Leave a Reply

Your email address will not be published. Required fields are marked *

?>