ansible-playbook hosts can not use jinja2 for matching more hosts
when i need to install more same service in one host in test env, i write a group like this
[nginx]
demo-region-nginx-1:22 ansible_ssh_host=10.0.0.1 port=80
demo-region-nginx-2:22 ansible_ssh_host=10.0.0.1 port=81
demo-region-nginx-3:22 ansible_ssh_host=10.0.0.1 port=82
my playbook like this
- hosts: "demo-region-nginx-{{ num }}"
remote_user: root
roles:
- role: upgrade_nginx
amosttime just setup a "num" when i want to upgrade one nginx, this is easy, if i upgrade nginx-1 and nginx-3, i must be execute multiple times playbook.
In the future, i will have many such service, I must by setup more variable
,so i use jinja2 to filter the hosts, i think jinja2 can fix my problem, but it seems like error, the playbook like this
- hosts: "{% for num in {{ awx_num }} %}demo-region-nginx-{{ num }}{% endfor %}"
remote_user: root
roles:
- role: upgrade_nginx
this is error log
ERROR! template error while templating string: expected token ':', got '}'. String: {% for num in {{ awx_num }} %}demo-region-nginx-{{ num }}{% endfor %}. expected token ':', got '}'
someone know how to fix this error?
2
Answers
You dont need to set variables between brackets inside jinja expression on the for:
You are reinventing the wheel and making your life complicated.
ansible(-playbook)
has alimit
option that does exactly what you want.Given your above inventory and the dummy playbook:
Here are some example commands making use of patterns for the
limit
argument and their results: