- for recording and grouping of hosts
- Dynamic
- where frequently a playbook will create a new cloud resource and then attempt to use it as if it were part of the inventory. This will fail, as the resource was not part of the inventory when the playbook launched. All is not lost though! A special module is provided that allows a playbook to temporarily add inventory to the in-memory inventory object, the add_host module
GCE dynamic inventory
https://docs.ansible.com/ansible/latest/scenario_guides/guide_gce.html. Except, it is slightly incomplete.
ansible.cfg also needs:
[defaults] host_key_checking = False # so as not to prompt for host key confirmation private_key_file = /path/a/private_key # a key that has a public key on the metadata for the project otherwise Ansible can’t connect remote_user = ansible # if the remote host is set to not allow root login [inventory] enable_plugins = gcp_compute [ssh_connection] retries = 6 # to give enough time and attempts to allow the instance to boot up fully
The “Wait for SSH to come up” task needs:
delay: 120 sleep: 10
to allow time for the machine to boot up
The gcp_compute_instance documentation is out-of-date.
network_interfaces.network and network_interfaces.subnetwork are maps not a strings. E.g.
network_interfaces: - network: selfLink: "{{ platform.network }}"
disks.boot is a required field for the first disk. E.g.
disks: - auto_delete: yes boot: yes
The returned object if formatted differently. For example,
… register: prototype_vm - name: Wait for SSH for instance wait_for: delay: 120 host: "{{ prototype_vm.networkInterfaces[0].networkIP }}"
More up-to-date documentation is in the source code: https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/google/gcp_compute_instance.py#L30
Authentication isn’t explained very well. However, reading the source code it is clear that to use the service account assigned to the instance where Ansible will be ran from, the auth_kind needs to be application:
- name: Create an instance hosts: localhost gather_facts: no connection: local vars: auth_kind: application tasks: - include_tasks: tasks/to_create_instance.yml - name: Configure prototype instance hosts: prototype_vms connection: ssh become: True roles: - role_to_configure_vm - name: create prototype instance gcp_compute_instance: auth_kind: "{{ auth_kind }}"