【问题标题】:get ansible groups by looping with_items通过循环 with_items 获取 ansible 组
【发布时间】:2020-06-23 15:30:52
【问题描述】:

我正在尝试通过以下不起作用的过程获取清单主机组名称

- debug:
   msg: "{{ groups['{{ item }}'] }}"
  with_items: "{{ vm.stdout_lines }}"

这就是我真正想做的事情 我将通过 shell 脚本获取服务器列表

    - name: Getting the servers list
      shell: |
        sh getServers.sh
      register: vm

然后通过 add_host 将它们添加到库存中

    - name: Creating Logical host_group for each server
      add_host:
       name: "{{ item }}"
       groups: ["{{item }}"]
      with_items: "{{ vm.stdout_lines }}"
      register: inv

我在这里尝试获取我在上述步骤中添加的唯一组,而不是所有组

- debug:
   msg: "{{ groups['{{ item }}'] }}"
  with_items: "{{ vm.stdout_lines }}"

错误是

{"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute '{{ item }}'

感谢您对此的帮助!

【问题讨论】:

    标签: ansible ansible-inventory


    【解决方案1】:

    修正语法

      msg: "{{ groups[item] }}"
    

    【讨论】:

    • 它能够给出第一个项目,但下一个项目给出以下错误 ` ok: [localhost] => (item=localhost-1.local) => { "msg": [ " localhost-1.local" ] } 致命:[localhost]:失败! => {"msg": "该任务包含一个带有未定义变量的选项。错误是:'dict object' has no attribute u'localhost-2.local' `
    • 通过msg: '"{{ groups[item] }}"' 得到它,但得到这样的输出"msg": "\"[u'localhost-1.local']\""
    猜你喜欢
    • 1970-01-01
    • 2019-12-27
    • 1970-01-01
    • 2019-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多