Ansible GCP dynamic Inventory Plugin How to pass Project ID

225 views
Skip to first unread message

A D

unread,
Jul 24, 2022, 4:21:14 AM7/24/22
to Ansible Project
Hi Everyone,

I am using the dynamic inventory file for setting up Ansible for running configurations in GCP
# inventory.compute.gcp.yml
plugin: gcp_compute             # name the plugin you want to use (use `ansible-doc -t inventory -l` to list available plugins)
projects:
  - <gcp_project_id>            # Id of your gcp project
regions:                        # regions from your project you want to fetch inventory from (you can also use zones instead of regions if you target one or several specific zones)        
  - <gcp_project_region>
filters: []
auth_kind: serviceaccount       # gcp authentication kind. with service account you should provide the service account json key file to authenticate
service_account_file: <service_account_file>.json   # Service account json keyfile

How do I set the project_id as a variable?how can I pass the project ID from CLI when executing an ansible playbook? does ansible support jinja templating for inventory file? Please advise

Walter Rowe

unread,
Aug 1, 2022, 8:40:12 AM8/1/22
to Ansible Project
Use templating to expand a variable name.

# inventory.compute.gcp.yml
plugin: gcp_compute             # name the plugin you want to use (use `ansible-doc -t inventory -l` to list available plugins)
projects:
  - "{{ gcp_project_id }}"            # Id of your gcp project
regions:                        # regions from your project you want to fetch inventory from (you can also use zones instead of regions if you target one or several specific zones)        
  - "{{ 
gcp_project_region }}"
filters: []
auth_kind: serviceaccount       # gcp authentication kind. with service account you should provide the service account json key file to authenticate
service_account_file: "{{ service_account_file }}.json"   # Service account json keyfile

Use the -e (extra var) command line option to specify the variable values on the command line.

% ansible-playbook my_gcp_inventory.yml -e "gcp_project_id=XYZ123 gcp_project_region=ABC789 service_account_file=my_gcp_file"

--
Walter Rowe, Chief
Infrastructure Services
Office of Information Systems Management
National Institute of Standards and Technology
United States Department of Commerce
Reply all
Reply to author
Forward
0 new messages