Ansible拆分字符串到n个变量数字

发布于 2025-02-07 19:15:34 字数 643 浏览 2 评论 0原文

我必须从集群中使用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 &quot;%r&quot; %s %b"/>\n'
      line: '<Valve className=\"org.apache.catalina.valves.RemoteAddrValve\" allow=\"{{ip1}}|{{ip2}}\"\ />'

问题:

  1. 如何将收集的字符串分为单独的变量?
  2. 如何将上述插入物概括为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:

  1. How can I split a collected string into separate variables?
  2. How can I generalize the above insert to n number of IPs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩发小公举 2025-02-14 19:15:34

使用逗号界定的IP地址列表(YAML),例如:

ips: 1.1.1.1,2.2.2.2,3.3.3.3

您可以在Jinja2中使用Python split方法来获取列表:

{{ ips.split(',') }}

并使用join jinja2滤波器:

  - 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('|') }}\"\ />'

join过滤器采用1个参数,这是界定模板输出中列表中每个项目的字符。

线输出:

<Valve className=\"org.apache.catalina.valves.RemoteAddrValve\" allow=\"1.1.1.1|2.2.2.2|3.3.3.3\"\ />

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\"\ />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文