Reputation: 1067
hosts
[mesosSandbox]
mesos1-01d.chalybs.net zoo_id=1
mesos1-02d.chalybs.net zoo_id=2
mesos1-03d.chalybs.net zoo_id=3
mesos1-04d.chalybs.net
mesos1-05d.chalybs.net
I have a template that generates the zookeeper zoo.cfg
{% for server in groups[cluster] %}
server.{{loop.index}}={{server}}:2888:3888
{% endfor %}
Is there a way to add an if zoo_id is defined
to this loop or can I generate a list via a task in the playbook?
Upvotes: 2
Views: 7587
Reputation: 159
Ansible's if var is defined
syntax is derived from jinja. You should be able to check for zoo id right in the loop:
{% for server in groups[cluster] %}
{% if hostvars[server].zoo_id is defined %}
server.{{loop.index}}={{server}}:2888:3888
{% endif %}
{% endfor %}
I can't say whether this will compile/run without seeing more of your playbook, but assuming the loop in your question works as is, this will allow you to filter on whether zoo_id
is defined.
Upvotes: 5
Reputation: 828
Ansible's template syntax is Jinja2, so you can use it right away.
Also, you need to access zoo_id
for the target host using hostvars
array.
{% for server in groups[cluster] %}
{% if hostvars[server].zoo_id is defined %}
server.{{loop.index}}={{server}}:2888:3888
{% endif %}
{% endfor %}
You may need to load delegate facts before being able to access hostvars, but I believe it's not needed if you need to access anything defined directly in the inventory.
Upvotes: 2