Search code examples
ansibleansible-inventory

Use Ansible inventory IP in Playbook


I have an inventory file called inventory.ini which contains exactly just:

10.0.0.4
10.0.0.5

I would like to use the IPs listed here in the my Playbook under the variable 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"

I have tried using {{ inventory_hostname }}, but that brings back localhost instead. I have also tried using {{ ansible_host }}, but that brings back 127.0.0.1

Does anyone know how I can use the IPs listed in the inventory in place of the variable as it loops through each deployment?

Just to note, I run the playbook as such:

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

Ansible version is 2.8.0


Solution

  • Turns out I needed to change the hosts to all, and leave {{ ansible_host }} in the ip_address variable field. The IPs get pulled from the inventory file and deploys fine. Like so:

    - 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"