Skip to content

Instantly share code, notes, and snippets.

@halberom
Last active May 5, 2022 07:38
Show Gist options
  • Save halberom/e02b1d644d54352820e8 to your computer and use it in GitHub Desktop.
Save halberom/e02b1d644d54352820e8 to your computer and use it in GitHub Desktop.
ansible - example of setting a complex var with set_fact
PLAY [foo] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [set_fact ] *************************************************************
ok: [localhost]
TASK: [debug ] ****************************************************************
ok: [localhost] => {
"var": {
"complex_arg": [
{
"address": "mars",
"name": "bob"
},
{
"name": "mary"
}
]
}
}
---
- hosts: foo
tasks:
- set_fact:
complex_arg:
- name: bob
address: mars
- name: mary
- debug:
var: complex_arg
---
- hosts: foo
tasks:
# or with a json value
- set_fact:
complex_arg: '[{"name":"bob", "address":"mars"},{"name":"mary"}]'
- debug:
var: complex_arg
---
- hosts: foo
vars:
complex_arg: '[{"name":"bob", "address":"mars"},{"name":"mary"}]'
tasks:
- set_fact: complex_arg
- debug:
var: complex_arg
@halberom
Copy link
Author

all plays give the same output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment