在Playbook中使用Ansible广告资源IP

时间:2019-05-30 10:41:18

标签: ansible ansible-inventory

我有一个名为inventory.ini的清单文件,其中仅包含以下内容:

10.0.0.4
10.0.0.5

我想使用我的剧本中此处列出的IP,位于变量ip_address下:

- name: Import & load configuration file into PAN-OS
  hosts: localhost
  connection: local
  gather_facts: False

  vars:
    ip_address: "{{ SOMETHING-GOES-HERE }}"
    username: "#{PALOS_USERNAME}#"
    password: "#{PALOS_PASSWORD}#"
    config_file: ""
    load_file: ""

  roles:
  - role: PaloAltoNetworks.paloaltonetworks

  tasks:
  - name: wait for reboot
    panos_check:
      ip_address: "{{ ip_address }}"
      username: "{{ username }}"
      password: "{{ password }}"
      interval: 5
      timeout: 900

  - name: import configuration file into PAN-OS
    panos_import:
      ip_address: "{{ ip_address }}"
      username: "{{ username }}"
      password: "{{ password }}"
      file: "{{ config_file }}"
      category: "configuration"

我尝试使用{{ inventory_hostname }},但是却带回了localhost。我也尝试过使用{{ ansible_host }},但这会带回127.0.0.1

有没有人知道我如何使用清单中列出的IP来代替变量在每次部署中循环?

请注意,我以这种方式运行剧本:

ansible-playbook panos-config.yml -i inventory.ini --extra-vars "config_file=./xml-config/asdf.xml load_file=asdf.xml" -vvv

可用的版本是2.8.0

1 个答案:

答案 0 :(得分:0)

结果是,我需要将hosts更改为all,并将{{ ansible_host }}留在ip_address变量字段中。 IP从清单文件中拉出并可以正常部署。像这样:

- name: Import & load configuration file into PAN-OS
  hosts: all
  connection: local
  gather_facts: False

  vars:
    ip_address: "{{ ansible_host }}"
    username: "#{PALOS_USERNAME}#"
    password: "#{PALOS_PASSWORD}#"
    config_file: ""
    load_file: ""

  roles:
  - role: PaloAltoNetworks.paloaltonetworks

  tasks:
  - name: wait for reboot
    panos_check:
      ip_address: "{{ ip_address }}"
      username: "{{ username }}"
      password: "{{ password }}"
      interval: 5
      timeout: 900

  - name: import configuration file into PAN-OS
    panos_import:
      ip_address: "{{ ip_address }}"
      username: "{{ username }}"
      password: "{{ password }}"
      file: "{{ config_file }}"
      category: "configuration"