Personally I would have done this like this (but you may have reasons
for arranging it differently)...

defaults/main.yml:-
---
nodes:
    - name: node1
      ip: 1.1.1.1
    - name: node2
      ip: 2.2.2.2
    - name: node3
      ip: 3.3.3.3

Then the template something like

address={% for node in nodes %}
{{ node.ip}}
{% if not loop.last %},{% endif %}
{% endfor %}

All untested (sorry!)

    Nigel.
> David Fazeli <mailto:davood.faze...@gmail.com>
> 23 February 2018 at 12:28
> Please explain to me how can I fix this problem? i have this
> file |defaults/main.yml|
>
> ||
> |---
> node1:
>  ip:1.1.1.1
>
> node2:
>  ip:2.2.2.2
>
> node3:
>  ip:3.3.3.3|
>
> Now, I want in template file |ip.j2| by |for loop| access the IPs of
> each server and save in |address| |variable|
>
> Like this:
>
>
> |address= 1.1.1.1,2.2.2.2,3.3.3.3|
>
> |
> |
>
> I tried this code:
>
>
> ||
> |address={%forx in{{nubmer_nodes}}%}
> {{node[x].ip}}
> {%ifloop.last%},{%endif %}
> {%endfor %}
> |
> |
> |
>
>
> But an error occurs. How should I do this?
>
>
> *Error* :
>
>
> ||
> |TASK
> [GatheringFacts]*********************************************************************
>
> ok:[db2]
> ok:[db3]
> ok:[db1]
>
> TASK [ssh :--myloop
> --]*************************************************************************
>
> fatal:[db1]:FAILED!=>{"changed":false,"msg":"AnsibleError: template
> error while templating string: expected token ':', got '}'. String:
> \r\naddress={% for x in {{number_nodes}} %}\r\n{{node[x].ip}}\r\n{% if
> loop.last %},{% endif %}\r\n{% endfor %}"}
> fatal:[db2]:FAILED!=>{"changed":false,"msg":"AnsibleError: template
> error while templating string: expected token ':', got '}'. String:
> \r\naddress={% for x in {{number_nodes}} %}\r\n{{node[x].ip}}\r\n{% if
> loop.last %},{% endif %}\r\n{% endfor %}"}
> fatal:[db3]:FAILED!=>{"changed":false,"msg":"AnsibleError: template
> error while templating string: expected token ':', got '}'. String:
> \r\naddress={% for x in {{number_nodes}} %}\r\n{{node[x].ip}}\r\n{% if
> loop.last %},{% endif %}\r\n{% endfor %}"}
>         to retry,use:--limit @/etc/ansible/playbooks/get_ip_ssh.retry
>
> PLAY RECAP ********************************************************
>
> db1                        :ok=1   changed=0   unreachable=0   failed=1
> db2                        :ok=1   changed=0   unreachable=0   failed=1
> db3                        :ok=1   changed=0   unreachable=0   failed=1
> |
> |
> |
>
>
>
>
>
>
>
>
>
> -- 
> 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 ansible-project+unsubscr...@googlegroups.com
> <mailto:ansible-project+unsubscr...@googlegroups.com>.
> To post to this group, send email to ansible-project@googlegroups.com
> <mailto:ansible-project@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/17069ac9-16f3-40f4-ae59-dbeaadc10133%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/17069ac9-16f3-40f4-ae59-dbeaadc10133%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

-- 

[ Nigel Metheringham ------------------------------ ni...@dotdot.it ] 
[                 Ellipsis Intangible Technologies                  ]
 


-- 
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 ansible-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/5A902B41.1060408%40dotdot.it.
For more options, visit https://groups.google.com/d/optout.

Reply via email to