From f0c2c9eb5ca2191720b53e051cf11b928c4f3388 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Cor=C3=A9?= Date: Thu, 15 Oct 2020 16:31:09 +0200 Subject: [PATCH 01/16] Allow dynamic workloads in agnosticd (WIP) --- ansible/infra_common.yml | 18 +++++++++++++++ ansible/main.yml | 38 ++++++++++++++++++++++++++++++++ ansible/post_infra_common.yml | 22 ++++++++++++++++++ ansible/post_software_common.yml | 22 ++++++++++++++++++ ansible/pre_infra_common.yml | 18 +++++++++++++++ ansible/pre_software_common.yml | 22 ++++++++++++++++++ ansible/software_common.yml | 22 ++++++++++++++++++ 7 files changed, 162 insertions(+) create mode 100644 ansible/infra_common.yml create mode 100644 ansible/post_infra_common.yml create mode 100644 ansible/post_software_common.yml create mode 100644 ansible/pre_infra_common.yml create mode 100644 ansible/pre_software_common.yml create mode 100644 ansible/software_common.yml diff --git a/ansible/infra_common.yml b/ansible/infra_common.yml new file mode 100644 index 00000000000..7c5f6844af8 --- /dev/null +++ b/ansible/infra_common.yml @@ -0,0 +1,18 @@ +--- +- name: Install Infra dynamic workloads + hosts: localhost + gather_facts: false + tags: + - dynamic_workloads + tasks: + - name: Process workloads + loop: "{{ agnosticd_infra_workloads | default([]) }}" + loop_control: + loop_var: _workload + label: "{{ _workload.name }}" + include_role: + name: "{{ _workload.name }}" + defaults_from: "{{ _workload.defaults_from | default('main') }}" + handlers_from: "{{ _workload.defaults_from | default('main') }}" + tasks_from: "{{ _workload.tasks_from | default('main') }}" + vars_from: "{{ _workload.vars_from | default('main') }}" diff --git a/ansible/main.yml b/ansible/main.yml index 2a92855703a..1f98c495dc4 100644 --- a/ansible/main.yml +++ b/ansible/main.yml @@ -24,6 +24,12 @@ - step000 - pre_infra_tasks +- name: Common Pre Infra + import_playbook: pre_infra_common.yml + tags: + - step000 + - pre_infra_tasks + ################################################################################# ################################################################################# ############ Step 001 Deploy Infrastructure @@ -43,6 +49,12 @@ - step001 - deploy_infrastructure +- name: Common Infra + import_playbook: infra_common.yml + tags: + - step001 + - deploy_infrastructure + ################################################################################ ################################################################################ ########### Step 002 Post Infrastructure Deploy Tasks @@ -55,6 +67,13 @@ - post_infra - post_infra_tasks +- name: Common Post Infra + import_playbook: post_infra_common.yml + tags: + - step002 + - post_infra + - post_infra_tasks + - import_playbook: "export_inventory.yml" vars: stage: post_infra @@ -76,6 +95,13 @@ - pre_software - pre_software_tasks +- name: Common Pre Software + import_playbook: pre_software_common.yml + tags: + - step003 + - pre_software + - pre_software_tasks + ################################################################################## ################################################################################## ############ Step 004 Software Deploy Tasks @@ -94,6 +120,12 @@ - step004 - deploy_software +- name: Common Software + import_playbook: software_common.yml + tags: + - step004 + - deploy_software + ################################################################################ ################################################################################ ############ Step 005 Post Software Deploy Tasks @@ -106,6 +138,12 @@ - post_software - post_software_tasks +- import_playbook: post_software_common.yml + tags: + - step005 + - post_software + - post_software_tasks + - import_playbook: "export_inventory.yml" vars: stage: post_software diff --git a/ansible/post_infra_common.yml b/ansible/post_infra_common.yml new file mode 100644 index 00000000000..5e6700964f2 --- /dev/null +++ b/ansible/post_infra_common.yml @@ -0,0 +1,22 @@ +--- +- name: Install Post Infra dynamic workloads + hosts: all + gather_facts: false + tags: + - dynamic_workloads + tasks: + - name: Process workloads + loop: "{{ agnosticd_pre_infra_workloads | default([]) }}" + loop_control: + loop_var: _workload + label: "{{ _workload.name }}" + include_role: + name: "{{ _workload.name }}" + defaults_from: "{{ _workload.defaults_from | default('main') }}" + handlers_from: "{{ _workload.defaults_from | default('main') }}" + tasks_from: "{{ _workload.tasks_from | default('main') }}" + vars_from: "{{ _workload.vars_from | default('main') }}" + when: >- + _workload.group | default('') in group_names ) + or + _workload.groups | default([]) | intersect(group_names) | list | length > 0 diff --git a/ansible/post_software_common.yml b/ansible/post_software_common.yml new file mode 100644 index 00000000000..3d11aae73a8 --- /dev/null +++ b/ansible/post_software_common.yml @@ -0,0 +1,22 @@ +--- +- name: Install Post Software dynamic workloads + hosts: all + gather_facts: false + tags: + - dynamic_workloads + tasks: + - name: Process workloads + loop: "{{ agnosticd_post_software_workloads | default([]) }}" + loop_control: + loop_var: _workload + label: "{{ _workload.name }}" + include_role: + name: "{{ _workload.name }}" + defaults_from: "{{ _workload.defaults_from | default('main') }}" + handlers_from: "{{ _workload.defaults_from | default('main') }}" + tasks_from: "{{ _workload.tasks_from | default('main') }}" + vars_from: "{{ _workload.vars_from | default('main') }}" + when: >- + _workload.group | default('') in group_names ) + or + _workload.groups | default([]) | intersect(group_names) | list | length > 0 diff --git a/ansible/pre_infra_common.yml b/ansible/pre_infra_common.yml new file mode 100644 index 00000000000..9bc56c38afa --- /dev/null +++ b/ansible/pre_infra_common.yml @@ -0,0 +1,18 @@ +--- +- name: Install Pre Infra dynamic workloads + hosts: localhost + gather_facts: false + tags: + - dynamic_workloads + tasks: + - name: Process workloads + loop: "{{ agnosticd_pre_infra_workloads | default([]) }}" + loop_control: + loop_var: _workload + label: "{{ _workload.name }}" + include_role: + name: "{{ _workload.name }}" + defaults_from: "{{ _workload.defaults_from | default('main') }}" + handlers_from: "{{ _workload.defaults_from | default('main') }}" + tasks_from: "{{ _workload.tasks_from | default('main') }}" + vars_from: "{{ _workload.vars_from | default('main') }}" diff --git a/ansible/pre_software_common.yml b/ansible/pre_software_common.yml new file mode 100644 index 00000000000..5cd4ac14b75 --- /dev/null +++ b/ansible/pre_software_common.yml @@ -0,0 +1,22 @@ +--- +- name: Install Pre Software dynamic workloads + hosts: all + gather_facts: false + tags: + - dynamic_workloads + tasks: + - name: Process workloads + loop: "{{ agnosticd_pre_software_workloads | default([]) }}" + loop_control: + loop_var: _workload + label: "{{ _workload.name }}" + include_role: + name: "{{ _workload.name }}" + defaults_from: "{{ _workload.defaults_from | default('main') }}" + handlers_from: "{{ _workload.defaults_from | default('main') }}" + tasks_from: "{{ _workload.tasks_from | default('main') }}" + vars_from: "{{ _workload.vars_from | default('main') }}" + when: >- + _workload.group | default('') in group_names ) + or + _workload.groups | default([]) | intersect(group_names) | list | length > 0 diff --git a/ansible/software_common.yml b/ansible/software_common.yml new file mode 100644 index 00000000000..bd92d8fd669 --- /dev/null +++ b/ansible/software_common.yml @@ -0,0 +1,22 @@ +--- +- name: Install Software dynamic workloads + hosts: all + gather_facts: false + tags: + - dynamic_workloads + tasks: + - name: Process workloads + loop: "{{ agnosticd_software_workloads | default([]) }}" + loop_control: + loop_var: _workload + label: "{{ _workload.name }}" + include_role: + name: "{{ _workload.name }}" + defaults_from: "{{ _workload.defaults_from | default('main') }}" + handlers_from: "{{ _workload.defaults_from | default('main') }}" + tasks_from: "{{ _workload.tasks_from | default('main') }}" + vars_from: "{{ _workload.vars_from | default('main') }}" + when: >- + _workload.group | default('') in group_names ) + or + _workload.groups | default([]) | intersect(group_names) | list | length > 0 From aed2dc79b9f08b32bbf7f27d20d162c36a199e2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Cor=C3=A9?= Date: Thu, 15 Oct 2020 17:09:32 +0200 Subject: [PATCH 02/16] Use 'roles' (ansiblish) not 'workloads' --- ansible/infra_common.yml | 22 +++++++++++----------- ansible/post_infra_common.yml | 26 +++++++++++++------------- ansible/post_software_common.yml | 26 +++++++++++++------------- ansible/pre_infra_common.yml | 22 +++++++++++----------- ansible/pre_software_common.yml | 26 +++++++++++++------------- ansible/software_common.yml | 26 +++++++++++++------------- 6 files changed, 74 insertions(+), 74 deletions(-) diff --git a/ansible/infra_common.yml b/ansible/infra_common.yml index 7c5f6844af8..6c1cbdd028f 100644 --- a/ansible/infra_common.yml +++ b/ansible/infra_common.yml @@ -1,18 +1,18 @@ --- -- name: Install Infra dynamic workloads +- name: Install Infra dynamic roles hosts: localhost gather_facts: false tags: - - dynamic_workloads + - dynamic_roles tasks: - - name: Process workloads - loop: "{{ agnosticd_infra_workloads | default([]) }}" + - name: Process roles + loop: "{{ agnosticd_infra_roles | default([]) }}" loop_control: - loop_var: _workload - label: "{{ _workload.name }}" + loop_var: _role + label: "{{ _role.name }}" include_role: - name: "{{ _workload.name }}" - defaults_from: "{{ _workload.defaults_from | default('main') }}" - handlers_from: "{{ _workload.defaults_from | default('main') }}" - tasks_from: "{{ _workload.tasks_from | default('main') }}" - vars_from: "{{ _workload.vars_from | default('main') }}" + name: "{{ _role.name }}" + defaults_from: "{{ _role.defaults_from | default('main') }}" + handlers_from: "{{ _role.defaults_from | default('main') }}" + tasks_from: "{{ _role.tasks_from | default('main') }}" + vars_from: "{{ _role.vars_from | default('main') }}" diff --git a/ansible/post_infra_common.yml b/ansible/post_infra_common.yml index 5e6700964f2..a4437d0bb37 100644 --- a/ansible/post_infra_common.yml +++ b/ansible/post_infra_common.yml @@ -1,22 +1,22 @@ --- -- name: Install Post Infra dynamic workloads +- name: Install Post Infra dynamic roles hosts: all gather_facts: false tags: - - dynamic_workloads + - dynamic_roles tasks: - - name: Process workloads - loop: "{{ agnosticd_pre_infra_workloads | default([]) }}" + - name: Process roles + loop: "{{ agnosticd_pre_infra_roles | default([]) }}" loop_control: - loop_var: _workload - label: "{{ _workload.name }}" + loop_var: _role + label: "{{ _role.name }}" include_role: - name: "{{ _workload.name }}" - defaults_from: "{{ _workload.defaults_from | default('main') }}" - handlers_from: "{{ _workload.defaults_from | default('main') }}" - tasks_from: "{{ _workload.tasks_from | default('main') }}" - vars_from: "{{ _workload.vars_from | default('main') }}" + name: "{{ _role.name }}" + defaults_from: "{{ _role.defaults_from | default('main') }}" + handlers_from: "{{ _role.defaults_from | default('main') }}" + tasks_from: "{{ _role.tasks_from | default('main') }}" + vars_from: "{{ _role.vars_from | default('main') }}" when: >- - _workload.group | default('') in group_names ) + _role.group | default('') in group_names ) or - _workload.groups | default([]) | intersect(group_names) | list | length > 0 + _role.groups | default([]) | intersect(group_names) | list | length > 0 diff --git a/ansible/post_software_common.yml b/ansible/post_software_common.yml index 3d11aae73a8..2632236bbad 100644 --- a/ansible/post_software_common.yml +++ b/ansible/post_software_common.yml @@ -1,22 +1,22 @@ --- -- name: Install Post Software dynamic workloads +- name: Install Post Software dynamic roles hosts: all gather_facts: false tags: - - dynamic_workloads + - dynamic_roles tasks: - - name: Process workloads - loop: "{{ agnosticd_post_software_workloads | default([]) }}" + - name: Process roles + loop: "{{ agnosticd_post_software_roles | default([]) }}" loop_control: - loop_var: _workload - label: "{{ _workload.name }}" + loop_var: _role + label: "{{ _role.name }}" include_role: - name: "{{ _workload.name }}" - defaults_from: "{{ _workload.defaults_from | default('main') }}" - handlers_from: "{{ _workload.defaults_from | default('main') }}" - tasks_from: "{{ _workload.tasks_from | default('main') }}" - vars_from: "{{ _workload.vars_from | default('main') }}" + name: "{{ _role.name }}" + defaults_from: "{{ _role.defaults_from | default('main') }}" + handlers_from: "{{ _role.defaults_from | default('main') }}" + tasks_from: "{{ _role.tasks_from | default('main') }}" + vars_from: "{{ _role.vars_from | default('main') }}" when: >- - _workload.group | default('') in group_names ) + _role.group | default('') in group_names ) or - _workload.groups | default([]) | intersect(group_names) | list | length > 0 + _role.groups | default([]) | intersect(group_names) | list | length > 0 diff --git a/ansible/pre_infra_common.yml b/ansible/pre_infra_common.yml index 9bc56c38afa..9cc3cca9c00 100644 --- a/ansible/pre_infra_common.yml +++ b/ansible/pre_infra_common.yml @@ -1,18 +1,18 @@ --- -- name: Install Pre Infra dynamic workloads +- name: Install Pre Infra dynamic roles hosts: localhost gather_facts: false tags: - - dynamic_workloads + - dynamic_roles tasks: - - name: Process workloads - loop: "{{ agnosticd_pre_infra_workloads | default([]) }}" + - name: Process roles + loop: "{{ agnosticd_pre_infra_roles | default([]) }}" loop_control: - loop_var: _workload - label: "{{ _workload.name }}" + loop_var: _role + label: "{{ _role.name }}" include_role: - name: "{{ _workload.name }}" - defaults_from: "{{ _workload.defaults_from | default('main') }}" - handlers_from: "{{ _workload.defaults_from | default('main') }}" - tasks_from: "{{ _workload.tasks_from | default('main') }}" - vars_from: "{{ _workload.vars_from | default('main') }}" + name: "{{ _role.name }}" + defaults_from: "{{ _role.defaults_from | default('main') }}" + handlers_from: "{{ _role.defaults_from | default('main') }}" + tasks_from: "{{ _role.tasks_from | default('main') }}" + vars_from: "{{ _role.vars_from | default('main') }}" diff --git a/ansible/pre_software_common.yml b/ansible/pre_software_common.yml index 5cd4ac14b75..d3108297ded 100644 --- a/ansible/pre_software_common.yml +++ b/ansible/pre_software_common.yml @@ -1,22 +1,22 @@ --- -- name: Install Pre Software dynamic workloads +- name: Install Pre Software dynamic roles hosts: all gather_facts: false tags: - - dynamic_workloads + - dynamic_roles tasks: - - name: Process workloads - loop: "{{ agnosticd_pre_software_workloads | default([]) }}" + - name: Process roles + loop: "{{ agnosticd_pre_software_roles | default([]) }}" loop_control: - loop_var: _workload - label: "{{ _workload.name }}" + loop_var: _role + label: "{{ _role.name }}" include_role: - name: "{{ _workload.name }}" - defaults_from: "{{ _workload.defaults_from | default('main') }}" - handlers_from: "{{ _workload.defaults_from | default('main') }}" - tasks_from: "{{ _workload.tasks_from | default('main') }}" - vars_from: "{{ _workload.vars_from | default('main') }}" + name: "{{ _role.name }}" + defaults_from: "{{ _role.defaults_from | default('main') }}" + handlers_from: "{{ _role.defaults_from | default('main') }}" + tasks_from: "{{ _role.tasks_from | default('main') }}" + vars_from: "{{ _role.vars_from | default('main') }}" when: >- - _workload.group | default('') in group_names ) + _role.group | default('') in group_names ) or - _workload.groups | default([]) | intersect(group_names) | list | length > 0 + _role.groups | default([]) | intersect(group_names) | list | length > 0 diff --git a/ansible/software_common.yml b/ansible/software_common.yml index bd92d8fd669..fe0355d30e3 100644 --- a/ansible/software_common.yml +++ b/ansible/software_common.yml @@ -1,22 +1,22 @@ --- -- name: Install Software dynamic workloads +- name: Install Software dynamic roles hosts: all gather_facts: false tags: - - dynamic_workloads + - dynamic_roles tasks: - - name: Process workloads - loop: "{{ agnosticd_software_workloads | default([]) }}" + - name: Process roles + loop: "{{ agnosticd_software_roles | default([]) }}" loop_control: - loop_var: _workload - label: "{{ _workload.name }}" + loop_var: _role + label: "{{ _role.name }}" include_role: - name: "{{ _workload.name }}" - defaults_from: "{{ _workload.defaults_from | default('main') }}" - handlers_from: "{{ _workload.defaults_from | default('main') }}" - tasks_from: "{{ _workload.tasks_from | default('main') }}" - vars_from: "{{ _workload.vars_from | default('main') }}" + name: "{{ _role.name }}" + defaults_from: "{{ _role.defaults_from | default('main') }}" + handlers_from: "{{ _role.defaults_from | default('main') }}" + tasks_from: "{{ _role.tasks_from | default('main') }}" + vars_from: "{{ _role.vars_from | default('main') }}" when: >- - _workload.group | default('') in group_names ) + _role.group | default('') in group_names ) or - _workload.groups | default([]) | intersect(group_names) | list | length > 0 + _role.groups | default([]) | intersect(group_names) | list | length > 0 From 3fb28fb1644861ff270f3ec1248c6b2566e1145a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Cor=C3=A9?= Date: Mon, 21 Jun 2021 12:58:47 +0200 Subject: [PATCH 03/16] refine structure --- ansible/{infra_common.yml => hooks/infra.yml} | 0 ansible/hooks/post_destroy.yml | 18 ++++++++++++ .../post_infra.yml} | 4 +-- .../post_software.yml} | 2 +- .../pre_infra.yml} | 0 .../pre_software.yml} | 2 +- .../software.yml} | 2 +- ansible/main.yml | 29 ++++++++++++------- 8 files changed, 41 insertions(+), 16 deletions(-) rename ansible/{infra_common.yml => hooks/infra.yml} (100%) create mode 100644 ansible/hooks/post_destroy.yml rename ansible/{post_infra_common.yml => hooks/post_infra.yml} (85%) rename ansible/{post_software_common.yml => hooks/post_software.yml} (93%) rename ansible/{pre_infra_common.yml => hooks/pre_infra.yml} (100%) rename ansible/{pre_software_common.yml => hooks/pre_software.yml} (93%) rename ansible/{software_common.yml => hooks/software.yml} (93%) diff --git a/ansible/infra_common.yml b/ansible/hooks/infra.yml similarity index 100% rename from ansible/infra_common.yml rename to ansible/hooks/infra.yml diff --git a/ansible/hooks/post_destroy.yml b/ansible/hooks/post_destroy.yml new file mode 100644 index 00000000000..168ed9aba6f --- /dev/null +++ b/ansible/hooks/post_destroy.yml @@ -0,0 +1,18 @@ +--- +- name: Post Destroy Dynamic Roles + hosts: localhost + gather_facts: false + tags: + - dynamic_roles + tasks: + - name: Process roles + loop: "{{ agnosticd_post_destroy_roles | default([]) }}" + loop_control: + loop_var: _role + label: "{{ _role.name }}" + include_role: + name: "{{ _role.name }}" + defaults_from: "{{ _role.defaults_from | default('main') }}" + handlers_from: "{{ _role.defaults_from | default('main') }}" + tasks_from: "{{ _role.tasks_from | default('main') }}" + vars_from: "{{ _role.vars_from | default('main') }}" diff --git a/ansible/post_infra_common.yml b/ansible/hooks/post_infra.yml similarity index 85% rename from ansible/post_infra_common.yml rename to ansible/hooks/post_infra.yml index a4437d0bb37..adf0665f0fb 100644 --- a/ansible/post_infra_common.yml +++ b/ansible/hooks/post_infra.yml @@ -6,7 +6,7 @@ - dynamic_roles tasks: - name: Process roles - loop: "{{ agnosticd_pre_infra_roles | default([]) }}" + loop: "{{ agnosticd_post_infra_roles | default([]) }}" loop_control: loop_var: _role label: "{{ _role.name }}" @@ -17,6 +17,6 @@ tasks_from: "{{ _role.tasks_from | default('main') }}" vars_from: "{{ _role.vars_from | default('main') }}" when: >- - _role.group | default('') in group_names ) + _role.group | default('') in group_names or _role.groups | default([]) | intersect(group_names) | list | length > 0 diff --git a/ansible/post_software_common.yml b/ansible/hooks/post_software.yml similarity index 93% rename from ansible/post_software_common.yml rename to ansible/hooks/post_software.yml index 2632236bbad..f6f202fdc2f 100644 --- a/ansible/post_software_common.yml +++ b/ansible/hooks/post_software.yml @@ -17,6 +17,6 @@ tasks_from: "{{ _role.tasks_from | default('main') }}" vars_from: "{{ _role.vars_from | default('main') }}" when: >- - _role.group | default('') in group_names ) + _role.group | default('') in group_names or _role.groups | default([]) | intersect(group_names) | list | length > 0 diff --git a/ansible/pre_infra_common.yml b/ansible/hooks/pre_infra.yml similarity index 100% rename from ansible/pre_infra_common.yml rename to ansible/hooks/pre_infra.yml diff --git a/ansible/pre_software_common.yml b/ansible/hooks/pre_software.yml similarity index 93% rename from ansible/pre_software_common.yml rename to ansible/hooks/pre_software.yml index d3108297ded..f6df1658678 100644 --- a/ansible/pre_software_common.yml +++ b/ansible/hooks/pre_software.yml @@ -17,6 +17,6 @@ tasks_from: "{{ _role.tasks_from | default('main') }}" vars_from: "{{ _role.vars_from | default('main') }}" when: >- - _role.group | default('') in group_names ) + _role.group | default('') in group_names or _role.groups | default([]) | intersect(group_names) | list | length > 0 diff --git a/ansible/software_common.yml b/ansible/hooks/software.yml similarity index 93% rename from ansible/software_common.yml rename to ansible/hooks/software.yml index fe0355d30e3..8ba651b2ff5 100644 --- a/ansible/software_common.yml +++ b/ansible/hooks/software.yml @@ -17,6 +17,6 @@ tasks_from: "{{ _role.tasks_from | default('main') }}" vars_from: "{{ _role.vars_from | default('main') }}" when: >- - _role.group | default('') in group_names ) + _role.group | default('') in group_names or _role.groups | default([]) | intersect(group_names) | list | length > 0 diff --git a/ansible/main.yml b/ansible/main.yml index 1f98c495dc4..f2f5b7911eb 100644 --- a/ansible/main.yml +++ b/ansible/main.yml @@ -24,10 +24,11 @@ - step000 - pre_infra_tasks -- name: Common Pre Infra - import_playbook: pre_infra_common.yml +- name: Pre Infra Dynamic Roles + import_playbook: hooks/pre_infra.yml tags: - step000 + - dynamic_roles - pre_infra_tasks ################################################################################# @@ -49,10 +50,11 @@ - step001 - deploy_infrastructure -- name: Common Infra - import_playbook: infra_common.yml +- name: Infra Dynamic Roles + import_playbook: hooks/infra.yml tags: - step001 + - dynamic_roles - deploy_infrastructure ################################################################################ @@ -67,10 +69,11 @@ - post_infra - post_infra_tasks -- name: Common Post Infra - import_playbook: post_infra_common.yml +- name: Post Infra Dynamic Roles + import_playbook: hooks/post_infra.yml tags: - step002 + - dynamic_roles - post_infra - post_infra_tasks @@ -95,10 +98,11 @@ - pre_software - pre_software_tasks -- name: Common Pre Software - import_playbook: pre_software_common.yml +- name: Pre Software Dynamic Roles + import_playbook: hooks/pre_software.yml tags: - step003 + - dynamic_roles - pre_software - pre_software_tasks @@ -120,10 +124,11 @@ - step004 - deploy_software -- name: Common Software - import_playbook: software_common.yml +- name: Software Dynamic Roles + import_playbook: hooks/software.yml tags: - step004 + - dynamic_roles - deploy_software ################################################################################ @@ -138,9 +143,11 @@ - post_software - post_software_tasks -- import_playbook: post_software_common.yml +- name: Post Software Dynamic Roles + import_playbook: hooks/post_software.yml tags: - step005 + - dynamic_roles - post_software - post_software_tasks From 161c2ce853a0c422c4763bc455945c80c2b342e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Cor=C3=A9?= Date: Mon, 21 Jun 2021 13:05:25 +0200 Subject: [PATCH 04/16] Fix handlers_from values --- ansible/hooks/infra.yml | 2 +- ansible/hooks/post_destroy.yml | 2 +- ansible/hooks/post_infra.yml | 2 +- ansible/hooks/post_software.yml | 2 +- ansible/hooks/pre_infra.yml | 2 +- ansible/hooks/pre_software.yml | 2 +- ansible/hooks/software.yml | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ansible/hooks/infra.yml b/ansible/hooks/infra.yml index 6c1cbdd028f..63fb791b0fd 100644 --- a/ansible/hooks/infra.yml +++ b/ansible/hooks/infra.yml @@ -13,6 +13,6 @@ include_role: name: "{{ _role.name }}" defaults_from: "{{ _role.defaults_from | default('main') }}" - handlers_from: "{{ _role.defaults_from | default('main') }}" + handlers_from: "{{ _role.handlers_from | default('main') }}" tasks_from: "{{ _role.tasks_from | default('main') }}" vars_from: "{{ _role.vars_from | default('main') }}" diff --git a/ansible/hooks/post_destroy.yml b/ansible/hooks/post_destroy.yml index 168ed9aba6f..8941a62bd30 100644 --- a/ansible/hooks/post_destroy.yml +++ b/ansible/hooks/post_destroy.yml @@ -13,6 +13,6 @@ include_role: name: "{{ _role.name }}" defaults_from: "{{ _role.defaults_from | default('main') }}" - handlers_from: "{{ _role.defaults_from | default('main') }}" + handlers_from: "{{ _role.handlers_from | default('main') }}" tasks_from: "{{ _role.tasks_from | default('main') }}" vars_from: "{{ _role.vars_from | default('main') }}" diff --git a/ansible/hooks/post_infra.yml b/ansible/hooks/post_infra.yml index adf0665f0fb..b8eabf5536e 100644 --- a/ansible/hooks/post_infra.yml +++ b/ansible/hooks/post_infra.yml @@ -13,7 +13,7 @@ include_role: name: "{{ _role.name }}" defaults_from: "{{ _role.defaults_from | default('main') }}" - handlers_from: "{{ _role.defaults_from | default('main') }}" + handlers_from: "{{ _role.handlers_from | default('main') }}" tasks_from: "{{ _role.tasks_from | default('main') }}" vars_from: "{{ _role.vars_from | default('main') }}" when: >- diff --git a/ansible/hooks/post_software.yml b/ansible/hooks/post_software.yml index f6f202fdc2f..ab796cfb958 100644 --- a/ansible/hooks/post_software.yml +++ b/ansible/hooks/post_software.yml @@ -13,7 +13,7 @@ include_role: name: "{{ _role.name }}" defaults_from: "{{ _role.defaults_from | default('main') }}" - handlers_from: "{{ _role.defaults_from | default('main') }}" + handlers_from: "{{ _role.handlers_from | default('main') }}" tasks_from: "{{ _role.tasks_from | default('main') }}" vars_from: "{{ _role.vars_from | default('main') }}" when: >- diff --git a/ansible/hooks/pre_infra.yml b/ansible/hooks/pre_infra.yml index 9cc3cca9c00..98f0212f796 100644 --- a/ansible/hooks/pre_infra.yml +++ b/ansible/hooks/pre_infra.yml @@ -13,6 +13,6 @@ include_role: name: "{{ _role.name }}" defaults_from: "{{ _role.defaults_from | default('main') }}" - handlers_from: "{{ _role.defaults_from | default('main') }}" + handlers_from: "{{ _role.handlers_from | default('main') }}" tasks_from: "{{ _role.tasks_from | default('main') }}" vars_from: "{{ _role.vars_from | default('main') }}" diff --git a/ansible/hooks/pre_software.yml b/ansible/hooks/pre_software.yml index f6df1658678..6a56f8c990b 100644 --- a/ansible/hooks/pre_software.yml +++ b/ansible/hooks/pre_software.yml @@ -13,7 +13,7 @@ include_role: name: "{{ _role.name }}" defaults_from: "{{ _role.defaults_from | default('main') }}" - handlers_from: "{{ _role.defaults_from | default('main') }}" + handlers_from: "{{ _role.handlers_from | default('main') }}" tasks_from: "{{ _role.tasks_from | default('main') }}" vars_from: "{{ _role.vars_from | default('main') }}" when: >- diff --git a/ansible/hooks/software.yml b/ansible/hooks/software.yml index 8ba651b2ff5..82ad3ee0972 100644 --- a/ansible/hooks/software.yml +++ b/ansible/hooks/software.yml @@ -13,7 +13,7 @@ include_role: name: "{{ _role.name }}" defaults_from: "{{ _role.defaults_from | default('main') }}" - handlers_from: "{{ _role.defaults_from | default('main') }}" + handlers_from: "{{ _role.handlers_from | default('main') }}" tasks_from: "{{ _role.tasks_from | default('main') }}" vars_from: "{{ _role.vars_from | default('main') }}" when: >- From 03ba89a2ae36f73d4db112b8b31049b4fa40ee9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Cor=C3=A9?= Date: Mon, 21 Jun 2021 13:07:42 +0200 Subject: [PATCH 05/16] Add hook to destroy.yml --- ansible/destroy.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ansible/destroy.yml b/ansible/destroy.yml index 88909f01fe1..f87e6601ba0 100644 --- a/ansible/destroy.yml +++ b/ansible/destroy.yml @@ -29,6 +29,12 @@ }) }} +- name: Post Destroy Hooks + import_playbook: hooks/post_destroy.yaml + tags: + - dynamic_roles + - post_destroy + - import_playbook: save_output_dir.yml - import_playbook: completion_callback.yml From 2383245d0261b67b015f2be1b5256633dc24689a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Cor=C3=A9?= Date: Mon, 21 Jun 2021 14:04:45 +0200 Subject: [PATCH 06/16] Expose stage to dynamic roles: agnosticd_stage var --- ansible/hooks/infra.yml | 2 ++ ansible/hooks/post_destroy.yml | 2 ++ ansible/hooks/post_infra.yml | 2 ++ ansible/hooks/post_software.yml | 5 +++++ ansible/hooks/pre_infra.yml | 2 ++ ansible/hooks/pre_software.yml | 2 ++ ansible/hooks/software.yml | 2 ++ 7 files changed, 17 insertions(+) diff --git a/ansible/hooks/infra.yml b/ansible/hooks/infra.yml index 63fb791b0fd..0e44e5d3e39 100644 --- a/ansible/hooks/infra.yml +++ b/ansible/hooks/infra.yml @@ -6,6 +6,8 @@ - dynamic_roles tasks: - name: Process roles + vars: + agnosticd_stage: infra loop: "{{ agnosticd_infra_roles | default([]) }}" loop_control: loop_var: _role diff --git a/ansible/hooks/post_destroy.yml b/ansible/hooks/post_destroy.yml index 8941a62bd30..06887c0fc86 100644 --- a/ansible/hooks/post_destroy.yml +++ b/ansible/hooks/post_destroy.yml @@ -6,6 +6,8 @@ - dynamic_roles tasks: - name: Process roles + vars: + agnosticd_stage: post_destroy loop: "{{ agnosticd_post_destroy_roles | default([]) }}" loop_control: loop_var: _role diff --git a/ansible/hooks/post_infra.yml b/ansible/hooks/post_infra.yml index b8eabf5536e..dd7b7785e9b 100644 --- a/ansible/hooks/post_infra.yml +++ b/ansible/hooks/post_infra.yml @@ -6,6 +6,8 @@ - dynamic_roles tasks: - name: Process roles + vars: + agnosticd_stage: post_infra loop: "{{ agnosticd_post_infra_roles | default([]) }}" loop_control: loop_var: _role diff --git a/ansible/hooks/post_software.yml b/ansible/hooks/post_software.yml index ab796cfb958..8f25da6b638 100644 --- a/ansible/hooks/post_software.yml +++ b/ansible/hooks/post_software.yml @@ -6,11 +6,16 @@ - dynamic_roles tasks: - name: Process roles + vars: + agnosticd_stage: post_software loop: "{{ agnosticd_post_software_roles | default([]) }}" loop_control: loop_var: _role label: "{{ _role.name }}" include_role: + apply: + tags: + - post_software name: "{{ _role.name }}" defaults_from: "{{ _role.defaults_from | default('main') }}" handlers_from: "{{ _role.handlers_from | default('main') }}" diff --git a/ansible/hooks/pre_infra.yml b/ansible/hooks/pre_infra.yml index 98f0212f796..b8105771ab0 100644 --- a/ansible/hooks/pre_infra.yml +++ b/ansible/hooks/pre_infra.yml @@ -6,6 +6,8 @@ - dynamic_roles tasks: - name: Process roles + vars: + agnosticd_stage: pre_infra loop: "{{ agnosticd_pre_infra_roles | default([]) }}" loop_control: loop_var: _role diff --git a/ansible/hooks/pre_software.yml b/ansible/hooks/pre_software.yml index 6a56f8c990b..02a05b3e435 100644 --- a/ansible/hooks/pre_software.yml +++ b/ansible/hooks/pre_software.yml @@ -6,6 +6,8 @@ - dynamic_roles tasks: - name: Process roles + vars: + agnosticd_stage: pre_software loop: "{{ agnosticd_pre_software_roles | default([]) }}" loop_control: loop_var: _role diff --git a/ansible/hooks/software.yml b/ansible/hooks/software.yml index 82ad3ee0972..2925773a005 100644 --- a/ansible/hooks/software.yml +++ b/ansible/hooks/software.yml @@ -6,6 +6,8 @@ - dynamic_roles tasks: - name: Process roles + vars: + agnosticd_stage: software loop: "{{ agnosticd_software_roles | default([]) }}" loop_control: loop_var: _role From 7e0b629036619c8a2b2196e9c687648055d4dfd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Cor=C3=A9?= Date: Mon, 21 Jun 2021 14:09:30 +0200 Subject: [PATCH 07/16] Tidy tags in main.yml and destroy.yml --- ansible/destroy.yml | 1 + ansible/main.yml | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/ansible/destroy.yml b/ansible/destroy.yml index f87e6601ba0..2fb307c83b3 100644 --- a/ansible/destroy.yml +++ b/ansible/destroy.yml @@ -34,6 +34,7 @@ tags: - dynamic_roles - post_destroy + - post_destroy_tasks - import_playbook: save_output_dir.yml diff --git a/ansible/main.yml b/ansible/main.yml index f2f5b7911eb..92a8aa7a758 100644 --- a/ansible/main.yml +++ b/ansible/main.yml @@ -29,6 +29,7 @@ tags: - step000 - dynamic_roles + - pre_infra - pre_infra_tasks ################################################################################# @@ -49,11 +50,16 @@ tags: - step001 - deploy_infrastructure + - infra + - infra_tasks - name: Infra Dynamic Roles import_playbook: hooks/infra.yml tags: - step001 + - deploy_infrastructure + - infra + - infra_tasks - dynamic_roles - deploy_infrastructure @@ -102,9 +108,9 @@ import_playbook: hooks/pre_software.yml tags: - step003 - - dynamic_roles - pre_software - pre_software_tasks + - dynamic_roles ################################################################################## ################################################################################## @@ -117,19 +123,25 @@ tags: - step004 - deploy_software + - software + - software_tasks - name: Software Playbook '{{ software_to_deploy | d('none') }}' import_playbook: "./software_playbooks/{{ software_to_deploy | d('none') }}.yml" tags: - step004 - deploy_software + - software + - software_tasks - name: Software Dynamic Roles import_playbook: hooks/software.yml tags: - step004 - - dynamic_roles - deploy_software + - software + - software_tasks + - dynamic_roles ################################################################################ ################################################################################ @@ -147,9 +159,9 @@ import_playbook: hooks/post_software.yml tags: - step005 - - dynamic_roles - post_software - post_software_tasks + - dynamic_roles - import_playbook: "export_inventory.yml" vars: From 47838a70b18bca0ea4a95ec1d4f750415da3e76e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Cor=C3=A9?= Date: Mon, 15 Nov 2021 10:35:14 +0100 Subject: [PATCH 08/16] Add 'when' keyword Add the ability to specify a condition per role, in the hook list. --- ansible/hooks/infra.yml | 1 + ansible/hooks/post_destroy.yml | 1 + ansible/hooks/post_infra.yml | 10 +++++++--- ansible/hooks/post_software.yml | 10 +++++++--- ansible/hooks/pre_infra.yml | 1 + ansible/hooks/pre_software.yml | 10 +++++++--- ansible/hooks/software.yml | 10 +++++++--- 7 files changed, 31 insertions(+), 12 deletions(-) diff --git a/ansible/hooks/infra.yml b/ansible/hooks/infra.yml index 0e44e5d3e39..d7e6f626dea 100644 --- a/ansible/hooks/infra.yml +++ b/ansible/hooks/infra.yml @@ -12,6 +12,7 @@ loop_control: loop_var: _role label: "{{ _role.name }}" + when: "{{ _role.when | default(true) }}" include_role: name: "{{ _role.name }}" defaults_from: "{{ _role.defaults_from | default('main') }}" diff --git a/ansible/hooks/post_destroy.yml b/ansible/hooks/post_destroy.yml index 06887c0fc86..3cf6952e67a 100644 --- a/ansible/hooks/post_destroy.yml +++ b/ansible/hooks/post_destroy.yml @@ -12,6 +12,7 @@ loop_control: loop_var: _role label: "{{ _role.name }}" + when: "{{ _role.when | default(true) }}" include_role: name: "{{ _role.name }}" defaults_from: "{{ _role.defaults_from | default('main') }}" diff --git a/ansible/hooks/post_infra.yml b/ansible/hooks/post_infra.yml index dd7b7785e9b..1cda656027e 100644 --- a/ansible/hooks/post_infra.yml +++ b/ansible/hooks/post_infra.yml @@ -19,6 +19,10 @@ tasks_from: "{{ _role.tasks_from | default('main') }}" vars_from: "{{ _role.vars_from | default('main') }}" when: >- - _role.group | default('') in group_names - or - _role.groups | default([]) | intersect(group_names) | list | length > 0 + {{ _role.when | default(true) }} + and + ( + _role.group | default('') in group_names + or + _role.groups | default([]) | intersect(group_names) | list | length > 0 + ) diff --git a/ansible/hooks/post_software.yml b/ansible/hooks/post_software.yml index 8f25da6b638..8e2a300f08e 100644 --- a/ansible/hooks/post_software.yml +++ b/ansible/hooks/post_software.yml @@ -22,6 +22,10 @@ tasks_from: "{{ _role.tasks_from | default('main') }}" vars_from: "{{ _role.vars_from | default('main') }}" when: >- - _role.group | default('') in group_names - or - _role.groups | default([]) | intersect(group_names) | list | length > 0 + {{ _role.when | default(true) }} + and + ( + _role.group | default('') in group_names + or + _role.groups | default([]) | intersect(group_names) | list | length > 0 + ) diff --git a/ansible/hooks/pre_infra.yml b/ansible/hooks/pre_infra.yml index b8105771ab0..5d9a7c510e6 100644 --- a/ansible/hooks/pre_infra.yml +++ b/ansible/hooks/pre_infra.yml @@ -12,6 +12,7 @@ loop_control: loop_var: _role label: "{{ _role.name }}" + when: "{{ _role.when | default(true) }}" include_role: name: "{{ _role.name }}" defaults_from: "{{ _role.defaults_from | default('main') }}" diff --git a/ansible/hooks/pre_software.yml b/ansible/hooks/pre_software.yml index 02a05b3e435..3a760faa44f 100644 --- a/ansible/hooks/pre_software.yml +++ b/ansible/hooks/pre_software.yml @@ -19,6 +19,10 @@ tasks_from: "{{ _role.tasks_from | default('main') }}" vars_from: "{{ _role.vars_from | default('main') }}" when: >- - _role.group | default('') in group_names - or - _role.groups | default([]) | intersect(group_names) | list | length > 0 + {{ _role.when | default(true) }} + and + ( + _role.group | default('') in group_names + or + _role.groups | default([]) | intersect(group_names) | list | length > 0 + ) diff --git a/ansible/hooks/software.yml b/ansible/hooks/software.yml index 2925773a005..b77cff2e512 100644 --- a/ansible/hooks/software.yml +++ b/ansible/hooks/software.yml @@ -19,6 +19,10 @@ tasks_from: "{{ _role.tasks_from | default('main') }}" vars_from: "{{ _role.vars_from | default('main') }}" when: >- - _role.group | default('') in group_names - or - _role.groups | default([]) | intersect(group_names) | list | length > 0 + {{ _role.when | default(true) }} + and + ( + _role.group | default('') in group_names + or + _role.groups | default([]) | intersect(group_names) | list | length > 0 + ) From 6143f23ccadca564c27bfbbc997a2323c9b9a334 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Cor=C3=A9?= Date: Wed, 24 Nov 2021 14:31:16 +0100 Subject: [PATCH 09/16] just-a-bunch-of-nodes update --- .../configs/just-a-bunch-of-nodes/post_infra.yml | 13 +++++++++++++ .../configs/just-a-bunch-of-nodes/pre_infra.yml | 4 ++++ .../just-a-bunch-of-nodes/pre_software.yml | 8 -------- .../infra-local-create-ssh_key/tasks/main.yml | 16 +++++++++++----- .../set_env_authorized_key/defaults/main.yaml | 1 + .../roles/set_env_authorized_key/tasks/main.yml | 8 +++++--- 6 files changed, 34 insertions(+), 16 deletions(-) diff --git a/ansible/configs/just-a-bunch-of-nodes/post_infra.yml b/ansible/configs/just-a-bunch-of-nodes/post_infra.yml index 88487c376e3..2481fb280e9 100644 --- a/ansible/configs/just-a-bunch-of-nodes/post_infra.yml +++ b/ansible/configs/just-a-bunch-of-nodes/post_infra.yml @@ -6,3 +6,16 @@ tasks: - debug: msg: "Step 002 Post Infrastructure" + +- name: Configure all hosts authorized_keys + hosts: + - all:!windows + become: true + gather_facts: false + tags: + - common_tasks + tasks: + + - include_role: + name: set_env_authorized_key + when: set_env_authorized_key | bool diff --git a/ansible/configs/just-a-bunch-of-nodes/pre_infra.yml b/ansible/configs/just-a-bunch-of-nodes/pre_infra.yml index ac875b08d77..901a48ea069 100644 --- a/ansible/configs/just-a-bunch-of-nodes/pre_infra.yml +++ b/ansible/configs/just-a-bunch-of-nodes/pre_infra.yml @@ -6,3 +6,7 @@ tasks: - debug: msg: "Step 000 Pre Infrastructure" + + - include_role: + name: infra-local-create-ssh_key + when: set_env_authorized_key | bool diff --git a/ansible/configs/just-a-bunch-of-nodes/pre_software.yml b/ansible/configs/just-a-bunch-of-nodes/pre_software.yml index d3ec4c23211..0544630112a 100644 --- a/ansible/configs/just-a-bunch-of-nodes/pre_software.yml +++ b/ansible/configs/just-a-bunch-of-nodes/pre_software.yml @@ -7,10 +7,6 @@ - debug: msg: "Step 003 Pre Software" - - include_role: - name: infra-local-create-ssh_key - when: set_env_authorized_key | bool - - name: Configure all hosts with Repositories, Common Files and Set environment key hosts: - all:!windows @@ -27,10 +23,6 @@ name: common when: install_common | bool - - include_role: - name: set_env_authorized_key - when: set_env_authorized_key | bool - - name: Configuring Bastion Hosts hosts: bastions become: true diff --git a/ansible/roles-infra/infra-local-create-ssh_key/tasks/main.yml b/ansible/roles-infra/infra-local-create-ssh_key/tasks/main.yml index bfa33b88e04..f2a24285274 100644 --- a/ansible/roles-infra/infra-local-create-ssh_key/tasks/main.yml +++ b/ansible/roles-infra/infra-local-create-ssh_key/tasks/main.yml @@ -1,18 +1,24 @@ --- - name: Generate SSH keys - shell: ssh-keygen -b 2048 -t rsa -f "{{output_dir}}/{{env_authorized_key}}" -q -N "" + command: >- + ssh-keygen -b 2048 -t rsa + -f "{{ output_dir }}/{{ env_authorized_key }}" + -q -N "" args: - creates: "{{output_dir}}/{{env_authorized_key}}" + creates: "{{ output_dir }}/{{ env_authorized_key }}" when: set_env_authorized_key | bool - name: fix permission file: - path: "{{output_dir}}/{{env_authorized_key}}" + path: "{{ output_dir }}/{{ env_authorized_key }}" mode: 0400 when: set_env_authorized_key | bool - name: Generate SSH pub key - shell: ssh-keygen -y -f "{{output_dir}}/{{env_authorized_key}}" > "{{output_dir}}/{{env_authorized_key}}.pub" + shell: >- + ssh-keygen -y -f + "{{ output_dir }}/{{ env_authorized_key }}" + > "{{ output_dir }}/{{ env_authorized_key }}.pub" args: - creates: "{{output_dir}}/{{env_authorized_key}}.pub" + creates: "{{ output_dir }}/{{ env_authorized_key }}.pub" when: set_env_authorized_key | bool diff --git a/ansible/roles/set_env_authorized_key/defaults/main.yaml b/ansible/roles/set_env_authorized_key/defaults/main.yaml index ed97d539c09..f3198c9daa9 100644 --- a/ansible/roles/set_env_authorized_key/defaults/main.yaml +++ b/ansible/roles/set_env_authorized_key/defaults/main.yaml @@ -1 +1,2 @@ --- +env_authorized_key: infra_key diff --git a/ansible/roles/set_env_authorized_key/tasks/main.yml b/ansible/roles/set_env_authorized_key/tasks/main.yml index d0829db8090..d88cb127804 100644 --- a/ansible/roles/set_env_authorized_key/tasks/main.yml +++ b/ansible/roles/set_env_authorized_key/tasks/main.yml @@ -5,7 +5,7 @@ mode: 0700 state: directory -- name: copy the environment .pem key +- name: Copy the infra .pem key on the bastions become: true copy: src: "{{ output_dir }}/{{ env_authorized_key }}" @@ -13,9 +13,11 @@ owner: root group: root mode: 0400 - when: set_env_authorized_key|bool + when: >- + set_env_authorized_key|bool + and inventory_hostname in groups['bastions'] -- name: copy the environment .pub key +- name: Copy the environment .pub key become: true copy: src: "{{output_dir}}/{{ env_authorized_key }}.pub" From 408498e9fde1d0b5ade2d3a4a95abe8c9a43121d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Cor=C3=A9?= Date: Fri, 3 Dec 2021 21:46:46 +0100 Subject: [PATCH 10/16] Add facts, be able to specify host --- ansible/hooks/facts_tasks.yaml | 4 +++ .../{post_destroy.yml => post_destroy.yaml} | 3 ++ ansible/hooks/post_infra.yml | 25 +++++++++++++++ ansible/hooks/post_software.yml | 25 +++++++++++++++ ansible/hooks/pre_software.yml | 31 ++++++++++++++++++- ansible/hooks/software.yml | 26 ++++++++++++++++ 6 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 ansible/hooks/facts_tasks.yaml rename ansible/hooks/{post_destroy.yml => post_destroy.yaml} (89%) diff --git a/ansible/hooks/facts_tasks.yaml b/ansible/hooks/facts_tasks.yaml new file mode 100644 index 00000000000..c99eb7c8419 --- /dev/null +++ b/ansible/hooks/facts_tasks.yaml @@ -0,0 +1,4 @@ +--- +- name: Set facts + loop: "{{ _facts | default({}) | dict2items }}" + set_fact: {"{{ item.key }}":"{{ item.value }}"} diff --git a/ansible/hooks/post_destroy.yml b/ansible/hooks/post_destroy.yaml similarity index 89% rename from ansible/hooks/post_destroy.yml rename to ansible/hooks/post_destroy.yaml index 3cf6952e67a..802e86bab3c 100644 --- a/ansible/hooks/post_destroy.yml +++ b/ansible/hooks/post_destroy.yaml @@ -19,3 +19,6 @@ handlers_from: "{{ _role.handlers_from | default('main') }}" tasks_from: "{{ _role.tasks_from | default('main') }}" vars_from: "{{ _role.vars_from | default('main') }}" + apply: + vars: + - "{{ _role.vars | default({}) }}" diff --git a/ansible/hooks/post_infra.yml b/ansible/hooks/post_infra.yml index 1cda656027e..c0fe8d7b978 100644 --- a/ansible/hooks/post_infra.yml +++ b/ansible/hooks/post_infra.yml @@ -5,6 +5,27 @@ tags: - dynamic_roles tasks: + - name: Process facts + loop: "{{ agnosticd_post_infra_roles | default([]) }}" + loop_control: + loop_var: _role + label: "{{ _role.name }}" + when: >- + _role.facts | default({}) | length > 0 + and + {{ _role.when | default(true) }} + and + ( + _role.host | default('') == inventory_hostname + or + _role.group | default('') in group_names + or + _role.groups | default([]) | intersect(group_names) | list | length > 0 + ) + vars: + _facts: "{{ _role.facts }}" + include_tasks: facts_tasks.yaml + - name: Process roles vars: agnosticd_stage: post_infra @@ -19,9 +40,13 @@ tasks_from: "{{ _role.tasks_from | default('main') }}" vars_from: "{{ _role.vars_from | default('main') }}" when: >- + _role.name | default('') != '' + and {{ _role.when | default(true) }} and ( + _role.host | default('') == inventory_hostname + or _role.group | default('') in group_names or _role.groups | default([]) | intersect(group_names) | list | length > 0 diff --git a/ansible/hooks/post_software.yml b/ansible/hooks/post_software.yml index 8e2a300f08e..2007ea7cff3 100644 --- a/ansible/hooks/post_software.yml +++ b/ansible/hooks/post_software.yml @@ -5,6 +5,27 @@ tags: - dynamic_roles tasks: + - name: Process facts + loop: "{{ agnosticd_post_software_roles | default([]) }}" + loop_control: + loop_var: _role + label: "{{ _role.name }}" + when: >- + _role.facts | default({}) | length > 0 + and + {{ _role.when | default(true) }} + and + ( + _role.host | default('') == inventory_hostname + or + _role.group | default('') in group_names + or + _role.groups | default([]) | intersect(group_names) | list | length > 0 + ) + vars: + _facts: "{{ _role.facts }}" + include_tasks: facts_tasks.yaml + - name: Process roles vars: agnosticd_stage: post_software @@ -22,9 +43,13 @@ tasks_from: "{{ _role.tasks_from | default('main') }}" vars_from: "{{ _role.vars_from | default('main') }}" when: >- + _role.name | default('') != '' + and {{ _role.when | default(true) }} and ( + _role.host | default('') == inventory_hostname + or _role.group | default('') in group_names or _role.groups | default([]) | intersect(group_names) | list | length > 0 diff --git a/ansible/hooks/pre_software.yml b/ansible/hooks/pre_software.yml index 3a760faa44f..72cc70641e3 100644 --- a/ansible/hooks/pre_software.yml +++ b/ansible/hooks/pre_software.yml @@ -5,6 +5,29 @@ tags: - dynamic_roles tasks: + - name: Process facts + loop: "{{ agnosticd_pre_software_roles | default([]) }}" + loop_control: + loop_var: _role + label: "{{ _role.name }}" + when: >- + _role.facts | default({}) | length > 0 + and + {{ _role.when | default(true) }} + and + ( + _role.host | default('') == inventory_hostname + or + _role.group | default('') in group_names + or + _role.groups | default([]) + | intersect(group_names) | list | length > 0 + ) + + include_tasks: facts_tasks.yaml + vars: + _facts: "{{ _role.facts }}" + - name: Process roles vars: agnosticd_stage: pre_software @@ -18,11 +41,17 @@ handlers_from: "{{ _role.handlers_from | default('main') }}" tasks_from: "{{ _role.tasks_from | default('main') }}" vars_from: "{{ _role.vars_from | default('main') }}" + when: >- + _role.name | default('') != '' + and {{ _role.when | default(true) }} and ( + _role.host | default('') == inventory_hostname + or _role.group | default('') in group_names or - _role.groups | default([]) | intersect(group_names) | list | length > 0 + _role.groups | default([]) + | intersect(group_names) | list | length > 0 ) diff --git a/ansible/hooks/software.yml b/ansible/hooks/software.yml index b77cff2e512..8ba4b8f6cb9 100644 --- a/ansible/hooks/software.yml +++ b/ansible/hooks/software.yml @@ -5,6 +5,28 @@ tags: - dynamic_roles tasks: + - name: Process facts + loop: "{{ agnosticd_software_roles | default([]) }}" + loop_control: + loop_var: _role + label: "{{ _role.name }}" + when: >- + _role.facts | default({}) | length > 0 + and + {{ _role.when | default(true) }} + and + ( + _role.host | default('') == inventory_hostname + or + _role.group | default('') in group_names + or + _role.groups | default([]) | intersect(group_names) | list | length > 0 + ) + vars: + _facts: "{{ _role.facts }}" + include_tasks: facts_tasks.yaml + + - name: Process roles vars: agnosticd_stage: software @@ -19,9 +41,13 @@ tasks_from: "{{ _role.tasks_from | default('main') }}" vars_from: "{{ _role.vars_from | default('main') }}" when: >- + _role.name | default('') != '' + and {{ _role.when | default(true) }} and ( + _role.host | default('') == inventory_hostname + or _role.group | default('') in group_names or _role.groups | default([]) | intersect(group_names) | list | length > 0 From 793d68d4b2bd88fb2fa20342f843a4dd9942de24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Cor=C3=A9?= Date: Fri, 3 Dec 2021 21:48:04 +0100 Subject: [PATCH 11/16] Add requirement_contents variable - enable providing the list of requirements (roles, collection) dynamically from agnosticV --- ansible/install_galaxy_roles.yml | 34 +++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/ansible/install_galaxy_roles.yml b/ansible/install_galaxy_roles.yml index 3e943636c87..6149bdad4c9 100644 --- a/ansible/install_galaxy_roles.yml +++ b/ansible/install_galaxy_roles.yml @@ -11,21 +11,45 @@ ## Needs to be validated ## var is called from main.yml requirements_path: "configs/{{ env_type }}/requirements.yml" + requirements_content: {} tasks: + - name: requirements_content is provided + when: requirements_content | length > 0 + block: + - name: Use requirements_content + set_fact: + requirements_path_final: "{{ output_dir }}/custom_requirements.yml" + + - name: Copy requirements content to output_dir + copy: + dest: "{{ requirements_path_final }}" + content: "{{ requirements_content | to_yaml }}" + + - when: requirements_content | length == 0 + name: Use requirements_path from the config + set_fact: + requirements_path_final: "{{ requirements_path }}" + - name: Check if requirements.yml exists stat: - path: "{{ requirements_path }}" + path: "{{ requirements_path_final }}" register: r_requirements_stat - set_fact: - r_requirements_content: "{{ lookup('file', requirements_path) | from_yaml }}" + r_requirements_content: "{{ lookup('file', requirements_path_final) | from_yaml }}" when: r_requirements_stat.stat.exists - name: Import roles from requirements.yml command: >- ansible-galaxy install - -r "{{ requirements_path }}" - -p "{{ ANSIBLE_REPO_PATH | default('.') }}/configs/{{ env_type }}/roles" + -r "{{ requirements_path_final }}" + -p "{%- if requirements_content | length > 0 -%} + {{ playbook_dir }}/dynamic-roles + {%- else -%} + {{ ANSIBLE_REPO_PATH + | default(playbook_dir) + | default('.') }}/configs/{{ env_type }}/roles + {%- endif -%}" when: >- r_requirements_stat.stat.exists and r_requirements_content | length > 0 @@ -37,7 +61,7 @@ - name: Import collections from requirements.yml command: >- ansible-galaxy collection install - -r "{{ requirements_path }}" + -r "{{ requirements_path_final }}" -p collections --force-with-deps when: >- From 5920c2242010ffb0ac27eb904332ced161120000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Cor=C3=A9?= Date: Fri, 3 Dec 2021 21:48:45 +0100 Subject: [PATCH 12/16] Fix repo_method in just-a-bunch-of-nodes --- ansible/configs/just-a-bunch-of-nodes/pre_software.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ansible/configs/just-a-bunch-of-nodes/pre_software.yml b/ansible/configs/just-a-bunch-of-nodes/pre_software.yml index 0544630112a..bd6115500fd 100644 --- a/ansible/configs/just-a-bunch-of-nodes/pre_software.yml +++ b/ansible/configs/just-a-bunch-of-nodes/pre_software.yml @@ -17,7 +17,7 @@ tasks: - include_role: name: set-repositories - when: repo_method is defined + when: repo_method is defined and repo_method != 'none' - include_role: name: common From 9cab49b3d234c1d1bb7365a390ee58dbc7db0341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Cor=C3=A9?= Date: Fri, 3 Dec 2021 23:24:48 +0100 Subject: [PATCH 13/16] Add complex example with OCP4 aio on equinix --- .../sample_vars/equinix_metal_ocp4_aio.yaml | 205 ++++++++++++++++++ 1 file changed, 205 insertions(+) create mode 100644 ansible/configs/just-a-bunch-of-nodes/sample_vars/equinix_metal_ocp4_aio.yaml diff --git a/ansible/configs/just-a-bunch-of-nodes/sample_vars/equinix_metal_ocp4_aio.yaml b/ansible/configs/just-a-bunch-of-nodes/sample_vars/equinix_metal_ocp4_aio.yaml new file mode 100644 index 00000000000..017ff2adf6b --- /dev/null +++ b/ansible/configs/just-a-bunch-of-nodes/sample_vars/equinix_metal_ocp4_aio.yaml @@ -0,0 +1,205 @@ +--- +cloud_provider: equinix_metal +remote_user: root +cloud_tags: + ci_org_owner: Customer And Field engagement BU + env_type: '{{ env_type }}' + platform: '{{ platform | d(''unknown'') }}' +env_type: just-a-bunch-of-nodes +extra_sno_nodes: false +hypervisor_count: 1 +hypervisor_os: centos_8 +install_bastion: false +install_common: false +install_ftl: false +install_student_user: true +key_name: opentlc_admin_backdoor +ocp4_aio_deploy_acm: false +ocp4_aio_deploy_acs: false +ocp4_aio_deploy_cnv: false +ocp4_aio_deploy_cnvlab: true +ocp4_aio_deploy_compact: false +ocp4_aio_deploy_disconnected: false +ocp4_aio_deploy_guacamole: true +ocp4_aio_deploy_ipi: true +ocp4_aio_deploy_nfs: false +ocp4_aio_deploy_ocp: true +ocp4_aio_deploy_ocp_plus: false +ocp4_aio_deploy_ocs: true +ocp4_aio_deploy_ods: false +ocp4_aio_deploy_sno: false +ocp4_aio_deploy_type: ipi +deploy_type: ipi +ocp4_aio_ocp_version: 4.9.5 +ocp4_aio_ocp_workers: 3 +ocp4_aio_baremetal_provider: "{{ cloud_provider }}" +ocp4_aio_rhcos_iso_url: https://mirror.openshift.com/pub/openshift-v4/dependencies/rhcos/latest/latest/rhcos-live.x86_64.iso +ocp4_aio_use_ddns: true +own_repo_path: http://d3s3zqyaz8cp2d.cloudfront.net/repos/ocp/{{osrelease}} +platform: dev +pull_secret: '{{ ocp4_pull_secret }}' +purpose: production +repo_method: none +install_common: false +install_bastion: false +set_env_authorized_key: false +override_deploy_ocp: >- + {{ ( ocp4_aio_deploy_ocs + or ocp4_aio_deploy_cnv + or ocp4_aio_deploy_acm ) | bool + }} +override_deploy_compact: "{{ ocp4_aio_ocp_workers != 0 }}" +override_deploy_nfs: "{{ not ocp4_aio_deploy_ocs | bool }}" + +equinix_metal_facility: am6 +hypervisor_type: s3.xlarge.x86 + +ansible_user: root + +# Environment Instances +instances: + - name: "hypervisor" + count: "{{ hypervisor_count }}" + public_dns: true + type: "{{ hypervisor_type }}" + os: "{{ hypervisor_os }}" + facility: "{{ equinix_metal_facility }}" + tags: + - key: "AnsibleGroup" + value: "bastions,hypervisors" + - key: "ostype" + value: "linux" + +requirements_content: + roles: + - src: https://github.com/fridim/ocp4_aio_infra_role_base_software.git + scm: git + name: ocp4_aio_base_software + version: main + + - name: ocp4_aio_base_virt + src: https://github.com/RHFieldProductManagement/ocp4_aio_infra_role_base_virt.git + scm: git + version: v0.0.3 + + - name: ocp4_aio_prepare_bastion + src: https://github.com/RHFieldProductManagement/ocp4_aio_infra_role_prepare_bastion.git + scm: git + version: v0.0.2 + + - name: ocp4_aio_role_acm + src: https://github.com/RHFieldProductManagement/ocp4_aio_role_acm.git + scm: git + version: v0.0.1 + + - name: ocp4_aio_role_acs + src: https://github.com/RHFieldProductManagement/ocp4_aio_role_acs.git + scm: git + version: v0.0.1 + + - name: ocp4_aio_role_cnv + src: https://github.com/RHFieldProductManagement/ocp4_aio_role_cnv.git + scm: git + version: v0.0.1 + + - name: ocp4_aio_role_imgreg + src: https://github.com/RHFieldProductManagement/ocp4_aio_role_imgreg.git + scm: git + version: v0.0.1 + + - name: ocp4_aio_role_nfsmount + src: https://github.com/RHFieldProductManagement/ocp4_aio_role_nfsmount.git + scm: git + version: v0.0.2 + + - name: ocp4_aio_role_ocs + src: https://github.com/RHFieldProductManagement/ocp4_aio_role_ocs.git + scm: git + version: v0.0.3 + + - name: ocp4_aio_deploy_bastion + src: https://github.com/RHFieldProductManagement/ocp4_aio_infra_role_deploy_bastion.git + scm: git + version: v0.0.3 + + - name: ocp4_aio_deploy_guac + src: https://github.com/RHFieldProductManagement/ocp4_aio_infra_role_deploy_guacamole.git + scm: git + version: v0.0.1 + + - name: ocp4_aio_deploy_ocp + src: https://github.com/RHFieldProductManagement/ocp4_aio_infra_role_deploy_ocp.git + scm: git + version: v0.0.4 + + - name: ocp4_aio_workload_cnvlab + src: https://github.com/RHFieldProductManagement/ocp4_aio_role_deploy_cnvlab.git + scm: git + version: v0.0.2 + + # PoC, implement rogue tasks as roles + + - name: ocp4_equinix_aio_post_infra + src: https://github.com/fridim/ocp4_equinix_aio_post_infra + version: main + + - name: ocp4_equinix_aio_set_guid + src: https://github.com/fridim/ocp4_equinix_aio_set_guid + version: main + + - name: ocp4_equinix_metal_kube + src: https://github.com/fridim/ocp4_equinix_metal_kube + version: main + + collections: + - name: community.general + - name: containers.podman + + +agnosticd_post_infra_roles: + - name: ocp4_equinix_aio_post_infra + +agnosticd_pre_software_roles: + - name: ocp4_equinix_aio_set_guid + group: hypervisors + + - name: bastion-student-user + group: hypervisors + when: install_student_user + + - name: ocp4_aio_base_software + group: hypervisors + + - name: ocp4_aio_base_virt + group: hypervisors + facts: + aio_host_ip_address: "{{ hostvars['hypervisor']['public_ip_address'] }}" + + - name: ocp4_aio_prepare_bastion + group: hypervisors + facts: + ocp4_aio_ssh_key: "{{ lookup('file', '{{ output_dir }}/{{ guid }}_id_rsa.pub' ) }}" + + - name: ocp4_aio_deploy_guac + group: hypervisors + when: ocp4_aio_deploy_guacamole + + - name: ocp4_aio_deploy_bastion + host: bastion-vm + facts: + ocp4_aio_ssh_key: "{{ lookup('file', '{{ output_dir }}/{{ guid }}_id_rsa.pub' ) }}" + +agnosticd_software_roles: + - name: ocp4_aio_deploy_ocp + host: bastion-vm + facts: + aio_host_ip_address: "{{ hostvars['hypervisor']['public_ip_address'] }}" + +agnosticd_post_software_roles: + # Setup OCS Storage + - name: ocp4_aio_role_ocs + host: bastion-vm + + # Enable internal registry + - name: ocp4_aio_role_imgreg + host: bastion-vm From 9b0074ff14a0e5af49f201b7f734e7c31f27a009 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Cor=C3=A9?= Date: Mon, 6 Dec 2021 10:28:56 +0100 Subject: [PATCH 14/16] Remove duplicate keys --- .../sample_vars/equinix_metal_ocp4_aio.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/ansible/configs/just-a-bunch-of-nodes/sample_vars/equinix_metal_ocp4_aio.yaml b/ansible/configs/just-a-bunch-of-nodes/sample_vars/equinix_metal_ocp4_aio.yaml index 017ff2adf6b..982a8b9e187 100644 --- a/ansible/configs/just-a-bunch-of-nodes/sample_vars/equinix_metal_ocp4_aio.yaml +++ b/ansible/configs/just-a-bunch-of-nodes/sample_vars/equinix_metal_ocp4_aio.yaml @@ -40,8 +40,6 @@ platform: dev pull_secret: '{{ ocp4_pull_secret }}' purpose: production repo_method: none -install_common: false -install_bastion: false set_env_authorized_key: false override_deploy_ocp: >- {{ ( ocp4_aio_deploy_ocs From a1364bbb4ac24371ef2e360cc5178ba9f8200c3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Cor=C3=A9?= Date: Mon, 6 Dec 2021 10:34:22 +0100 Subject: [PATCH 15/16] Add facts hooks --- ansible/hooks/infra.yml | 14 ++++++++++++++ ansible/hooks/post_destroy.yaml | 14 ++++++++++++++ ansible/hooks/post_software.yml | 4 +++- ansible/hooks/pre_infra.yml | 14 ++++++++++++++ ansible/hooks/software.yml | 4 +++- 5 files changed, 48 insertions(+), 2 deletions(-) diff --git a/ansible/hooks/infra.yml b/ansible/hooks/infra.yml index d7e6f626dea..57b8a9f2f24 100644 --- a/ansible/hooks/infra.yml +++ b/ansible/hooks/infra.yml @@ -5,6 +5,20 @@ tags: - dynamic_roles tasks: + - name: Process facts + loop: "{{ agnosticd_infra_roles | default([]) }}" + loop_control: + loop_var: _role + label: "{{ _role.name }}" + when: >- + _role.facts | default({}) | length > 0 + and + {{ _role.when | default(true) }} + + include_tasks: facts_tasks.yaml + vars: + _facts: "{{ _role.facts }}" + - name: Process roles vars: agnosticd_stage: infra diff --git a/ansible/hooks/post_destroy.yaml b/ansible/hooks/post_destroy.yaml index 802e86bab3c..df319c28ffa 100644 --- a/ansible/hooks/post_destroy.yaml +++ b/ansible/hooks/post_destroy.yaml @@ -5,6 +5,20 @@ tags: - dynamic_roles tasks: + - name: Process facts + loop: "{{ agnosticd_post_destroy_roles | default([]) }}" + loop_control: + loop_var: _role + label: "{{ _role.name }}" + when: >- + _role.facts | default({}) | length > 0 + and + {{ _role.when | default(true) }} + + include_tasks: facts_tasks.yaml + vars: + _facts: "{{ _role.facts }}" + - name: Process roles vars: agnosticd_stage: post_destroy diff --git a/ansible/hooks/post_software.yml b/ansible/hooks/post_software.yml index 2007ea7cff3..d3c0bc874a5 100644 --- a/ansible/hooks/post_software.yml +++ b/ansible/hooks/post_software.yml @@ -20,8 +20,10 @@ or _role.group | default('') in group_names or - _role.groups | default([]) | intersect(group_names) | list | length > 0 + _role.groups | default([]) + | intersect(group_names) | list | length > 0 ) + vars: _facts: "{{ _role.facts }}" include_tasks: facts_tasks.yaml diff --git a/ansible/hooks/pre_infra.yml b/ansible/hooks/pre_infra.yml index 5d9a7c510e6..d22c9bc3221 100644 --- a/ansible/hooks/pre_infra.yml +++ b/ansible/hooks/pre_infra.yml @@ -5,6 +5,20 @@ tags: - dynamic_roles tasks: + - name: Process facts + loop: "{{ agnosticd_pre_infra_roles | default([]) }}" + loop_control: + loop_var: _role + label: "{{ _role.name }}" + when: >- + _role.facts | default({}) | length > 0 + and + {{ _role.when | default(true) }} + + include_tasks: facts_tasks.yaml + vars: + _facts: "{{ _role.facts }}" + - name: Process roles vars: agnosticd_stage: pre_infra diff --git a/ansible/hooks/software.yml b/ansible/hooks/software.yml index 8ba4b8f6cb9..6132ff48f6d 100644 --- a/ansible/hooks/software.yml +++ b/ansible/hooks/software.yml @@ -20,8 +20,10 @@ or _role.group | default('') in group_names or - _role.groups | default([]) | intersect(group_names) | list | length > 0 + _role.groups | default([]) + | intersect(group_names) | list | length > 0 ) + vars: _facts: "{{ _role.facts }}" include_tasks: facts_tasks.yaml From a8fe1167684b3c77b1e2fe7d612429e2f590c0d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Cor=C3=A9?= Date: Mon, 21 Feb 2022 09:51:42 +0100 Subject: [PATCH 16/16] Use test lookup to avoid warnings - update test lookup to support booleans --- ansible/hooks/infra.yml | 4 +-- ansible/hooks/post_destroy.yaml | 4 +-- ansible/hooks/post_infra.yml | 4 +-- ansible/hooks/post_software.yml | 4 +-- ansible/hooks/pre_infra.yml | 4 +-- ansible/hooks/pre_software.yml | 4 +-- ansible/hooks/software.yml | 4 +-- ansible/lookup_plugins/test.py | 43 +++++++++++++++++++++++++++++++++ 8 files changed, 57 insertions(+), 14 deletions(-) create mode 100644 ansible/lookup_plugins/test.py diff --git a/ansible/hooks/infra.yml b/ansible/hooks/infra.yml index 57b8a9f2f24..736b2ee455e 100644 --- a/ansible/hooks/infra.yml +++ b/ansible/hooks/infra.yml @@ -13,7 +13,7 @@ when: >- _role.facts | default({}) | length > 0 and - {{ _role.when | default(true) }} + lookup('test', _role.when | default(true)) include_tasks: facts_tasks.yaml vars: @@ -26,7 +26,7 @@ loop_control: loop_var: _role label: "{{ _role.name }}" - when: "{{ _role.when | default(true) }}" + when: lookup('test', _role.when | default(true)) include_role: name: "{{ _role.name }}" defaults_from: "{{ _role.defaults_from | default('main') }}" diff --git a/ansible/hooks/post_destroy.yaml b/ansible/hooks/post_destroy.yaml index df319c28ffa..6f93deb2e8a 100644 --- a/ansible/hooks/post_destroy.yaml +++ b/ansible/hooks/post_destroy.yaml @@ -13,7 +13,7 @@ when: >- _role.facts | default({}) | length > 0 and - {{ _role.when | default(true) }} + lookup('test', _role.when | default(true)) include_tasks: facts_tasks.yaml vars: @@ -26,7 +26,7 @@ loop_control: loop_var: _role label: "{{ _role.name }}" - when: "{{ _role.when | default(true) }}" + when: lookup('test', _role.when | default(true)) include_role: name: "{{ _role.name }}" defaults_from: "{{ _role.defaults_from | default('main') }}" diff --git a/ansible/hooks/post_infra.yml b/ansible/hooks/post_infra.yml index c0fe8d7b978..e7d4f0d6fac 100644 --- a/ansible/hooks/post_infra.yml +++ b/ansible/hooks/post_infra.yml @@ -13,7 +13,7 @@ when: >- _role.facts | default({}) | length > 0 and - {{ _role.when | default(true) }} + lookup('test', _role.when | default(true)) and ( _role.host | default('') == inventory_hostname @@ -42,7 +42,7 @@ when: >- _role.name | default('') != '' and - {{ _role.when | default(true) }} + lookup('test', _role.when | default(true)) and ( _role.host | default('') == inventory_hostname diff --git a/ansible/hooks/post_software.yml b/ansible/hooks/post_software.yml index d3c0bc874a5..70074c8fd43 100644 --- a/ansible/hooks/post_software.yml +++ b/ansible/hooks/post_software.yml @@ -13,7 +13,7 @@ when: >- _role.facts | default({}) | length > 0 and - {{ _role.when | default(true) }} + lookup('test', _role.when | default(true)) and ( _role.host | default('') == inventory_hostname @@ -47,7 +47,7 @@ when: >- _role.name | default('') != '' and - {{ _role.when | default(true) }} + lookup('test', _role.when | default(true)) and ( _role.host | default('') == inventory_hostname diff --git a/ansible/hooks/pre_infra.yml b/ansible/hooks/pre_infra.yml index d22c9bc3221..c2bf00ac584 100644 --- a/ansible/hooks/pre_infra.yml +++ b/ansible/hooks/pre_infra.yml @@ -13,7 +13,7 @@ when: >- _role.facts | default({}) | length > 0 and - {{ _role.when | default(true) }} + lookup('test', _role.when | default(true)) include_tasks: facts_tasks.yaml vars: @@ -26,7 +26,7 @@ loop_control: loop_var: _role label: "{{ _role.name }}" - when: "{{ _role.when | default(true) }}" + when: lookup('test', _role.when | default(true)) include_role: name: "{{ _role.name }}" defaults_from: "{{ _role.defaults_from | default('main') }}" diff --git a/ansible/hooks/pre_software.yml b/ansible/hooks/pre_software.yml index 72cc70641e3..c80968890ac 100644 --- a/ansible/hooks/pre_software.yml +++ b/ansible/hooks/pre_software.yml @@ -13,7 +13,7 @@ when: >- _role.facts | default({}) | length > 0 and - {{ _role.when | default(true) }} + lookup('test', _role.when | default(true)) and ( _role.host | default('') == inventory_hostname @@ -45,7 +45,7 @@ when: >- _role.name | default('') != '' and - {{ _role.when | default(true) }} + lookup('test', _role.when | default(true)) and ( _role.host | default('') == inventory_hostname diff --git a/ansible/hooks/software.yml b/ansible/hooks/software.yml index 6132ff48f6d..8dd90f0e653 100644 --- a/ansible/hooks/software.yml +++ b/ansible/hooks/software.yml @@ -13,7 +13,7 @@ when: >- _role.facts | default({}) | length > 0 and - {{ _role.when | default(true) }} + lookup('test', _role.when | default(true)) and ( _role.host | default('') == inventory_hostname @@ -45,7 +45,7 @@ when: >- _role.name | default('') != '' and - {{ _role.when | default(true) }} + lookup('test', _role.when | default(true)) and ( _role.host | default('') == inventory_hostname diff --git a/ansible/lookup_plugins/test.py b/ansible/lookup_plugins/test.py new file mode 100644 index 00000000000..8f269e0172e --- /dev/null +++ b/ansible/lookup_plugins/test.py @@ -0,0 +1,43 @@ +# Copyright: (c) 2020, Johnathan Kupferer +# Copyright: (c) 2022, Guillaume Core +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +DOCUMENTATION = """ + lookup: test + author: Johnathan Kupferer , Guillaume Core + version_added: "2.9" + short_description: check test condition + description: + - Return evaluation of test condition + options: + _terms: + description: list of tests +""" + +EXAMPLES = """ +- name: show templating results + debug: + msg: "{{ lookup('test', 'foo is defined') }}" +""" + +RETURN = """ +_raw: + description: boolean result of conditional evaluation +""" + +from ansible.plugins.lookup import LookupBase +from ansible.plugins.filter.core import to_bool + +class LookupModule(LookupBase): + + def run(self, terms, variables, **kwargs): + result = [] + for term in terms: + if isinstance(term, str): + result.append(self._templar.template('{{(' + term + ')|bool}}')) + else: + result.append(to_bool(term)) + return result