无法使用其他主机IP地址配置主机

时间:2019-02-19 23:41:23

标签: ansible ansible-facts

我创建了Ansible剧本来配置: -数据库服务器 -Nginx和php服务器

数据库服务器需要知道php服务器的IP地址以配置数据库访问权限。 Web服务器需要知道数据库服务器的IP地址,以配置网站的数据库访问权限。 我尝试了proxy_facts:

vars:
    Database_server_name: "SVWEB-03"
    Web_server_name: "SVWEB-02"


- name: Web server ip address
  set_fact:
    Web_server_ip: "{{  ansible_default_ipv4.address  }}"
  delegate_to: "{{  Web_server_name  }}"
  delegate_facts: True
  when:
    - "Database_server_name in inventory_hostname"

- name: Database server ip address
  set_fact:
    Database_server_ip: "{{  ansible_default_ipv4.address  }}"
  delegate_to: "{{  Database_server_name  }}"
  delegate_facts: True
  when:
    - "Web_server_name in inventory_hostname"

- debug:
    msg: "{{  Web_server_ip  }}"
  when:
    - "Database_server_name in inventory_hostname"

- debug:
    msg: "{{  Database_server_ip  }}"
  when:
    - "Web_server_name in inventory_hostname"

但是我有错误:

TASK [Web server ip address] ***************************************************************************************************
skipping: [SVWEB-02]
ok: [SVWEB-03 -> SVWEB-02]

TASK [Database server ip address] ************************************************************************************************
skipping: [SVWEB-03]
ok: [SVWEB-02 -> SVWEB-03]

TASK [debug] ***************************************************************************************************************
skipping: [SVWEB-02]
fatal: [SVWEB-03]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'Web_server_ip' is undefined\n\nThe error appears to have been in '/home/murmure/ansible/playbooks/Install_Wordpress_Separate_DB_and_Web.yml': line 64, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - debug:\n      ^ here\n"}

TASK [debug] ***************************************************************************************************************
fatal: [SVWEB-02]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'Database_server_ip' is undefined\n\nThe error appears to have been in '/home/murmure/ansible/playbooks/Install_Wordpress_Separate_DB_and_Web.yml': line 69, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - debug:\n      ^ here\n"}

请问有人知道我的错误吗?

1 个答案:

答案 0 :(得分:0)

我相信您需要的是hostvars

- name: Database server ip address
  set_fact:
    Database_server_ip: "{{  hostvars.Database_server_name.ansible_default_ipv4.address  }}"