Ansible内联循环创建列表

huangapple go评论82阅读模式
英文:

Ansible create a list from a loop inline

问题

我有一个操作手册,内容如下:

- name: 安装 PHP 扩展
  ansible.builtin.apt:
    pkg:
    - php{{ php_version }}{{ item.pkg_extension }}
  loop:
    - { pkg_extension: -mysql }
    - { pkg_extension: -bz2 }
    - { pkg_extension: -curl }
    - { pkg_extension: -gd }
    - { pkg_extension: -imap }
    - { pkg_extension: -mbstring }
    - { pkg_extension: -xml }
    - { pkg_extension: -zip }
  when: php_key_setup is succeeded and nginx_install is succeeded and php_base_install is succeeded
  register: php_install

有时出现我不明白的原因,这个循环中的某个包安装失败。我想将这些包作为列表或字典传递给 apt 模块,就像这样:

- name: 最佳 yum
  ansible.builtin.yum:
    name: "{{ list_of_packages }}"
    state: present

如何从这个循环中创建这个列表,将所有内容连接起来,而不需要额外的步骤来创建这个列表并在之后重复使用它呢?

英文:

I have a playbook that goes like this:

    - name: Install php extensions
      ansible.builtin.apt:
        pkg:
        - php{{ php_version }}{{ item.pkg_extension }}
       loop:
        - { pkg_extension: -mysql }
        - { pkg_extension: -bz2 }
        - { pkg_extension: -curl }
        - { pkg_extension: -gd }
        - { pkg_extension: -imap }
        - { pkg_extension: -mbstring }
        - { pkg_extension: -xml }
        - { pkg_extension: -zip }
      when: php_key_setup is succeeded and nginx_install is succeeded and php_base_install is succeeded
      register: php_install

Sometimes for reasons unknown to me, this fails for one of the packages in the loop. I want to pass these packages as a list or a dictionary to the apt module instead, like in

https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_loops.html#iterating-over-a-simple-list :

- name: Optimal yum
  ansible.builtin.yum:
    name: "{{ list_of_packages }}"
    state: present

How can I create this list from this loop, concatenating everything without adding an extra step to create this list and reuse it afterwards?

答案1

得分: 3

如果您有一个扩展名列表,您可以使用map来对每个项目应用regex_replace过滤器,就像这样:

- hosts: localhost
  gather_facts: false
  vars:
    php_version: 8.3
    php_pkg_extensions:
        - mysql
        - bz2
        - curl
        - gd
        - imap
        - mbstring
        - xml
        - zip
  tasks:
    - name: 安装 PHP 扩展
      ansible.builtin.apt:
        pkg: "{{ packages }}"
      vars:
        packages: >-
          {{
            php_pkg_extensions |
            map('regex_replace', '^(.*)$', 'php%s-\' % (php_version,))
          }}          

不需要php_pkg_extensions变量,但它会使事情更清晰。您可以改为这样编写:

- hosts: localhost
  gather_facts: false
  vars:
    php_version: 8.3
  tasks:
    - name: 安装 PHP 扩展
      ansible.builtin.apt:
        pkg: "{{ packages }}"
      vars:
        packages: >-
          {{
            ["mysql", "bz2", "curl", "gd", "imap", "mbstring", "xml", "zip"] |
            map('regex_replace', '^(.*)$', 'php%s-\' % (php_version,))
          }}          
英文:

If you have the list of extensions in a list, you can use map to apply the regex_replace filter to each item, like this:

- hosts: localhost
  gather_facts: false
  vars:
    php_version: 8.3
    php_pkg_extensions:
        - mysql
        - bz2
        - curl
        - gd
        - imap
        - mbstring
        - xml
        - zip
  tasks:
    - name: Install php extensions
      ansible.builtin.apt:
        pkg: "{{ packages }}"
      vars:
        packages: >-
          {{
            php_pkg_extensions |
            map('regex_replace', '^(.*)$', 'php%s-' % (php_version,))
          }}

You don't need the php_pkg_extensions variable, but it makes things much cleaner. You could instead write:

- hosts: localhost
  gather_facts: false
  vars:
    php_version: 8.3
  tasks:
    - name: Install php extensions
      ansible.builtin.apt:
        pkg: "{{ packages }}"
      vars:
        packages: >-
          {{
            ["mysql", "bz2", "curl", "gd", "imap", "mbstring", "xml", "zip"] |
            map('regex_replace', '^(.*)$', 'php%s-' % (php_version,))
          }}

答案2

得分: 2

给定以下变量

```yaml
  php_version: 8.3
  php_pkg_extensions:
    - mysql
    - bz2
    - curl
    - gd
    - imap
    - mbstring
    - xml
    - zip

声明列表

  packages: "{{ ['php' ~ php_version]|
                product(php_pkg_extensions)|
                map('join', '-') }}"

将得到

  packages:
  - php8.3-mysql
  - php8.3-bz2
  - php8.3-curl
  - php8.3-gd
  - php8.3-imap
  - php8.3-mbstring
  - php8.3-xml
  - php8.3-zip

<hr>

<sup>

用于测试的完整playbook示例

- hosts: localhost

  vars:

    php_version: 8.3
    php_pkg_extensions:
        - mysql
        - bz2
        - curl
        - gd
        - imap
        - mbstring
        - xml
        - zip
    packages: "{{ ['php' ~ php_version]|
                  product(php_pkg_extensions)|
                  map('join', '-') }}"

  tasks:

    - debug:
        var: packages

    - name: 安装php扩展
      ansible.builtin.apt:
        pkg: "{{ packages }}"
      when: install_php_extensions|d(false)|bool

</sup>


<details>
<summary>英文:</summary>

Given the variables

```yaml
  php_version: 8.3
  php_pkg_extensions:
    - mysql
    - bz2
    - curl
    - gd
    - imap
    - mbstring
    - xml
    - zip

Declare the list

  packages: &quot;{{ [&#39;php&#39; ~ php_version]|
                product(php_pkg_extensions)|
                map(&#39;join&#39;, &#39;-&#39;) }}&quot;

gives what you want

  packages:
  - php8.3-mysql
  - php8.3-bz2
  - php8.3-curl
  - php8.3-gd
  - php8.3-imap
  - php8.3-mbstring
  - php8.3-xml
  - php8.3-zip

<hr>

<sup>

Example of a complete playbook for testing

- hosts: localhost

  vars:

    php_version: 8.3
    php_pkg_extensions:
        - mysql
        - bz2
        - curl
        - gd
        - imap
        - mbstring
        - xml
        - zip
    packages: &quot;{{ [&#39;php&#39; ~ php_version]|
                  product(php_pkg_extensions)|
                  map(&#39;join&#39;, &#39;-&#39;) }}&quot;

  tasks:

    - debug:
        var: packages

    - name: Install php extensions
      ansible.builtin.apt:
        pkg: &quot;{{ packages }}&quot;
      when: install_php_extensions|d(false)|bool

</sup>

huangapple
  • 本文由 发表于 2023年7月10日 23:08:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定