Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Host_vars and group_vars from different sources are not merged together in one inventory #9762

Closed
abcqwertz opened this issue Mar 31, 2021 · 4 comments

Comments

@abcqwertz
Copy link

abcqwertz commented Mar 31, 2021

ISSUE TYPE
  • Bug Report
SUMMARY

I am trying to merge multiple inventory sources together in one inventory. The sync of hosts and groups works fine but variables are not merged.
The host vm01.testing.local should have an additional variable „hostvar2“ which is coming from the project. The variable is not merged to the host, as you can see in the variables I get in the AWX-web-ui:

{
  "ansible_host": "vm01.testing.local",
  "ansible_uuid": "5016c76c-eec9-c883-6cc3-4366034217f2",
  "config": {
    "instanceUuid": "5016c76c-eec9-c883-6cc3-4366034217f2",
    "name": "vm01.testing.local"
  },
  "config.instanceUuid": "5016c76c-eec9-c883-6cc3-4366034217f2",
  "config.name": "vm01.testing.local",
  "configIssue": [],
  "configStatus": "green",
  "configissue": [],
  "configstatus": "green",
  "name": "vm01.testing.local",
  "runtime": {
    "connectionState": "connected"
  },
  "runtime.connectionState": "connected"
}

Here are my vCenter source variables:

---
plugin: community.vmware.vmware_vm_inventory
strict: false
validate_certs: false
with_tags: false
with_nested_properties: true
compose:
  ansible_host: config.name
  ansible_ssh_host: guest.ipAddress
  ansible_uuid: config.instanceUuid
  configissue: configIssue
  configstatus: configStatus
filters:
  - runtime.powerState == "poweredOn"
  - config.name is match('vm01\.testing\.local')
hostnames:
  - 'config.name'
groups:
  testing: config.name is match('vm01\.testing\.local')
properties:
  - 'config.name'
  - 'guest.ipAddress'
  - 'config.instanceUuid'
  - 'configIssue'
  - 'configStatus'
  - 'name'

Here is the project (git repo) layout:

  • hosts (Hostfile in INI format):
[backup_test]
tbackup.testing.local
  • host_vars/tbackup.testing.local
---
hostvar1: "This is a hostvar"
  • host_vars/vm01.testing.local
---
hostvar2: "This is a host var for a virtual machine"
  • group_vars/testing
---
groupvar: "This is a groupvar"
ENVIRONMENT
  • AWX version: 17.1.0
  • AWX install method: docker on linux
  • Ansible version: 2.9.18
  • Operating System: openSUSE LEAP 15.2
  • Web Browser: Firefox latest version
STEPS TO REPRODUCE

Create an inventory with two sources:
- First source syncs virtual machines from VMware vCenter (for source variables see summary above)
- Second source syncs hosts from a hosts file in a (Git) project (for repo setup see summary above)
- Project source also contains host_vars/group_vars for virtual machines and groups created by the first source
- Sync both sources

EXPECTED RESULTS

Host and group variables from the project source should be merged with hosts/groups created by vCenter source

ACTUAL RESULTS

Additional custom host_vars and group_vars from a project are not merged to hosts/groups created by vCenter source
It doesn’t matter if the project source has „overwrite variables“ enabled or not.

ADDITIONAL INFORMATION

I put the vCenter source variables in a file and ran the ansible-inventory command manually in the awx_task container:

ansible-inventory --list --export -i /var/lib/awx/projects/_6__awx_vmware/testing.vmware.yml  -i /var/lib/awx/projects/_19__awx_inventory/hosts --output /tmp/output.json

If i now look at output.json, I get the exptected result (As you can see the variables for the testing group and vm01.testing.local are merged successfully to the hosts and groups created by vCenter import):

{
    "_meta": {
        "hostvars": {
            "tbackup.testing.local": {
                "hostvar1": "This is a hostvar"
            }, 
            "vm01.testing.local": {
                "ansible_host": "vm01.testing.local",
                "ansible_uuid": "5016c76c-eec9-c883-6cc3-4366034217f2",
                "config": {
                    "instanceUuid": "5016c76c-eec9-c883-6cc3-4366034217f2",
                    "name": "vm01.testing.local"
                },
                "config.instanceUuid": "5016c76c-eec9-c883-6cc3-4366034217f2",
                "config.name": "vm01.testing.local",
                "configIssue": [],
                "configStatus": "green",
                "configissue": [],
                "configstatus": "green",
                "hostvar2": "This is a host var for a virtual machine",
                "name": "vm01.testing.local",
                "runtime": {
                    "connectionState": "connected"
                },
                "runtime.connectionState": "connected"
            }
        }
    },
    "all": {
        "children": [
            "backup_test",
            "testing",
            "ungrouped"
        ],
        "vars": {
            "groupvar_all": "This is a groupvar for all groups"
        }
    },
    "backup_test": {
        "hosts": [
            "tbackup.testing.local"
        ]
    },
    "testing": {
        "hosts": [
            "vm01.testing.local"
        ],
        "vars": {
            "groupvar": "This is a groupvar"
        }
    }
}

Any help is much appreciated

@ryanpetrello
Copy link
Contributor

@AlanCoding any thoughts on this one?

@abcqwertz
Copy link
Author

abcqwertz commented May 12, 2021

Hi @ryanpetrello @AlanCoding,
sorry for the late reply. I recently set up a bare-metal kubernetes cluster to install a newer version of AWX:

ENVIRONMENT

  • AWX version: 19.0.0
  • AWX install method: kubernetes 1.21 with ingress-nginx and metalLB
  • Ansible version: 2.9.18 (I guess?)
  • Operating System: Master and worker-Nodes on openSUSE LEAP 15.2
  • Web Browser: Firefox latest version on MacOS 10.15.17 Catalina

Unforetunatly the variables are still not combined in the hosts/groups. I used the same configuration as mentioned above.

There should be an additional variable "hostvar2": "This is a host var for a virtual machine"

Here is the variable data from the host vm01.testing.local (via the API):

HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
X-API-Node: awx-b5f6cf4d4-rt6qk
X-API-Product-Name: AWX
X-API-Product-Version: 19.0.0
X-API-Time: 0.017s

{
    "ansible_host": "vm01.testing.local",
    "ansible_uuid": "5016bbd9-8ba0-c9ce-4549-5dd6f6e545ed",
    "config": {
        "instanceUuid": "5016bbd9-8ba0-c9ce-4549-5dd6f6e545ed",
        "name": "vm01.testing.local"
    },
    "config.instanceUuid": "5016bbd9-8ba0-c9ce-4549-5dd6f6e545ed",
    "config.name": "vm01.testing.local",
    "configIssue": [],
    "configStatus": "green",
    "configissue": [],
    "configstatus": "green",
    "name": "vm01.testing.local",
    "runtime": {
        "connectionState": "connected"
    },
    "runtime.connectionState": "connected"
}

@AlanCoding
Copy link
Member

Second source syncs hosts from a hosts file in a (Git) project (for repo setup see summary above)

You say that your second source that got updated was your file source.

Additional custom host_vars and group_vars from a project are not merged to hosts/groups created by vCenter source
It doesn’t matter if the project source has „overwrite variables“ enabled or not.

But you checked overwrite_vars for your first source.

It doesn't matter if the first source has set to overwrite vars or not. It's the 2nd import that will be doing the overwriting. Can you check to make extra sure that the SCM-based inventory source which imports from a file has "overwrite variables" set to False?

We have a weird behavior where SCM inventory sources will default this field to True. Maybe it shouldn't be that way. This is my best guess as to the thing that's messing you up. I created a test case here:

ansible/test-playbooks#185

That combines variables (does not overwrite) when both files are added as SCM inventory sources. But it only does this if I specifically set overwrite variables to False for them.

@AlanCoding
Copy link
Member

I cannot reproduce, so closing for now. If you have any new information I'll adjust and re-run my test case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants