Wednesday, January 24, 2018

Ansible with_dict to iterate over items in a dictionary type variable

Iterating the nested variables using "with_dict" provides great flexibility of addressing the dictionary type variables. Here is a simple playbook to illustrate that.

Here the variables are defined as normally using vars in the playbook play.  The variable "production" as seen in the playbook  is a YAML dictionary type variable.


  • The way the three servers 'server1', 'server2' and 'server3' are defined are the same even though they are written not in the same manne
  • YAML reads [...] as a list and { ... } as a dictionary as a key-value pairs.


The task at the and of the playbook using the keyword "with_dict" and variable as " {{ production }}" reads through all the member variables that are key value pairs.

In the last task that is a debug task you can also see by replacing the with_dict with the value of  "{{production.server1}} and see the results. The same works still as even the variable "server1" is a dictionary.

---
- name: test playbook
  hosts: localhost
  become: true
  become_user: root

  vars:
    production:
      server1:
        name: "server1"
        ip: "192.1.1.1"
        port: 443
        pools:
          - pool1
          - pool2
        vss:
          - vs1
          - vs2

      server2:
        name: "server2"
        ip: "192.168.1.2"
        port: 443
        pools: [ "pool1", "pool2" ]
        vss: [ "vs1", "vs2" ]

      server3: { name: "server3", ip: "192.168.100.200", port: 443, pools: [ "pool1", "pool2" ], vss: [ "vs1", "vs2" ] }


  tasks:
    - name: see the variables for production
      debug: msg="{{item.key}}"
      with_dict: "{{ production }}"

----

As the play book is run the following is observed as to how Ansible reads through each of the key-value pairs in the dictionary and the sub-values also


[root@ansible testplaybooks]# ansible-playbook 2play.yaml

PLAY [test playbook] *************************************************************************************************************************************************************************

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

TASK [see the variables for production] ******************************************************************************************************************************************************
ok: [localhost] => (item={'value': {u'pools': [u'pool1', u'pool2'], u'ip': u'192.1.1.1', u'vss': [u'vs1', u'vs2'], u'name': u'server1', u'port': 443}, 'key': u'server1'}) => {
    "changed": false,
    "item": {
        "key": "server1",
        "value": {
            "ip": "192.1.1.1",
            "name": "server1",
            "pools": [
                "pool1",
                "pool2"
            ],
            "port": 443,
            "vss": [
                "vs1",
                "vs2"
            ]
        }
    },
    "msg": "server1"
}
ok: [localhost] => (item={'value': {u'pools': [u'pool1', u'pool2'], u'ip': u'192.168.1.2', u'vss': [u'vs1', u'vs2'], u'name': u'server2', u'port': 443}, 'key': u'server2'}) => {
    "changed": false,
    "item": {
        "key": "server2",
        "value": {
            "ip": "192.168.1.2",
            "name": "server2",
            "pools": [
                "pool1",
                "pool2"
            ],
            "port": 443,
            "vss": [
                "vs1",
                "vs2"
            ]
        }
    },
    "msg": "server2"
}
ok: [localhost] => (item={'value': {u'pools': [u'pool1', u'pool2'], u'ip': u'192.168.100.200', u'vss': [u'vs1', u'vs2'], u'name': u'server3', u'port': 443}, 'key': u'server3'}) => {
    "changed": false,
    "item": {
        "key": "server3",
        "value": {
            "ip": "192.168.100.200",
            "name": "server3",
            "pools": [
                "pool1",
                "pool2"
            ],
            "port": 443,
            "vss": [
                "vs1",
                "vs2"
            ]
        }
    },
    "msg": "server3"
}

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

No comments:

Post a Comment