Reputation: 143
I have to update a config file with server IPs from a cluster. The cluster contains minimum 2 servers, however, the maximum number of servers in the cluster is unknown. I want to collect the IPs as a delimited list IP1|IP2|IPn and split them into separate variables. The below snippet is working fine with two variables.
- name: Configure ACL
lineinfile:
path: /server.xml
insertafter: '<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b"/>\n'
line: '<Valve className=\"org.apache.catalina.valves.RemoteAddrValve\" allow=\"{{ip1}}|{{ip2}}\"\ />'
Questions:
Upvotes: 0
Views: 718
Reputation: 461
With a list of comma delimited IP addresses (YAML) such as:
ips: 1.1.1.1,2.2.2.2,3.3.3.3
You can use the Python split
method in your Jinja2 templating to obtain a list:
{{ ips.split(',') }}
And template it out with the join
Jinja2 filter:
- name: Configure ACL
vars:
ip_list: "{{ ips.split(',') }}"
lineinfile:
path: /server.xml
insertafter: '<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b"/>\n'
line: '<Valve className=\"org.apache.catalina.valves.RemoteAddrValve\" allow=\"{{ ip_list|join('|') }}\"\ />'
The join
filter takes 1 argument, which is the character to delimit each item in the list in the output of the template.
Line output:
<Valve className=\"org.apache.catalina.valves.RemoteAddrValve\" allow=\"1.1.1.1|2.2.2.2|3.3.3.3\"\ />
Upvotes: 3