尽管使用了 set_fact 和 HostVars,但无法从另一个播放中获取变量

unable to get variable from another play despite using set_fact and hostvars

提问人:Ashar 提问时间:11/9/2023 更新时间:11/9/2023 访问量:67

问:

我无法从其他主机读取内容。set_fact

我的 ansible 版本是 2.8.4

以下是我的剧本:

[wladmin@ansiblehost ~]$ cat getvar.yml
 
---
- name: "Play 1"

  hosts: localhost
  any_errors_fatal: True
  serial: 1
  gather_facts: no

  tasks:

   - add_host:
       name: "sourcehost"
       groups: source_node
       ansible_user: "linuxuser"

   - add_host:
       name: "desthost"
       groups: "dest_node"
       ansible_user: "linuxuser"
 

- name: "Gather facts from source_node"
  hosts: source_node

  tasks:

    - name: Set copyuser variable on source_node
      set_fact:
        copyuser: "HELLO"

- name: "Configure Destination node {{ dest_node }}"

  hosts: dest_node
  any_errors_fatal: True
  gather_facts: false
  serial: 1

  tasks:

    - debug:
        msg: "The value of copyuser from source_node is {{ hostvars['source_node']['copyuser'] }}"

错误输出在这里:

[wladmin@ansiblehost ~]$ ansible-playbook getvar.yml


PLAY [Play 1] *****************************************************************************************************************


TASK [add_host] ***************************************************************************************************************

Thursday 09 November 2023  01:26:59 -0600 (0:00:00.048)       0:00:00.049 *****

changed: [localhost]

 

TASK [add_host] ***************************************************************************************************************

Thursday 09 November 2023  01:26:59 -0600 (0:00:00.017)       0:00:00.066 *****

changed: [localhost]

 

PLAY [Gather facts from source_node] ******************************************************************************************

 

TASK [Gathering Facts] ********************************************************************************************************

Thursday 09 November 2023  01:26:59 -0600 (0:00:00.024)       0:00:00.091 *****

ok: [sourcehost]

 

TASK [Set copyuser variable on source_node] ***********************************************************************************

Thursday 09 November 2023  01:27:02 -0600 (0:00:03.095)       0:00:03.186 *****

ok: [sourcehost]

 

PLAY [Configure Destination node {{ dest_node }}] *****************************************************************************

 

TASK [debug] ******************************************************************************************************************

Thursday 09 November 2023  01:27:02 -0600 (0:00:00.028)       0:00:03.214 *****

fatal: [desthost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: \"hostvars['source_node']\" is undefined\n\nThe error appears to be in '/home/wladmin/getvar.yml': line 37, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  tasks:\n    - debug:\n      ^ here\n"}

 

NO MORE HOSTS LEFT ************************************************************************************************************

 

PLAY RECAP ********************************************************************************************************************

sourcehost               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

desthost               : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

localhost                  : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

 

Thursday 09 November 2023  01:27:03 -0600 (0:00:00.018)       0:00:03.233 *****

===============================================================================

Gathering Facts -------------------------------------------------------------------------------------------------------- 3.10s

Set copyuser variable on source_node ----------------------------------------------------------------------------------- 0.03s

add_host --------------------------------------------------------------------------------------------------------------- 0.02s

debug ------------------------------------------------------------------------------------------------------------------ 0.02s

add_host --------------------------------------------------------------------------------------------------------------- 0.02s
ansible scope 运行时错误 全局变量 ansible-inventory

评论


答: 暂无答案