Ansible拆分字符串到n个变量数字
我必须从集群中使用Server IP更新配置文件。群集包含至少2个服务器,但是,群集中的最大服务器数量未知。我想将IP作为定界列表IP1 | IP2 | IPN收集,然后将它们分为单独的变量。 以下片段使用两个变量正常工作。
- 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}}\"\ />'
问题:
- 如何将收集的字符串分为单独的变量?
- 如何将上述插入物概括为n个IP的数量?
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:
- How can I split a collected string into separate variables?
- How can I generalize the above insert to n number of IPs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用逗号界定的IP地址列表(YAML),例如:
您可以在Jinja2中使用Python
split
方法来获取列表:并使用
join jinja2滤波器:
join
过滤器采用1个参数,这是界定模板输出中列表中每个项目的字符。线输出:
With a list of comma delimited IP addresses (YAML) such as:
You can use the Python
split
method in your Jinja2 templating to obtain a list:And template it out with the
join
Jinja2 filter: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: