Use items from task condition in template

113 views
Skip to first unread message

Sergei Antipov

unread,
Aug 26, 2014, 6:40:11 AM8/26/14
to ansible...@googlegroups.com
Hello.

Please tell me how I can use items from task condition in template. Or other method, how I can decide my probled.
goal: I have a template for /etc/network/interfaces file. I want to set up bond interface, which contains the parameter "slaves XXX XXX XXX", where XXX - name of interface.
I want use only interfaces, which name match ethX regex, for example "slaves eth0 eth1".
How I can do this template with ansible, maybe I should use ansible_interfaces variable or should use shell for register intefaces names?

Michael DeHaan

unread,
Aug 26, 2014, 8:47:21 AM8/26/14
to ansible...@googlegroups.com
I'm not sure what you mean you say "use items from the task condition", but you can in fact use conditionals with your template.

Take a look at the Jinja2 documentation, and you'll see things like

{% if x %}

{% else %}

{% endif %}

and these can be put in your .j2 files.

I might have misunderstood the question.


--
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-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/de6d2241-7bc0-493d-86b4-be7974c05941%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sergei Antipov

unread,
Aug 26, 2014, 11:23:40 PM8/26/14
to ansible...@googlegroups.com
example:
- name: create /etc/network/intefaces file
template: src=xxx dest=xxx
with_items: ansible_interfaces
when: item | match("eth*")

and in template I need:
....
slaves eth0 eth1 eth2 ethX
....

вторник, 26 августа 2014 г., 19:47:21 UTC+7 пользователь Michael DeHaan написал:

Michael DeHaan

unread,
Aug 27, 2014, 8:21:13 AM8/27/14
to ansible...@googlegroups.com
{{ ansible_interfaces }} will be available in the template and you'll be able to loop over it in the template logic.

Do not use with_items on the template task, as this will result in writing the file N different times, and will not do what you want.




Reply all
Reply to author
Forward
0 new messages