Hi! I’m trying to implement firewall rules management with ufw
module. Idea is to have list of ports to be accessable from sources somewhere in host variables. For example, I have this in my group_vars/consul_servers
:
ufw_open_docker_from_sources:
- {port: 8500, src: consul_agents}
- {port: 8301, src: consul_agents}
Where consul_agents
is a group name.
And I have a firewall/main.yml
in my rules
, where I’m trying to expand this list of ports and groups into some tasks:
{% for item in hostvars['ufw_open_docker_from_sources'] %}
{% for host in groups[item.src] %}
- name: Enable access to some docker ports from particular source
ufw:
insert=1
route=yes
rule=allow
port={{ item.port }}
src={{ hostvars[host]['ip'] }}
{% endfor %}
{% endfor %}
But this does not work because of error:
ERROR! Syntax Error while loading YAML.
The error appears to have been in '/Users/art/projects/ansible/with-roles/roles/firewall/tasks/main.yml': line 25, column 2, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
{% for item in hostvars['ufw_open_docker_from_sources'] %}
^ here
Is it possible to overcome this limitation?