Skip to content

Commit

Permalink
state: fix ansible limit, set custom terrible_path, set custom state …
Browse files Browse the repository at this point in the history
…path
  • Loading branch information
89luca89 committed Jun 26, 2021
1 parent 0ee1c80 commit ad4a6b0
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 15 deletions.
5 changes: 5 additions & 0 deletions main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@
- hosts: deploy
gather_facts: False
max_fail_percentage: 0

pre_tasks:
- set_fact:
full_run: '{{play_hosts == groups.deploy}}'

roles:
- terrible
5 changes: 3 additions & 2 deletions roles/terrible/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ terraform_version: 0.12

# In case a bastion is specified, we should provide a jump host
# also to ansible to access the VMs via the bastion host.
ansible_ssh_common_args: '-o StrictHostKeyChecking=no {% if ansible_jump_hosts is defined and ansible_jump_hosts is not none %} -o ProxyCommand="ssh -W %h:%p -q {% for bastion in ansible_jump_hosts %}{{ bastion.user }}@{{ bastion.host }} -p {{ bastion.port }} {% if not loop.last %} -o ProxyCommand={% for quote in range(loop.index-1) %}\\{% endfor %}\"ssh -W {{ bastion.host }}:{{ bastion.port }} -q {% endif %} {% endfor %} {% for x in ansible_jump_hosts %}{% for quote in range(loop.revindex)%}{% for q in range(quote) %}\{% endfor %}{% endfor %}"{% endfor %}{% endif %}'
ansible_ssh_common_args: '-o StrictHostKeyChecking=no {% if ansible_jump_hosts is defined and ansible_jump_hosts is not none %} -o ProxyCommand="ssh -W %h:%p -o StrictHostKeyChecking=no -q {% for bastion in ansible_jump_hosts %}{{ bastion.user }}@{{ bastion.host }} -p {{ bastion.port }} {% if not loop.last %} -o ProxyCommand={% for quote in range(loop.index-1) %}\\{% endfor %}\"ssh -W {{ bastion.host }}:{{ bastion.port }} -q {% endif %} {% endfor %} {% for x in ansible_jump_hosts %}{% for quote in range(loop.revindex)%}{% for q in range(quote) %}\{% endfor %}{% endfor %}"{% endfor %}{% endif %}'

##
hcl_deploy_path: "~/.ansible-terraform-kvm/{{ ansible_inventory_sources[0].split('/')[-1] }}/{{ provider_uri | replace('+', '_') | replace(':', '_') | replace('/', '_') }}"
base_deploy_path: "{{ lookup('env','HOME') }}"
state_save_file: "{{ ansible_inventory_sources[0] }}-state.tar.gz"

os_family_support:
- RedHat
Expand Down
7 changes: 5 additions & 2 deletions roles/terrible/tasks/deploy_vm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

- name: "Find hypervisors file tree"
find:
path: "{{ lookup('env','HOME') }}/.terrible/{{ ansible_inventory_sources[0].split('/')[-1] }}"
path: "{{ terrible_path }}"
file_type: directory
register: hypervisors_tf
tags: deploy, destroy, apply, generate_hcl, purge
Expand All @@ -51,6 +51,7 @@
run_once: yes
with_items:
- "{{ play_hosts }}"
when: full_run
delegate_to: terraform_node

# Create an array containing the list of files representing the nodes in the folder.
Expand All @@ -59,6 +60,7 @@
paths: "{{ hcl_deploy_path }}/"
patterns: "*.tf"
register: nodes_tf
when: full_run
tags: deploy, generate_hcl
delegate_to: terraform_node

Expand All @@ -72,8 +74,9 @@
with_items:
- "{{ nodes_tf.files | sort(attribute='path') }}"
when:
- not "variables.tf" in item.path
- full_run
- item.path not in managed_files
- not "variables.tf" in item.path
tags: deploy, generate_hcl
delegate_to: terraform_node

Expand Down
10 changes: 8 additions & 2 deletions roles/terrible/tasks/setup_vars.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
---

# Setup tf files folder
- name: Setup - set terrible file path
set_fact:
terrible_path: "{{ base_deploy_path }}/.terrible/{{ ansible_inventory_sources[0].split('/')[-1] }}"
delegate_to: terraform_node

# local ansible, terraform and kvm
# just use qemu:///system
- name: Setup - local ansible, local terraform, local kvm
Expand Down Expand Up @@ -77,12 +83,12 @@

- name: Set HCL Deploy path
set_fact:
hcl_deploy_path: "{{ lookup('env','HOME') }}/.terrible/{{ ansible_inventory_sources[0].split('/')[-1] }}/{{ provider_uri.split('/')[-2] }}"
hcl_deploy_path: "{{ terrible_path }}/{{ provider_uri.split('/')[-2] }}"
delegate_to: terraform_node

- name: Set HCL Deploy path - (localhost workaround)
set_fact:
hcl_deploy_path: "{{ lookup('env','HOME') }}/.terrible/{{ ansible_inventory_sources[0].split('/')[-1] }}/localhost"
hcl_deploy_path: "{{ terrible_path }}/localhost"
when: provider_uri.split('/')[-2] | length == 0
delegate_to: terraform_node

Expand Down
10 changes: 5 additions & 5 deletions roles/terrible/tasks/state_restore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

- name: Check that a state file exists for current inventory
stat:
path: "{{ ansible_inventory_sources[0] }}-state.tar.gz"
path: "{{ state_save_file }}"
register: inventory_status
delegate_to: localhost

- name: Ensure destination folder exists
file:
path: "{{ ansible_inventory_sources[0] | dirname }}"
path: "{{ state_save_file | dirname }}"
state: directory
recurse: yes
mode: 0755
Expand All @@ -26,8 +26,8 @@
# before unpacking it
- name: Send status to terraform_node
copy:
src: "{{ ansible_inventory_sources[0] }}-state.tar.gz"
dest: "{{ ansible_inventory_sources[0] }}-state.tar.gz"
src: "{{ state_save_file }}"
dest: "{{ state_save_file }}"
mode: 0644
run_once: yes
when:
Expand All @@ -37,7 +37,7 @@

- name: Unpack saved state to "{{ hcl_deploy_path | dirname }}"
unarchive:
src: "{{ ansible_inventory_sources[0] }}-state.tar.gz"
src: "{{ state_save_file }}"
dest: "{{ hcl_deploy_path | dirname }}"
creates: "{{ hcl_deploy_path }}/.terraform"
remote_src: yes
Expand Down
8 changes: 4 additions & 4 deletions roles/terrible/tasks/state_save.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
- name: Create archive # noqa 301
shell: |
cd {{ hcl_deploy_path | dirname }}
mkdir -p $(dirname {{ ansible_inventory_sources[0] }})
tar cfv {{ ansible_inventory_sources[0] }}-state.tar.gz ./
mkdir -p $(dirname {{ state_save_file }})
tar cfv {{ state_save_file }} ./
changed_when: no
run_once: yes
delegate_to: terraform_node
Expand All @@ -17,8 +17,8 @@
# local
- name: Fetch status from terraform_node
fetch:
src: "{{ ansible_inventory_sources[0] }}-state.tar.gz"
dest: "{{ ansible_inventory_sources[0] | dirname }}/"
src: "{{ state_save_file }}"
dest: "{{ state_save_file | dirname }}/"
mode: 0644
flat: yes
validate_checksum: yes
Expand Down

0 comments on commit ad4a6b0

Please sign in to comment.