创建包含多对元素的字典

创建包含多对元素的字典

目录中有文件:

environment1_--_192.168.12.239
environment1_--_192.168.12.46
environment1_--_192.168.12.72
environment1_--_192.168.12.83
environment2_--_192.168.12.150
environment2_--_192.168.12.53
environment2_--_192.168.12.44
environment2_--_192.168.12.90

查找文件:

- ansible.builtin.find:
    paths: "./environments/"
    file_type: file
  register: environment_hosts_files

我需要获取一个字典列表:

environment_hosts:
  - name: environment1
    hosts:
      - 192.168.12.239
      - 192.168.12.46
      - 192.168.12.72
      - 192.168.12.83
  - name: environment2
    hosts:
      - 192.168.12.150
      - 192.168.12.53
      - 192.168.12.44
      - 192.168.12.90

这个怎么做?

答案1

获取列表

    files: "{{ environment_hosts_files.files |
               map(attribute='path') |
               map('basename') |
               map('replace', '_', '') |
               map('split', '--') |
               map('zip', ['name', 'host']) |
               map('map', 'reverse') |
               map('community.general.dict') |
               groupby('name') }}"

给出

  files:
    - - environment1
      - - {host: 192.168.12.72, name: environment1}
        - {host: 192.168.12.239, name: environment1}
        - {host: 192.168.12.83, name: environment1}
        - {host: 192.168.12.46, name: environment1}
    - - environment2
      - - {host: 192.168.12.53, name: environment2}
        - {host: 192.168.12.150, name: environment2}
        - {host: 192.168.12.44, name: environment2}
        - {host: 192.168.12.90, name: environment2}

获取环境和主机列表,并创建字典

    env: "{{ files | map('first') }}"
    host: "{{ files | map('last') |
              map('map', attribute='host') }}"
    env_hosts: "{{ dict(env|zip(hosts)) }}"

给出

  env_hosts:
    environment1: [192.168.12.72, 192.168.12.239, 192.168.12.83, 192.168.12.46]
    environment2: [192.168.12.53, 192.168.12.150, 192.168.12.44, 192.168.12.90]

如果需要,请将字典转换为列表

    env_hosts_list: "{{ env_hosts |
                        dict2items(key_name='name', value_name='hosts') }}"

给出

  env_hosts_list:
    - hosts: [192.168.12.72, 192.168.12.239, 192.168.12.83, 192.168.12.46]
      name: environment1
    - hosts: [192.168.12.53, 192.168.12.150, 192.168.12.44, 192.168.12.90]
      name: environment2

给定树

shell> tree /tmp/ansible/env
/tmp/ansible/env
├── environment1_--_192.168.12.239
├── environment1_--_192.168.12.46
├── environment1_--_192.168.12.72
├── environment1_--_192.168.12.83
├── environment2_--_192.168.12.150
├── environment2_--_192.168.12.44
├── environment2_--_192.168.12.53
└── environment2__--_192.168.12.90

完整测试剧本的示例

- hosts: all

  vars:

    files: "{{ environment_hosts_files.files |
               map(attribute='path') |
               map('basename') |
               map('replace', '_', '') |
               map('split', '--') |
               map('zip', ['name', 'host']) |
               map('map', 'reverse') |
               map('community.general.dict') |
               groupby('name') }}"
    env: "{{ files | map('first') }}"
    host: "{{ files | map('last') |
              map('map', attribute='host') }}"
    env_hosts: "{{ dict(env|zip(host)) }}"
    env_hosts_list: "{{ env_hosts |
                        dict2items(key_name='name',
                                   value_name='hosts') }}"

  tasks:

    - ansible.builtin.find:
        paths: /tmp/ansible/env
        file_type: file
      register: environment_hosts_files

    - debug:
        var: files | to_yaml
    - debug:
        var: env_hosts | to_yaml
    - debug:
        var: env_hosts_list | to_yaml

相关内容