0

Problem: referencing a fact about a host ( in this case, the private ip ) from another host in a playbook using a wildcard only seems to work in the "Host" part of a playbook, not inside a task. vm_ubuntu* cannot be used in a task.

In a single playbook, I have a couple of hosts, and because the inventory is dynamic, I don't have the hostname ahead of time as Azure appends an identifier after it has been created. I am using TF to create. And using the Azure dynamic inventory method. I am calling my playbook like this, where myazure_rm.yml is a bog standard azure dynamic inventory method, as of the time of this writing.

ansible-playbook -i ./myazure_rm.yml ./bwaf-playbook.yaml --key-file ~/.ssh/id_rsa --u azureuser

My playbook looks like this ( abbreviated ).

- hosts: vm_ubuntu*

  tasks:
    - name: housekeeping
      set_fact:
        vm_ubuntu_private_ip="{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}"
#"
    - debug: var=vm_ubuntu_private_ip

- hosts: vm_bwaf*
  connection: local

  vars:
    vm_bwaf_private_ip: "{{private_ipv4_addresses | join }}"
    vm_bwaf_public_ip: "{{ public_ipv4_addresses | join }}"
    vm_ubuntu_private_ip: "{{ hostvars['vm_ubuntu*']['ip'] }}"
    api_url: "http://{{ vm_bwaf_public_ip }}:8000/restapi/{{ api_version }}"
#"
bwolmarans
  • 31
  • 3

1 Answers1

0

I am answering my own question to get rep, and to to help others of course. I also want to thank the person ( https://stackoverflow.com/users/4281353/mon ) who came up with this first, which appears in here: How do I set register a variable to persist between plays in ansible?

- name: "Save private ip to dummy host" 
  add_host: 
     name:   "dummy_host" 
     ip:     "{{ vm_ubuntu_private_ip }}"

And then this can be referenced in the other host in the playbook like this:

- hosts: vm_bwaf*
  connection: local

  vars:
    vm_bwaf_private_ip: "{{private_ipv4_addresses | join }}"
    vm_bwaf_public_ip: "{{ public_ipv4_addresses | join }}"
    vm_ubuntu_private_ip: "{{ hostvars['dummy_host']['ip'] }}"
bwolmarans
  • 31
  • 3
  • I'm so glad you asked ( even though you are wrong/misinformed, which you can go and check for yourself, prehaps by first reading the other question to which you are referring before posting on my question) it's because the existing answer was for a different question as are many things. Is there something you wish to contribute to the community, or what is it that you are intending to do here? – bwolmarans Feb 09 '20 at 04:51