Ansible match inventory groups in role with dynamic inventory in GCP

Viewed 44

I have a dynamic inventory in GCP for app that runs on multiple VMs. However I cannot seem to match any hosts with my playbook that calls a role.

The dynamic inventory creates groups with this naming convention, based on GCP labels and gpc.yml dynamic inventory file:

demo4_costing_db
demo4_costing_app

I can interrogate the entire inventory with ansible-inventory --list -i gcp.yml

Example output from the list command:

"demo4_costing_db": {
        "hosts": [
            "10.10.60.194",
            "10.10.60.195",
            "10.10.60.196",
            "10.10.60.197",
            "10.10.60.198"
        ]

and so on, for various server functions in the app.

I then have a role that needs to do various disparate tasks to the different inventory groups.

The tasks/main.yml in the role looks like this:

- name: "Install packages"
  ansible.builtin.dosomething
    action
  when: "'costing_db' in group_names"

- name: "Install Python packages"
  ansible.builtin.dosomethingelse
    different_action
  when: "'costing_app' in group_names"

the role is invoked with a playbook like this: ansible-playbook deploy.yml -i ../inventory/gcp-dynamic/demo4/gcp.yml --extra-vars "targets=demo4" -u ansible --key-file ~/ansible.pem --vault-password-file ~/.ansible/vault_pass.txt

The deployment playbook looks like this:

- hosts: all
  become: true
  become_user: root 
  
  roles:
   -  ../roles/postgres

Why does my playbook fail with no hosts matched? From other examples of the when: conditional, I should be able to string-match on group names like that.

My inventory looks soemthing like this: ansible-inventory --list -i gcp.yml

  "all": {
        "children": [
            "demo4_component_admin",
            "demo4_component_artemis",
            "demo4_component_batch",
            "demo4_component_discovery",
            "demo4_component_elastic",
            "demo4_component_gateway",
            "demo4_component_inbox",
            "demo4_component_tools",
            "demo4_component_transfer",
            "demo4_costing_app",
            "demo4_costing_db",
            "demo4_inventory_demo4",
            "demo4_schema_artemisdb",
            "demo4_schema_batchdb",
            "demo4_schema_gatewaydb",
            "demo4_schema_inboxdb",
            "demo4_schema_transferdb",
            "ungrouped"
        ]
    }
0 Answers
Related