Ansible dictionary variables

Dictionary variables are another type of ansible variable additionally supported in the playbook. To define the dictionary variable, simply ident the key-value pair just below the dictionary variable.

ansible playbook for dictionary variables
# cat dictvariable.yaml
---
- hosts: all
  vars:
    training:
      topic: ansible
      trainer: sathish
      lab: aws
  tasks:
  - name: print dict variable
    debug:
      msg: "key is {{ item.key }} and value is {{ item.value }}"
    with_dict: "{{ training }}"

To run this playbook
# ansible-playbook -i hosts dictvariable.yaml
PLAY [all] **************************************************************************************************************************************************

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

TASK [print dict variable] **********************************************************************************************************************************
ok: [192.168.1.2] => (item={u'key': u'topic', u'value': u'ansible'}) => {
    "msg": "key is topic and value is ansible"
}
ok: [192.168.1.2] => (item={u'key': u'trainer', u'value': u'sathish'}) => {
    "msg": "key is trainer and value is sathish"
}
ok: [192.168.1.2] => (item={u'key': u'lab', u'value': u'aws'}) => {
    "msg": "key is lab and value is aws"
}

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





Recent Comments

No comments

Leave a Comment