Ansible IP地址变量远程主机

时间:2019-05-31 16:17:27

标签: python shell ansible ip

我有以下问题:

我正在编写用于配置NFS服务器和NFS客户端的剧本。

在同一本剧本中,我还包括其他两项任务:

CentOS_7_NFS_Server.yml和CentOS_7_NFS_Client.yml

因为我可以执行比较

的IP地址所需的任务之一

远程主机

示例:

- include: CentOS_7_NFS_Server.yml
  when: ansible_all_ipv4_addresses == NFS_Server

- include: CentOS_7_NFS_Client.yml
  when:  ansible_all_ipv4_addresses == NFS_Client 

在var中:

NFS_Server: x.x.x.x
NFS_Client : y.y.y.y

我收到此错误

FAILED! => {"msg": "The conditional check 'ansible_all_ipv4.address 
== NFS_Client' failed. The error was: error while evaluating 
conditional (ansible_all_ipv4.address == NFS_Client): 
'ansible_all_ipv4' is undefined\n\nThe error appears to have been 
in

1 个答案:

答案 0 :(得分:0)

很有可能已禁用facts的收集。当您看到错误时:

  

ansible_all_ipv4_addresses:变量未定义!

启用

- hosts: nfs_server_and_client
  gather_facts: yes

还有另一个问题。 ansible_all_ipv4_addresses 所有地址的列表。例如。

  ansible_all_ipv4_addresses:
  - 192.168.122.1
  - 10.1.0.11
  - 192.168.1.10

您必须选择一个或多个并将其分配给 NFS_Server NFS_Client 。然后,如果 NFS_Server NFS_Client 分别在列表 ansible_all_ipv4_addresses 中,则条件为test

- include: CentOS_7_NFS_Server.yml
  when: NFS_Server in ansible_all_ipv4_addresses

- include: CentOS_7_NFS_Client.yml
  when: NFS_Client in ansible_all_ipv4_addresses