ansible多维数组

时间:2019-01-19 15:56:42

标签: automation deployment ansible

我开始学习ansible,然后尝试安装PHP和php扩展。 我有一个文件,在其中声明了我的php版本和php扩展名,其外观如下:

    php:
  versions:
    - 7.2:
      - cli
      - fpm
      - curl
      - mysql
      - pdo
      - zip
      - xml
      - gd
      - mbstring
      - tokenizer
      - ctype
      - json
    - 7.3:
      - cli
      - fpm
      - curl
      - mysql
      - pdo
      - zip
      - xml
      - gd
      - mbstring
      - tokenizer
      - ctype
      - json

之所以这样做,是因为我想学习Ansible,并且还希望获得灵活性,并为每个php版本安装不同的扩展名。

我尝试以此调试我的代码(这在我的playbook.yml文件的“任务”部分下)

- name: DEBUG ANSIBLE PLAYBOOK
  debug:
    msg: The key is - {{ item.keys() | first }} The item is "{{ item[item.keys() | first] }}"
  loop: "{{ php.versions }}"
  loop_control:
    loop_var: item

我得到以下输出:

TASK [DEBUG ANSIBLE PLAYBOOK] 
ok: [127.0.0.1] => (item={7.2: [u'cli', u'fpm', u'curl', u'mysql', u'pdo', u'zip', u'xml', u'gd', u'mbstring', u'tokenizer', u'ctype', u'json']}) => {
    "msg": "The key is - 7.2 The item is \"[u'cli', u'fpm', u'curl', u'mysql', u'pdo', u'zip', u'xml', u'gd', u'mbstring', u'tokenizer', u'ctype', u'json']\""
}
ok: [127.0.0.1] => (item={7.3: [u'cli', u'fpm', u'curl', u'mysql', u'pdo', u'zip', u'xml', u'gd', u'mbstring', u'tokenizer', u'ctype', u'json']}) => {
    "msg": "The key is - 7.3 The item is \"[u'cli', u'fpm', u'curl', u'mysql', u'pdo', u'zip', u'xml', u'gd', u'mbstring', u'tokenizer', u'ctype', u'json']\""
}

然后我尝试使用此任务安装PHP:

- name: Install PHP
  package:
    pkg: php{{ item.keys() | first }}-{{ item[item.keys() | first] }}
    state: latest
  loop: "{{ php.versions }}"
  loop_control:
    loop_var: item
  notify: Restart Nginx

但是不起作用,因为item[item.keys() | first]是一个数组,我不知道如何再次循环以接收cli,fpt而不是数组。

我从3天开始学习ansible,但我真的不明白如何循环更多次。我在网上搜索,阅读了文档,但是我不理解

https://docs.ansible.com/ansible/2.4/playbooks_loops.html

如果有人可以解释,我将非常感谢!

2 个答案:

答案 0 :(得分:0)

一个选择是使用include_tasks。请参阅下面的提示。

tasks:
  - include_tasks: test.yml
    loop: "{{ php.versions }}"
    loop_control:
      loop_var: php_ver

> cat test.yml
- debug: msg="{{ lookup('dict', php_ver).key }}"
- debug: msg="{{ item }}"
  loop: "{{ lookup('dict', php_ver).value }}"

这是剧本

> cat test-31.yml
---
- hosts: localhost
  gather_facts: no
  vars:
    php:
      versions:
        - 7.2:
            - cli
            - fpm
            - curl
            - mysql
            - pdo
            - zip
            - xml
            - gd
            - mbstring
            - tokenizer
            - ctype
            - json
        - 7.3:
            - cli
            - fpm
            - curl
            - mysql
            - pdo
            - zip
            - xml
            - gd
            - mbstring
            - tokenizer
            - ctype
            - json
  tasks:
    - include_tasks: test.yml
      loop: "{{ php.versions }}"
      loop_control:
        loop_var: php_ver

这是输出

> ansible-playbook test-31.yml | grep msg
    "msg": "7.2"
    "msg": "cli"
    "msg": "fpm"
    "msg": "curl"
    "msg": "mysql"
    "msg": "pdo"
    "msg": "zip"
    "msg": "xml"
    "msg": "gd"
    "msg": "mbstring"
    "msg": "tokenizer"
    "msg": "ctype"
    "msg": "json"
    "msg": "7.3"
    "msg": "cli"
    "msg": "fpm"
    "msg": "curl"
    "msg": "mysql"
    "msg": "pdo"
    "msg": "zip"
    "msg": "xml"
    "msg": "gd"
    "msg": "mbstring"
    "msg": "tokenizer"
    "msg": "ctype"
    "msg": "json"

答案 1 :(得分:0)

好吧,经过大量研究,我找到了解决方法

- name: Install PHP && Packages
  become: true
  package:
    pkg: php{{ item.0.version }}-{{ item.1 }}
    state: latest
  with_subelements:
    - "{{ php }}"
    - libs
  notify: Restart Nginx

为我工作。此外,我更改了数组:

php:
  - version: 7.2
    libs:
      - cli
      - fpm
      - curl
      - mysql
      - pdo
      - zip
      - xml
      - gd
      - mbstring
      - tokenizer
      - ctype
      - json
  - version: 7.3
    libs:
      - cli
      - fpm
      - curl
      - mysql
      - pdo
      - zip
      - xml
      - gd
      - mbstring
      - tokenizer
      - ctype
      - json

我在这里允许这样做,也许有人会觉得有用。