Bootstrap FreeKB - Ansible - Create variables, lists and dictionaries using vars
Ansible - Create variables, lists and dictionaries using vars

Updated:   |  Ansible articles

Following are the differet ways that variables can be set in Ansible. This list is in the order of precedence, where the option higher in the list takes precedence over options lower in the list.

vars is used to create variables and arrays (aka lists) and hashes or dictionaries (key value pairs). vars can also be used to return the value of a variable.

In this example, var1 contains a string (AnsibleUnicode), var2 contains a boolean, and var3 contains an integer. It is important that var2 (boolean) and var3 (integer) are not wrapped in quotes. If these were to be wrapped in quotes, they would be interpreted as a string (AnsibleUnicode). The debug module can be used to print the value of each variable.

AVOID TROUBLE

The -e or --extra-vars command line optionExtra Variables in Tower and set_fact module will take precedence over the vars plugin.

---
- hosts: localhost
  vars:
    var1: "Hello World"
    var2: true
    var3: 123
  tasks:
  - debug:
      var: var1
  - debug:
      var: var2
  - debug:
      var: var3
...

 

Something like this should be returned.

TASK [debug]
ok: [localhost] => {
    "var1": "Hello World"
}

TASK [debug]
ok: [localhost] => {
    "var2": true
}

TASK [debug]
ok: [localhost] => {
    "var3": 123
}

 


Variable with multiple values (array / list)

In this example, the "foo" variable has two values, "Hello" and "World". When there are two or more items, this is now an array (aka list).

vars:
  foo: 
    - Hello 
    - World

 

Or like this.

vars:
  foo: ['Hello', 'World']

 

The debug module can be used to output the array, like this.

- name: "output the contents of the 'foo' array"
  debug: 
    msg: "{{ foo }}"

 

Which should return the following. Notice the bracket characters [ ], which means this is indeed an array.

TASK [output the contents of the 'foo' array]
ok: [server1.example.com] => {
    "msg": [
        "Hello"
        "World",
    ]
}

 

Additionally, the loop or with_items parameters can be used to loop through each item in the array, like this.

- name: "loop through the 'foo' array"
  debug: 
    msg: "{{ item }}"
  loop: "{{ foo }}"

 

Which should return the following.

TASK [loop through the 'foo' array] 
ok: [server1.example.com] => (item=Hello) => {
    "msg": "Hello"
}
ok: [server1.example.com] => (item=World) => {
    "msg": "World"
}

 


key value pairs

Or, key value pair can be created like this.

vars:
  variables:
    - key: foo
      value: Hello
    - key: bar
      value: World

 

The debug and loop modules can be used to print the key value pairs.

- debug: 
    msg: "{{ item }}"
  loop: "{{ variables }}"

 

Which should return the following.

TASK [debug] 
ok: [server1.example.com] => (item={u'value': u'Hello', u'key': u'foo'}) => {
    "msg": {
        "key": "foo",
        "value": "Hello"
    }
}
ok: [server1.example.com] => (item={u'value': u'World', u'key': u'bar'}) => {
    "msg": {
        "key": "bar",
        "value": "World"
    }
}

 


vars can also be used to return the value of a variable. In this simple example, vars is used so that the "foo" variable contains a value of "Hello World" and then vars is used in the debug module to print the value of the "foo" variable.

---
- hosts: localhost
  vars:
    foo: "Hello World"
  tasks:
  - debug:
      var: vars['foo']
...

 

As a much more practical example, let's say your vars/main.yml file contains a different username for each environment.

dev_username: "john.doe"
stage_username: "jane.doe"
prod_username: "jack.doe"

 

Here is how you could define the username using set_fact and vars. In this example, "env" would most likely be set using the -e or --extra-vars command line option.

---
- hosts: localhost
  vars_files:
    - vars/main.yml
  tasks:
  - name: username
    set_fact:
      username: "{{ vars[env+'_username'] }}"

  - debug:
      var: username
...

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 70fd1e in the box below so that we can be sure you are a human.