Skip to content

Instantly share code, notes, and snippets.

@zeitounator
Last active August 3, 2021 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeitounator/28ca735058eed16891b6d96989a09379 to your computer and use it in GitHub Desktop.
Save zeitounator/28ca735058eed16891b6d96989a09379 to your computer and use it in GitHub Desktop.
#### 1 - var is a dict inside the playbook #####
$ cat test.yml
- hosts: localhost
gather_facts: false
vars:
impactedEntities: {"type":"HOST","name":"xxxxxxxx.xxxx.xxxxxx.xx","entity":"HOST-053BD6D9938F01C5"}
tasks:
- debug:
var: impactedEntities
- debug:
msg: "{{ impactedEntities | type_debug }}"
- debug:
msg: "{{ impactedEntities.name }}"
$ ansible-playbook test.yml
PLAY [localhost] **************************************************************************************************************************************************************************************************
TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
"impactedEntities": {
"entity": "HOST-053BD6D9938F01C5",
"name": "xxxxxxxx.xxxx.xxxxxx.xx",
"type": "HOST"
}
}
TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "dict"
}
TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "xxxxxxxx.xxxx.xxxxxx.xx"
}
PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
##### 2 - var is a dict passed as pure json in extra vars
$ cat test1.yml
- hosts: localhost
gather_facts: false
tasks:
- debug:
var: impactedEntities
- debug:
msg: "{{ impactedEntities | type_debug }}"
- debug:
msg: "{{ impactedEntities.name }}"
$ ansible-playbook test1.yml -e '{"impactedEntities":{"type":"HOST","name":"xxxxxxxx.xxxx.xxxxxx.xx","entity":"HOST-053BD6D9938F01C5"}}'
PLAY [localhost] **************************************************************************************************************************************************************************************************
TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
"impactedEntities": {
"entity": "HOST-053BD6D9938F01C5",
"name": "xxxxxxxx.xxxx.xxxxxx.xx",
"type": "HOST"
}
}
TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "dict"
}
TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "xxxxxxxx.xxxx.xxxxxx.xx"
}
PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
#### 3- var is passed as a string in extra var but representing a json dict
$ cat test2.yml
- hosts: localhost
gather_facts: false
tasks:
- debug:
var: impactedEntities
- debug:
msg: "{{ impactedEntities | type_debug }}"
- debug:
msg: "{{ (impactedEntities | from_json).name }}"
$ ansible-playbook test2.yml -e 'impactedEntities={"type":"HOST","name":"xxxxxxxx.xxxx.xxxxxx.xx","entity":"HOST-053BD6D9938F01C5"}'
PLAY [localhost] **************************************************************************************************************************************************************************************************
TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
"impactedEntities": {
"entity": "HOST-053BD6D9938F01C5",
"name": "xxxxxxxx.xxxx.xxxxxx.xx",
"type": "HOST"
}
}
TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "str"
}
TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "xxxxxxxx.xxxx.xxxxxx.xx"
}
PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
###### ..... to be continued ....... #######
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment