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?
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.
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
…
{{ 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.