Ansible - 循环两个列表

发布于 2025-01-11 14:31:50 字数 1537 浏览 0 评论 0原文

我看过类似的问题,虽然我已经解决了问题,但我认为这不是最优雅的解决方案。

我正在尝试循环访问网站列表 - 这些网站是单独的词典。对于每个站点,我想使用 lineinfile 对站点内的配置文件执行一系列替换(配置文件的路径是根据站点字典中的信息确定的)

我想执行一个循环通过 lineinfile 这两个网站,以及要尝试的正则表达式/替换列表。唯一的问题是,替换列表需要使用站点字典中找到的值。

我目前通过以下方式实现这一点,在 playbook.yml 中:

- name: Perform replacements with loop over sites
  ansible.builtin.include_tasks: replacements.yml
  tags: test
  loop: "{{ sites }}"
  loop_control:
    loop_var: site
  vars:
    sites:
      - apache_servername: "site1.com"
        apache_documentroot: /var/www/site1
      - apache_servername: "site2.com"
        apache_documentroot: /var/www/site2

以及 replacements.yml 的内容:

- name: Perform replacements
  ansible.builtin.lineinfile:
    path: "{{ site.apache_documentroot }}/config.txt"
    backrefs: yes
    regexp: "{{ item.regex }}"
    state: present
    line: "{{ item.replacement }}"
  loop:
    - {regex: "(public \\$tmp_path.*?')(?:.+)(';)", replacement: "\\1{{ site.apache_documentroot }}/tmp\\2"}
    - {regex: "(public \\$log_path.*?')(?:.+)(';)", replacement: "\\1{{ site.apache_documentroot }}/administrator/logs\\2"}
    - {regex: "(public \\$password.*?')(?:.+)(';)", replacement: "\\1{{ site.password }}\\2"}
  tags: test

这工作正常,但它有点不优雅的分割跨多个文件,更不用说控制这些正则表达式替换并不是最容易的 - 如果它们可以保存在单独的变量中,那就太好了。

是否可以在同一任务中一起循环这两个字典列表,同时还允许正则表达式替换引用第一个循环的值?我有点想象构建一个包含所有这些内容的数据结构,然后对其进行循环。

I've looked at similar questions and although I've solved the problem, I don't think it's the most elegant solution.

I'm trying to loop over a list of sites - which are individual dictionaries. For each site, I want to perform a series of replacements on a config file within the site using lineinfile (the path to the config file is determined from information in the sites dictionary)

I want to perform a loop over lineinfile with both these sites, and a list of regex/replacements to try. The only problem is, the list of replacements needs to use a value found within the sites dictionary.

I'm currently achieving this the following way, in playbook.yml:

- name: Perform replacements with loop over sites
  ansible.builtin.include_tasks: replacements.yml
  tags: test
  loop: "{{ sites }}"
  loop_control:
    loop_var: site
  vars:
    sites:
      - apache_servername: "site1.com"
        apache_documentroot: /var/www/site1
      - apache_servername: "site2.com"
        apache_documentroot: /var/www/site2

And the contents of replacements.yml:

- name: Perform replacements
  ansible.builtin.lineinfile:
    path: "{{ site.apache_documentroot }}/config.txt"
    backrefs: yes
    regexp: "{{ item.regex }}"
    state: present
    line: "{{ item.replacement }}"
  loop:
    - {regex: "(public \\$tmp_path.*?')(?:.+)(';)", replacement: "\\1{{ site.apache_documentroot }}/tmp\\2"}
    - {regex: "(public \\$log_path.*?')(?:.+)(';)", replacement: "\\1{{ site.apache_documentroot }}/administrator/logs\\2"}
    - {regex: "(public \\$password.*?')(?:.+)(';)", replacement: "\\1{{ site.password }}\\2"}
  tags: test

This works fine, but it is a little bit inelegant split out across multiple files, not to mention it's not the easiest to control those regex replacements - would be good if they could be held in a separate variable.

Is it possible to loop over these two lists of dictionaries together within the same task, whilst also allowing the regex replacements to reference a value of the first loop? I sort of imagine building a data structure that has all of these things created and then just looping over that.

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

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

发布评论

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

评论(1

眼泪淡了忧伤 2025-01-18 14:31:50

迭代with_nested< /a>.例如,简化的测试

    - name: Perform replacements with loop over sites
      debug:
        msg: |
          path: {{ item.0.apache_documentroot }}/config.txt
          regexp: {{ item.1.regex }}
          line: {{ item.1.replace }}
      with_nested:
        - "{{ sites }}"
        - "{{ regex_replace }}"
      vars:
        sites:
          - apache_servername: site1.com
            apache_documentroot: /var/www/site1
          - apache_servername: site2.com
            apache_documentroot: /var/www/site2
        regex_replace:
          - {regex: A, replace: X}
          - {regex: B, replace: Y}

给出(删节)

  msg: |-
    path: /var/www/site1/config.txt
    regexp: A
    line: X
--
  msg: |-
    path: /var/www/site1/config.txt
    regexp: B
    line: Y
--
  msg: |-
    path: /var/www/site2/config.txt
    regexp: A
    line: X
--
  msg: |-
    path: /var/www/site2/config.txt
    regexp: B
    line: Y

Iterate with_nested. For example, simplified for the testing

    - name: Perform replacements with loop over sites
      debug:
        msg: |
          path: {{ item.0.apache_documentroot }}/config.txt
          regexp: {{ item.1.regex }}
          line: {{ item.1.replace }}
      with_nested:
        - "{{ sites }}"
        - "{{ regex_replace }}"
      vars:
        sites:
          - apache_servername: site1.com
            apache_documentroot: /var/www/site1
          - apache_servername: site2.com
            apache_documentroot: /var/www/site2
        regex_replace:
          - {regex: A, replace: X}
          - {regex: B, replace: Y}

gives (abridged)

  msg: |-
    path: /var/www/site1/config.txt
    regexp: A
    line: X
--
  msg: |-
    path: /var/www/site1/config.txt
    regexp: B
    line: Y
--
  msg: |-
    path: /var/www/site2/config.txt
    regexp: A
    line: X
--
  msg: |-
    path: /var/www/site2/config.txt
    regexp: B
    line: Y
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文