FreeKB - Ansible vars plugin (variables, arrays, lists)
Ansible - vars plugin (variables, arrays, lists)

The vars plugin is used to create variables and arrays (aka lists). Different types of variables can be created.

  • AnsibleUnicode = a string, such as Hello World
  • boolean = true, false, yes, no, 1, 0
  • integer = a number other than 1 or 0, such as 123456

In this example, three variables are created. var1 is a string (AnsibleUnicode), var2 is a boolean, and var3 is 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).

vars:
  var1: "Hello World"
  var2: true
  var3: 123

 

The debug module can be used to print the value of each variable.

- name: output the 'var1' variable
  debug:
    var: var1

- name: output the 'var2' variable
  debug:
    var: var2

- name: output the 'var3' variable
  debug:
    var: var3

 

Something like this should be returned.

TASK [output the 'var1' variable]
ok: [server1.example.com] => {
    "var1": "Hello World"
}

TASK [output the 'var2' variable]
ok: [server1.example.com] => {
    "var2": true
}

TASK [output the 'var3' variable]
ok: [server1.example.com] => {
    "var3": 123
}

 


One key with multiple values (array)

In this example, the "foo" key 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"
    }
}


Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




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




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |