【问题标题】:Unable to get value of a variable from another Play in Ansible无法从 Ansible 中的另一个 Play 获取变量的值
【发布时间】:2019-12-27 20:55:36
【问题描述】:

我希望将Play 2的“fdet_APP”变量的值获取到Play 3中。

以下是我的剧本,该案例用作测试用例:

- name: "Play 1"
  hosts: localhost
  tasks:
   - add_host: name={{ item }}
                        groups=dest_nodes
                        ansible_user={{ USER }}
     with_items: "{{ Dest_IP.split(',') }}"

- name: "Play 2"
  hosts: dest_nodes
  user: "{{ USER }}"
  tasks:
   - set_fact:
       fdet_APP: "Yellow"

- name: "Play 3"
  hosts: localhost
  user: "{{ USER }}"
  vars:
    dbfiledet: "{{ hostvars['dest_nodes']['fdet_APP'] }}"

  tasks:
   - debug: msg="{{ dbfiledet.stdout }}"

我的尝试出现以下错误:

剧本运行命令:

ansible-playbook variabletest.yml -e "USER=user1 Dest_IP=10.17.44.26,10.17.54.26"

[警告]:提供的主机列表为空,只有 localhost 可用。请注意,隐式 localhost 不匹配 'all'

播放 [播放 1]


TASK [收集事实] ****************************************************** ****************************************************** ************************************ 好的:[本地主机]

任务 [add_host] ****************************************************** ****************************************************** ***************************************** 更改:[localhost] =>(项目=10.17.44.26) 更改:[本地主机] => (项目=10.17.54.26)

播放 [播放 2]


TASK [收集事实] ****************************************************** ****************************************************** ************************************ 好的:[10.17.54.26] 好的:[10.17.44.26]

任务 [set_fact] ****************************************************** ****************************************************** ***************************************** 好的:[10.17.44.26] 好的: [10.17.54.26]

播放 [播放 3]


TASK [收集事实] ****************************************************** ****************************************************** ************************************ 好的:[本地主机]

任务 [调试] ****************************************************** ****************************************************** ************************************************ 致命:[本地主机]:失败的! => {"msg": "任务包含一个选项 带有未定义的变量。错误是:\"hostvars['dest_nodes']\" 未定义\n\n错误似乎在“variabletest.yml”中:行 36,第 6 列,但可能\n位于文件中的其他位置,具体取决于确切的 语法问题。\n\n有问题的行似乎是:\n\n\n - 调试: msg=\"{{ dbfiledet.stdout }}\"\n ^ here\n我们可能错了,但是 这个看起来可能是\n缺少引号的问题。总是 当它们\n开始一个值时,引用模板表达式括号。为了 实例:\n\n with_items:\n - {{ foo }}\n\n应该写成 如:\n\n with_items:\n - \"{{ foo }}\"\n"}

播放回顾


10.17.44.26 : ok=2 更改=0 无法访问=0 失败=0 跳过=0 获救=0 忽略=0
10.17.54.26 : ok=2 更改=0 无法访问=0 失败=0 跳过=0 获救=0 忽略=0 localhost
: ok=3 改变=1 不可达=0 失败=1 跳过=0
获救=0 忽略=0

我正在使用最新版本的 ansible 和 python 2.7.5

有人能建议什么是错的吗?请问我如何获得 Play 3 中变量的值?

【问题讨论】:

    标签: variables ansible


    【解决方案1】:

    hostvars 绑定到单个 Ansible 托管主机,而不是清单组。尝试运行debug: var=hostvars 看看我的意思。在您的情况下,dest_nodes 是库存组,而不是主机。

    如果只是想从组中的任何主机中提取变量,请尝试:

    - debug:
        msg: "{{ hostvars[groups.dest_nodes|first]['fdet_APP'] }}"
    

    如果您要解析组中所有主机的值,则需要实现循环或 json_query 过滤器

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多