Dynamically assign ansible_fqdn during play

Posted on

I am using GCP as one of my inventory sources, challenge being we don't have fqdn or suffix exposed.

Ansible_host is set to vm instance IP from GCP.

I created a label for domain membership, the value being the domain name.

The second part of the domain label is alwasys '.somedomain'.

I am attemping to use the above values to build a string to set ansible_fqdn dynamically so I can set ansible_host to its fqdn during a play.

Playbook:


  • name: Patching - pre checks
    hosts: "{{ target_hosts | default('all') }}"
    gather_facts: false

    pre_tasks:

    • name: Include environment variables for GCP Windows Domain Members
      include_vars:
      dir: group_vars/gcp
      when:

      • labels.domainmembership is defined
      • '"windows" in labels.operatingsystem'
    • name: Set facts for identified GCP Domain Members
      ansible.builtin.set_fact:
      domainlabel_secondpart: '{{ ".somedomain" }}'
      ansible_fqdn: "{{ hostvars[labels.domainmembership] + domainlabel_secondpart }}"
      ansible_host: "{{ ansible_hostname_full }}"
      cacheable: yes
      when:

      • labels.domainmembership is defined
      • '"windows" in labels.operatingsystem'

I am using GCP as one of my inventory sources, challenge being we don't have fqdn or suffix exposed.

Ansible_host is set to vm instance IP from GCP.

I created a label for domain membership, the value being the domain name.

The second part of the domain label is alwasys '.somedomain'.

I am attemping to use the above values to build a string to set ansible_fqdn dynamically so I can set ansible_host to its fqdn during a play.

Playbook:


  • name: Patching - pre checks
    hosts: "{{ target_hosts | default('all') }}"
    gather_facts: false

    pre_tasks:

    • name: Include environment variables for GCP Windows Domain Members
      include_vars:
      dir: group_vars/gcp
      when:

      • labels.domainmembership is defined
      • '"windows" in labels.operatingsystem'
    • name: Set facts for identified GCP Domain Members
      ansible.builtin.set_fact:
      domainlabel_secondpart: '{{ ".somedomain" }}'
      ansible_fqdn: "{{ hostvars[labels.domainmembership] + domainlabel_secondpart }}"
      ansible_host: "{{ ansible_hostname_full }}"
      cacheable: yes
      when:

      • labels.domainmembership is defined
      • '"windows" in labels.operatingsystem'

Instead of getting the value of hostvars[labels.domainmembership] (which is domain1) to build the string I get the following error:

TASK [Set facts for identified GCP Domain Members] *****************************
fatal: [server1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: \"hostvars['domain1']\" is undefined. \"hostvars['domain1']\" is undefined\n\nThe error appears to be in '/runner/project/pre-checks-test.yml': line 36, 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 - name: Set facts for identified GCP Domain Members\n ^ here\n"}

A host from GCP inventory source:

{
"ansible_host": "10.0.0.2",
"canIpForward": false,
"cpuPlatform": "Intel Cascade Lake",
"creationTimestamp": "2023-04-26T23:55:38.932-07:00",
"deletionProtection": false,
"name": "server1",
"project": "someproject",
"startRestricted": false,
"status": "RUNNING",
"labels": {
"domainmembership": "domain1",
"operatingsystem": "windows"
}
}

How do I dynamically set the ansible_fqdn using the domain value as above?

Responses