glux
glux

Reputation: 532

Ansible - GCP Dynamic inventory & tags

I'm just getting started with ansible and have successfully been able to configure ansible to get dynamic inventory from GCP.

I am able to successfully run the ping module against all instances:

ansible -i ~/git/ansible/inventory all -m ping

I am also able to successfully run the ping module against a single instance based on hostname:

ansible -i ~/git/ansible/inventory instance-2 -m ping

I would now like to utilize tags to group instances. For example, I have set of instances that are labeled 'env:dev' https://www.evernote.com/l/AfcLWLkermxMyIK7GvGpQXjXdIDFVAiT_z0

I have attempted multiple variations of the command below with no luck

ansible -i ~/git/ansible/inventory tag_env:dev -m ping

How can I filter and group my dynamic inventory on GCP?

Upvotes: 2

Views: 2455

Answers (1)

marc345
marc345

Reputation: 26

So you need to add network tag in instance settings not labels i don't know why but gce.py doesn't return GCP labels so you can only use network tags wich is limited (i mean not key=value but just value) For example add network tag just 'dev' and then run ansible -i ~/git/ansible/inventory tag_dev -m ping also if you need to filter by few tags only way i found it's

- name: test stuff   
  hosts: tag_api:&tag_{{ environment }}   
  var_files:
    vars/{{ environment }}
    vars/api

  tasks:
    - name: test
      command: echo "test"

run playbook like this ansible-playbook -i inventory/ -u user playbook/test.yml -e environment=dev

maybe someone know better way, with aws ec2.py i could filter in ec2.ini config but gce.py very limited also i noticed that sometimes you need to clear cache gce.py --refresh-cache

Upvotes: 1

Related Questions