Skip to content
This repository has been archived by the owner on Jan 5, 2023. It is now read-only.

Commit

Permalink
use FQCN and expose the required collection (#25)
Browse files Browse the repository at this point in the history
- Use FQCN for every module as recommended by Ansible
- Add collection dependency for openssh_keypair (community.crypto)
  • Loading branch information
nbttmbrg authored Sep 14, 2021
1 parent e4e8af2 commit 296760b
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 22 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ provisioning test resources.

Please note that this driver is currently in its early stage of development.

This plugin requires google.cloud and community.crypto collections to be present:
```
ansible-galaxy collection install google.cloud
ansible-galaxy collection install community.crypto
```

# Installation and Usage

Install molecule-gce :
Expand Down
2 changes: 1 addition & 1 deletion molecule/default/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
gather_facts: true
tasks:
- name: Display info
debug:
ansible.builtin.debug:
msg: >
{{ ansible_distribution }}-{{ ansible_distribution_major_version }}-{{ ansible_architecture }}
2 changes: 1 addition & 1 deletion molecule/default/prepare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
gather_facts: false
tasks:
- name: Wait 600 seconds for target connection to become reachable/usable
wait_for_connection:
ansible.builtin.wait_for_connection:
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
hosts: all
tasks:
- name: "Include {{ cookiecutter.role_name }}"
include_role:
ansible.builtin.include_role:
name: "{{ cookiecutter.role_name }}"
2 changes: 1 addition & 1 deletion src/molecule_gce/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,4 @@ def template_dir(self):
@property
def required_collections(self) -> Dict[str, str]:
# https://galaxy.ansible.com/google/cloud
return {"google.cloud": "1.0.2"}
return {"google.cloud": "1.0.2", "community.crypto": "1.8.0"}
8 changes: 4 additions & 4 deletions src/molecule_gce/playbooks/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@

tasks:
- name: Make sure if linux or windows either specified
assert:
ansible.builtin.assert:
that:
- molecule_yml.driver.instance_os_type | lower == "linux" or
molecule_yml.driver.instance_os_type | lower == "windows"
fail_msg: "instance_os_type is possible only to specify linux or windows either"

- name: Include create_linux_instance tasks
include_tasks: tasks/create_linux_instance.yml
ansible.builtin.include_tasks: tasks/create_linux_instance.yml
when:
- molecule_yml.driver.instance_os_type | lower == "linux"

- name: Include create_windows_instance tasks
include_tasks: tasks/create_windows_instance.yml
ansible.builtin.include_tasks: tasks/create_windows_instance.yml
when:
- molecule_yml.driver.instance_os_type | lower == "windows"

handlers:
- name: Import main handler tasks
import_tasks: handlers/main.yml
ansible.builtin.import_tasks: handlers/main.yml
4 changes: 2 additions & 2 deletions src/molecule_gce/playbooks/destroy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
- "Dump instance config"

- name: Wait for instance(s) deletion to complete
async_status:
ansible.builtin.async_status:
jid: "{{ item.ansible_job_id }}"
register: server
until: server.finished
Expand All @@ -35,4 +35,4 @@

handlers:
- name: Import main handler tasks
import_tasks: handlers/main.yml
ansible.builtin.import_tasks: handlers/main.yml
10 changes: 5 additions & 5 deletions src/molecule_gce/playbooks/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name: Populate instance config dict Linux
set_fact:
ansible.builtin.set_fact:
instance_conf_dict: {
'instance': "{{ instance_info.name }}",
'address': "{{ instance_info.networkInterfaces.0.accessConfigs.0.natIP if molecule_yml.driver.external_access else instance_info.networkInterfaces.0.networkIP }}",
Expand All @@ -16,7 +16,7 @@
register: instance_conf_dict

- name: Populate instance config dict Windows
set_fact:
ansible.builtin.set_fact:
instance_conf_dict: {
'instance': "{{ instance_info.name }}",
'address': "{{ instance_info.networkInterfaces.0.accessConfigs.0.natIP if molecule_yml.driver.external_access else instance_info.networkInterfaces.0.networkIP }}",
Expand All @@ -35,15 +35,15 @@


- name: Wipe out instance config
set_fact:
ansible.builtin.set_fact:
instance_conf: {}

- name: Convert instance config dict to a list
set_fact:
ansible.builtin.set_fact:
instance_conf: "{{ instance_conf_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}"

- name: Dump instance config
copy:
ansible.builtin.copy:
content: "{{ instance_conf }}"
dest: "{{ molecule_instance_config }}"
mode: '0600'
6 changes: 3 additions & 3 deletions src/molecule_gce/playbooks/tasks/create_linux_instance.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name: create ssh keypair
openssh_keypair:
community.crypto.openssh_keypair:
comment: "{{ lookup('env','USER') }} user for Molecule"
path: "{{ ssh_identity_file }}"
register: keypair
Expand Down Expand Up @@ -42,7 +42,7 @@
poll: 0

- name: Wait for instance(s) creation to complete
async_status:
ansible.builtin.async_status:
jid: "{{ item.ansible_job_id }}"
loop: "{{ async_results.results }}"
register: server
Expand All @@ -55,7 +55,7 @@
- "Dump instance config"

- name: Wait for SSH
wait_for:
ansible.builtin.wait_for:
port: 22
host: "{{ item.networkInterfaces.0.accessConfigs.0.natIP if molecule_yml.driver.external_access else item.networkInterfaces.0.networkIP }}"
search_regex: SSH
Expand Down
8 changes: 4 additions & 4 deletions src/molecule_gce/playbooks/tasks/create_windows_instance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
poll: 0

- name: Wait for instance(s) creation to complete
async_status:
ansible.builtin.async_status:
jid: "{{ item.ansible_job_id }}"
loop: "{{ async_results.results }}"
register: server
Expand All @@ -45,14 +45,14 @@
- "Dump instance config"

- name: Wait for WinRM
wait_for:
ansible.builtin.wait_for:
port: 5986
host: "{{ item.networkInterfaces.0.accessConfigs.0.natIP if molecule_yml.driver.external_access else item.networkInterfaces.0.networkIP }}"
delay: 10
loop: "{{ server.results }}"

- name: Prepare Windows User
script: ./files/windows_auth.py --instance {{ item.name }} --zone {{ item.zone | default(molecule_yml.driver.region + '-b') }} --project {{ gcp_project_id }} --username molecule_usr
ansible.builtin.script: ./files/windows_auth.py --instance {{ item.name }} --zone {{ item.zone | default(molecule_yml.driver.region + '-b') }} --project {{ gcp_project_id }} --username molecule_usr
args:
executable: python3
environment:
Expand All @@ -66,7 +66,7 @@
delay: 10

- name: Add password for instances in server list
set_fact:
ansible.builtin.set_fact:
win_instances: "{{ win_instances|default([]) + [dict(item[0], password=item[1].stdout_lines |last)] }}"
loop: "{{ server.results | zip(password.results) | list }}"
no_log: true

0 comments on commit 296760b

Please sign in to comment.