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
    • 最新
    • 标签
主页 / coding / 问题 / 77106000
Accepted
AbreQueVoy
AbreQueVoy
Asked: 2023-09-14 22:39:44 +0800 CST2023-09-14 22:39:44 +0800 CST 2023-09-14 22:39:44 +0800 CST

如何从组中的所有主机中提取多个主机变量?

  • 772

这是此问题中解决的问题的后续行动。

需要从主机添加另一个变量,即INSTANCE_CLASS. 主机变量在目录中保存为无扩展名文件host_vars,以主机名作为文件名,例如host12:

---
ansible_host: 10.11.12.15
INSTANCE_CLASS: vm_host
...

链接的问题中描述了从多个主机中提取 hostvar ,但是当我尝试创建字典列表而不是字符串列表时,结果并不像我预期的那样。

尝试#1:

  hosts: general
  gather_facts: false
  vars:
    ip_list: "{{ ansible_play_hosts_all | map('extract', hostvars, 'ansible_hostname') }}"
  tasks:
    - name: Get all IPs
      set_fact:
        ne_hosts:
          - ip: "{{ hostvars[item]['ansible_host'] }}"
            instance_class: "{{ hostvars[item]['INSTANCE_CLASS'] }}"
      loop: "{{ ip_list }}"
      delegate_to: localhost
      delegate_facts: true
      run_once: true
-
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Display gathered IPs
      debug:
        var: ne_hosts

这个会覆盖之前的变量,只留下数组中的最后一个:

TASK [Display gathered IPs] ***************************************************************************************************************************************
ok: [localhost] => {
    "ne_hosts": [
        {
            "instance_class": "mdd_controller",
            "ip": "10.11.12.19"
        }
    ]
}

尝试#2:

  hosts: general
  gather_facts: false
  vars:
    ip_list: "{{ ansible_play_hosts_all | map('extract', hostvars, 'ansible_host') }}" ### changed var
  tasks:
    - name: Get all IPs
      set_fact:
        ne_hosts: "ip: {{ ip_list }}, instance_class: {{ INSTANCE_CLASS }}" ### changed line
      delegate_to: localhost
      delegate_facts: true
      run_once: true
-
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Display gathered IPs
      debug:
        var: ne_hosts

这给出了完整的 IP 列表,但仅显示最后一个实例类:

TASK [Display gathered IPs] ***************************************************************************************************************************************
ok: [localhost] => {
    "ne_hosts": "ip: ['10.11.12.15', '10.11.12.19'], instance_class: mdd_controller"
}

怎样做才能收到类似的东西:

"ne_hosts": "[
    {ip: '10.11.12.15', instance_class: vm_host},
    {ip: '10.11.12.19', instance_class: mdd_controller}
]"

?

ansible
  • 2 2 个回答
  • 26 Views

2 个回答

  • Voted
  1. Best Answer
    Vladimir Botka
    2023-09-15T00:53:06+08:002023-09-15T00:53:06+08:00

    下面的声明给了你你想要的

    ne_hosts: "{{ ansible_play_hosts_all|
                  map('extract', hostvars)|
                  json_query('[].{ip: ansible_host,
                                  instance_class: INSTANCE_CLASS}') }}"
    

    例如,给定库存

    shell> cat hosts
    [general]
    host11
    host12
    host13
    

    和主机变量

    shell> cat host_vars/host11
    ansible_host: 10.1.0.11
    INSTANCE_CLASS: vm_host_11
    
    shell> cat host_vars/host12
    ansible_host: 10.1.0.12
    INSTANCE_CLASS: vm_host_12
    
    shell> cat host_vars/host13
    ansible_host: 10.1.0.13
    INSTANCE_CLASS: vm_host_13
    

    剧本

    shell> cat pb.yml
    - hosts: general
    
      vars:
    
        ne_hosts: "{{ ansible_play_hosts_all|
                      map('extract', hostvars)|
                      json_query('[].{ip: ansible_host,
                                      instance_class: INSTANCE_CLASS}') }}"
      tasks:
    
        - debug:
            msg: "ansible_host: {{ ansible_host }} INSTANCE_CLASS: {{ INSTANCE_CLASS }}"
        - debug:
            var: ne_hosts|to_yaml
          run_once: true
    

    给出(删节)

      ne_hosts:
        - {instance_class: vm_host_11, ip: 10.1.0.11}
        - {instance_class: vm_host_12, ip: 10.1.0.12}
        - {instance_class: vm_host_13, ip: 10.1.0.13}
    

    注:以上声明有效,因为该剧的主持人仅限于集团将军。因此,列表ansible_play_hosts_all保留与groups.general相同的项目。回答你的问题:

    问:“如何从组中的所有主机中提取多个主机变量?”

    答:更好的答案可能是

    ne_hosts: "{{ groups.general|
                  map('extract', hostvars)|
                  json_query('[].{ip: ansible_host,
                                  instance_class: INSTANCE_CLASS}') }}"
    
    • 2
  2. jsxqf
    2023-09-14T23:27:27+08:002023-09-14T23:27:27+08:00

    基本上你问的是如何在 ansible 中附加一个数组。

    尝试这个:

    - hosts: general
      gather_facts: false
      vars:
        ip_list: "{{ ansible_play_hosts_all | map('extract', hostvars, 'ansible_hostname') }}"
        ne_hosts: [] # initial empty array
      tasks:
        - name: Get all IPs
          set_fact:
            ne_hosts: "{{ ne_hosts  + [{'ip': hostvars[item]['ansible_host'] , 'instance_class': hostvars[item]['INSTANCE_CLASS'] }] }}" # append object
          loop: "{{ ip_list }}"
          delegate_to: localhost
          delegate_facts: true
          run_once: true
    
    - hosts: localhost
      gather_facts: false
      tasks:
        - name: Display gathered IPs
          debug:
            var: ne_hosts
    
    • 1

相关问题

  • 仅在本地主机上运行一次 ansible 任务

  • 如何在 Ansible 循环中注册变量?

  • Ansible 中是否可以使用寄存器动态分配文件源?[关闭]

  • 通过 Ansible map('regex_replace') 后比较两个列表

Sidebar

Stats

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

    使用 <font color="#xxx"> 突出显示 html 中的代码

    • 2 个回答
  • Marko Smith

    为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类?

    • 1 个回答
  • Marko Smith

    您可以使用花括号初始化列表作为(默认)模板参数吗?

    • 2 个回答
  • Marko Smith

    为什么列表推导式在内部创建一个函数?

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Marko Smith

    java.lang.NoSuchMethodError: 'void org.openqa.selenium.remote.http.ClientConfig.<init>(java.net.URI, java.time.Duration, java.time.Duratio

    • 3 个回答
  • Marko Smith

    为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)?

    • 4 个回答
  • Marko Smith

    为什么库中不调用全局变量的构造函数?

    • 1 个回答
  • Marko Smith

    std::common_reference_with 在元组上的行为不一致。哪个是对的?

    • 1 个回答
  • Marko Smith

    C++17 中 std::byte 只能按位运算?

    • 1 个回答
  • Martin Hope
    fbrereto 为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类? 2023-12-21 00:31:04 +0800 CST
  • Martin Hope
    比尔盖子 您可以使用花括号初始化列表作为(默认)模板参数吗? 2023-12-17 10:02:06 +0800 CST
  • Martin Hope
    Amir reza Riahi 为什么列表推导式在内部创建一个函数? 2023-11-16 20:53:19 +0800 CST
  • Martin Hope
    Michael A fmt 格式 %H:%M:%S 不带小数 2023-11-11 01:13:05 +0800 CST
  • Martin Hope
    God I Hate Python C++20 的 std::views::filter 未正确过滤视图 2023-08-27 18:40:35 +0800 CST
  • Martin Hope
    LiDa Cute 为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)? 2023-08-24 20:46:59 +0800 CST
  • Martin Hope
    jabaa 为什么库中不调用全局变量的构造函数? 2023-08-18 07:15:20 +0800 CST
  • Martin Hope
    Panagiotis Syskakis std::common_reference_with 在元组上的行为不一致。哪个是对的? 2023-08-17 21:24:06 +0800 CST
  • Martin Hope
    Alex Guteniev 为什么编译器在这里错过矢量化? 2023-08-17 18:58:07 +0800 CST
  • Martin Hope
    wimalopaan C++17 中 std::byte 只能按位运算? 2023-08-17 17:13:58 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

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

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve