Why is it possible to do with an array though?
- hosts: all
connection: local
gather_facts: false
tasks:
- set_fact:
myvar: |
{% set comma = joiner(",") %}
[ {% for item in ['honey', 'bunch'] -%}
{{ comma() }}"monkey"
{%- endfor %}]
- debug: var=myvar
PLAY [all]
********************************************************************
TASK: [set_fact ]
*************************************************************
ok: [127.0.0.1]
TASK: [debug var=myvar]
*******************************************************
ok: [127.0.0.1] => {
"var": {
"myvar": [
"monkey",
"monkey"
]
}
}
PLAY RECAP
********************************************************************
127.0.0.1 : ok=2 changed=0 unreachable=0 failed=0
But:
- hosts: all
connection: local
gather_facts: false
tasks:
- set_fact:
myvar: |
{% set comma = joiner(",") %}
[ {% for item in ['honey', 'bunch'] -%}
{{ comma() }}{ monkey: banana }
{%- endfor %}]
- debug: var=myvar
Produces:
TASK: [debug var=myvar]
*******************************************************
ok: [127.0.0.1] => {
"var": {
"myvar": "[ { monkey: banana },{ monkey: banana }]"
}
}
And:
- hosts: all
connection: local
gather_facts: false
tasks:
- set_fact:
myvar: |
{{ "\n" }}
{%- for item in ['honey', 'bunch'] -%}
- { monkey: banana }
{% endfor %}
- debug: var=myvar
TASK: [debug var=myvar]
*******************************************************
ok: [127.0.0.1] => {
"var": {
"myvar": "\n- { monkey: banana }\n- { monkey: banana }\n"
}
}
Why does one way work and the other way fail?
On Thursday, October 23, 2014 at 12:29:42 PM UTC-4, James Cammarata wrote:
>
> Hi Anth,
>
> Unfortunately, this cannot be done the way you're trying it. You're
> creating YAML after the YAML structure is already parsed, so the variable
> is just storing it as a string.
>
> Instead of doing this, I would recommend using the add_host module (
> http://docs.ansible.com/add_host_module.html), which can also accept any
> additional variables you may wish to associate with that host entry.
>
> Thanks!
>
> On Mon, Oct 20, 2014 at 8:55 PM, Anth Courtney <[email protected]
> <javascript:>> wrote:
>
>> Hello there,
>>
>> Is it possible to create a variable which is a dict using jinja2 within a
>> playbook?
>>
>> For example, I have the following (contrived) playbook, within which I am
>> trying to create a 'host_info' dict which I can then access from a task:
>>
>> ---
>>
>> - hosts: 127.0.0.1
>> connection: local
>> sudo: no
>>
>> vars:
>> app_servers: 5
>> ipaddress_base: "192.168.0"
>> rmi_portbase: 10000
>> host_info: |
>> {%- for number in range(1,app_servers + 1) -%}
>> {% set host = 'app' + number|string %}
>> - hostname: host
>> - ipaddress: {{ ipaddress_base }}.{{ number }}
>> - rmi_port: {{ rmi_portbase|int + ( number * 10) }}
>> {%- endfor %}
>>
>> tasks:
>>
>> - debug: var=item
>> with_items: host_info
>>
>> - debug: var=item.hostname
>> with_dict: host_info
>>
>>
>> However I can't seem to generate a dict as intended.
>>
>> $ ansible-playbook -i "localhost," test.yml
>>
>> PLAY [127.0.0.1]
>> **************************************************************
>>
>> GATHERING FACTS
>> ***************************************************************
>> ok: [127.0.0.1]
>>
>> TASK: [debug var=item]
>> ********************************************************
>> ok: [127.0.0.1] => (item= - hostname: host
>> - ipaddress: 192.168.0.1
>> - rmi_port: 10010 - hostname: host
>> - ipaddress: 192.168.0.2
>> - rmi_port: 10020 - hostname: host
>> - ipaddress: 192.168.0.3
>> - rmi_port: 10030 - hostname: host
>> - ipaddress: 192.168.0.4
>> - rmi_port: 10040 - hostname: host
>> - ipaddress: 192.168.0.5
>> - rmi_port: 10050) => {
>> "item": " - hostname: host\n - ipaddress: 192.168.0.1\n - rmi_port:
>> 10010 - hostname: host\n - ipaddress: 192.168.0.2\n - rmi_port: 10020 -
>> hostname: host\n - ipaddress: 192.168.0.3\n - rmi_port: 10030 - hostname:
>> host\n - ipaddress: 192.168.0.4\n - rmi_port: 10040 - hostname: host\n -
>> ipaddress: 192.168.0.5\n - rmi_port: 10050"
>> }
>>
>> TASK: [debug var=item.hostname]
>> ***********************************************
>> fatal: [127.0.0.1] => with_dict expects a dict
>>
>> FATAL: all hosts have already failed -- aborting
>>
>> PLAY RECAP
>> ********************************************************************
>> to retry, use: --limit @/tmp/test.retry
>>
>> 127.0.0.1 : ok=2 changed=0 unreachable=1
>> failed=0
>>
>>
>> Any thoughts / assistance would be appreciated.
>>
>> cheers,
>> Anth
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Ansible Project" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected]
>> <javascript:>.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/ansible-project/a3ef549a-441b-464f-a494-b635424b0356%40googlegroups.com
>>
>> <https://groups.google.com/d/msgid/ansible-project/a3ef549a-441b-464f-a494-b635424b0356%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/c3c2be79-d726-4ccb-b0f8-41cc77195828%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.