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]> 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]. > 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/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/CAMFyvFiK9%2B3fs5zhNdGZhHdUxfo0xMcp_b%2B9hoYunRsm5obh_w%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
