Search code examples
ansiblestdoutansible-inventory

Ansible dynamic hosts skips


I am trying to set up a very hacky vm deployment Environment utilizing ansible 1.9.4 for my homeoffice.

I am pretty far, but the last step just won't work. I have a written 10loc plugin which generates temporary dns names for the staging vlan and I want to pass that value as a host to the next playbook role.


    TASK: [localhost-eval | debug msg="{{ hostvars['127.0.0.1'].dns_record.stdout_lines }}"] ***
    ok: [127.0.0.1] => {
        "msg": "['vm103.local', 'vm-tmp103.local']"
    }

It's accessible in the global playbook scope via the hostvars:

{{ hostvars['127.0.0.1'].dns_record.stdout_lines[1] }}

and should be passed on to:


    - name: configure vm template
      hosts: "{{ hostvars['127.0.0.1'].dns_record.stdout_lines[1] }}"
      gather_facts: no
      roles:
        - template-vm-configure

which results in:

PLAY [configure vm template] **************************************************
skipping: no hosts matched

my inventory looks like this and seems to work. Hardcoding 'vm-tmp103.local' gets the role running.

[vm]
vm[001:200].local
[vm-tmp]
vm-tmp[001:200].local

Thank you in advance, hopefully someone can point me in the right direction. Plan B would consists of passing the dns-records to a bash script for finalizing configuration, since I just want to configure the Network interface before adding the vm to the monitoring.

Edit: Modified a play to use add hosts and add them to a temp group - add_host: name={{ hostvars['127.0.0.1'].dns_record.stdout_lines[1] }} groups=just_vm

but it still doesn't match.


Solution

  • That's not intended behaviour I think. #9733 lead me to a solution.

    Adding this task let's me use staging as a group.

      tasks:
        - set_fact: hostname_next="{{ hostvars['127.0.0.1'].dns_record.stdout_lines[1] }}"
        - add_host: group=staging name='{{ hostname_next }}'