AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1156614
Accepted
Konstantin
Konstantin
Asked: 2024-03-21 01:38:01 +0800 CST2024-03-21 01:38:01 +0800 CST 2024-03-21 01:38:01 +0800 CST

创建多对元素的字典

  • 772

目录下有文件:

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

这个怎么做?

ansible
  • 1 1 个回答
  • 34 Views

1 个回答

  • Voted
  1. Best Answer
    Vladimir Botka
    2024-03-21T03:13:43+08:002024-03-21T03:13:43+08:00

    获取列表

        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
    
    • 0

相关问题

  • Ansible:将字符串转换为字典

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve