首页 > 解决方案 > 将事实传递给另一台主机上的角色

问题描述

我有 2 个主机组:A 和 B。在主机组 B 上执行的角色需要对主机组 A 的 IP 地址做一些事情。如何使主机组 A 的 IP 地址可用于主机 B?

主机

[A]
example.host.a.com

[B]
example.host.b.com

部署.yml

---
- name: Gather ip address of host A
  hosts: A
  tasks:
  - set_fact:
      a_host_ip: "{{ ansible_eth0.ipv4.address  }}"

- name: Debug result
  hosts: A
  tasks:
    - debug:
        msg: "Host A ip adress: {{ a_host_ip}}" # prints correct ip address

- name: Deploy role on host B that has to access host A ip address
  hosts: B
  gather_facts: true
  serial: 1
  roles:
    - role: role_b

role_b/tasks/main.yml

---
# 1. try
- debug:
    msg: "IP of host A: {{ a_host_ip }}" # Error: 'a_host_ip' is undefined

# 2. try
- debug:
    msg: "IP of host A: hostvars['A'].a_host_ip" # Error: hostvars['A'] is undefined

# 3. try
- debug:
    msg: "IP of host A: hostvars['A'].ansible_eth0.ipv4.address " # Error: hostvars['A'] is undefined

标签: ansible

解决方案


推荐阅读