jinja2 - loop over hosts in jinj2a template, respecting --limit -
i'm aware ansible supports loops in templates in form:
{% host in groups['all'] %} "{{ host }}"{% if not loop.last %},{% endif %} {% endfor %}
when run ansible, loops on in hosts file, 1 might expect.
when run ansible --limit command line argument, loop on hosts match limit. there way express loop in jinja2 templates?
you can use play_hosts
variable vars
, example:
{% host in vars['play_hosts'] %} "{{ host }}"{% if not loop.last %},{% endif %} {% endfor %}
imagine setup:
# hosts [all-hosts] ansible ansible_ssh_host=192.168.42.2 webapp ansible_ssh_host=192.168.42.10 postgresql ansible_ssh_host=192.168.42.20 #playbook.yml --- - hosts: gather_facts: no tasks: - name: hosts template: src=myhosts.j2 dest=./myhosts.json delegate_to: 127.0.0.1 run_once: yes
then running no limit give same result have, when specify limit produce "limited" host names:
ansible-playbook -i hosts playbook.yml --limit postgresql,ansible
output:
"ansible", "postgresql"
Comments
Post a Comment