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
      topic: ansible
      trainer: sathish
      lab: aws
  - name: print dict variable
      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: []

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

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

Recent Comments

No comments

Leave a Comment