From 56f3d890a076c25e65da4f663a605ec6ef06dce9 Mon Sep 17 00:00:00 2001 From: Dawa Ometto Date: Mon, 18 Nov 2024 15:23:23 +0100 Subject: [PATCH 1/6] Install dependencies before running component --- _run_component.yml | 46 ++++++++++++++++++++++++++++++++++++++++++++++ converge.yml | 12 ++++-------- 2 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 _run_component.yml diff --git a/_run_component.yml b/_run_component.yml new file mode 100644 index 0000000..61fc1e8 --- /dev/null +++ b/_run_component.yml @@ -0,0 +1,46 @@ +--- +- name: Check for plugin requirements file + stat: + path: /rsc/plugins/{{ remote_plugin.script_folder | basename }}/requirements.yml + register: register_requirements_1 + +- name: Check for plugin requirements file 2 + stat: + path: /rsc/plugins/{{ remote_plugin.script_folder | basename }}/{{ remote_plugin.path | dirname }}/requirements.yml + register: register_requirements_2 + +- name: Set path to file 1 if it exists + set_fact: + requirements_path: /rsc/plugins/{{ remote_plugin.script_folder | basename }}/requirements.yml + when: register_requirements_1.stat.exists + +- name: Set path to file 2 if file 1 does not exist and file 2 exists + set_fact: + requirements_path: /rsc/plugins/{{ remote_plugin.script_folder | basename }}/{{ remote_plugin.path | dirname }}/requirements.yml + when: not register_requirements_1.stat.exists and register_requirements_2.stat.exists + +- name: Install role dependencies for Ansible plugin + command: | + ansible-galaxy role install -r {{ requirements_path }} -p /rsc/plugins/{{ remote_plugin.script_folder | basename }} + args: + chdir: /rsc/plugins/{{ remote_plugin.script_folder | basename }} + executable: /bin/bash + when: requirements_path is defined + +- name: Install collection dependencies for Ansible plugin + command: | + ansible-galaxy collection install -r {{ requirements_path }} -p /rsc/plugins/{{ remote_plugin.script_folder | basename }}/collections + args: + chdir: /rsc/plugins/{{ remote_plugin.script_folder | basename }} + executable: /bin/bash + when: requirements_path is defined + +- name: Test the component by executing it using ansible on the workspace + ansible.builtin.command: > + ansible-playbook -c local -v -b {{ remote_plugin.arguments }} -e='{{ remote_plugin.parameters }}' /rsc/plugins/{{ remote_plugin.script_folder + }}/{{remote_plugin.path }} + register: ansible_on_workspace + changed_when: > + ansible_on_workspace.stdout_lines is not defined or + 'changed=0' not in + ansible_on_workspace.stdout_lines[ lookup('ansible.utils.index_of', ansible_on_workspace.stdout_lines, 'regex', '\s*PLAY RECAP\s*')+1 ] diff --git a/converge.yml b/converge.yml index 13d916b..354fe4f 100644 --- a/converge.yml +++ b/converge.yml @@ -8,17 +8,13 @@ msg: "{{ item.name }}" with_items: "{{ lookup('env', 'components') }}" - - name: Test the component by executing it using ansible on the workspace - ansible.builtin.command: > - ansible-playbook -c local -v -b {{ remote_plugin.arguments }} --extra-vars='{{ remote_plugin.parameters }}' /rsc/plugins/{{ item.name}}/{{ item.path }} - register: ansible_on_workspace - changed_when: > - ansible_on_workspace.stdout_lines is not defined or - 'changed=0' not in - ansible_on_workspace.stdout_lines[ lookup('ansible.utils.index_of', ansible_on_workspace.stdout_lines, 'regex', '\s*PLAY RECAP\s*')+1 ] + - name: Run components + ansible.builtin.include_tasks: _run_component.yml vars: remote_plugin: script_type: Ansible PlayBook arguments: -i 127.0.0.1, --skip-tags {{ ansible_skip_tags | join(',') }} parameters: "{{ item.parameters | default({}) | to_json }}" + script_folder: "{{ item.name }}/" + path: "{{ item.path }}" with_items: "{{ lookup('env', 'components') }}" From 7f4815fb2fb1d572036f136c2e18b6c53619fc8e Mon Sep 17 00:00:00 2001 From: Dawa Ometto Date: Mon, 18 Nov 2024 15:45:17 +0100 Subject: [PATCH 2/6] Fix --- _run_component.yml | 8 ++++---- converge.yml | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/_run_component.yml b/_run_component.yml index 61fc1e8..6e306b5 100644 --- a/_run_component.yml +++ b/_run_component.yml @@ -1,22 +1,22 @@ --- - name: Check for plugin requirements file stat: - path: /rsc/plugins/{{ remote_plugin.script_folder | basename }}/requirements.yml + path: /rsc/plugins/{{ remote_plugin.script_folder }}/requirements.yml register: register_requirements_1 - name: Check for plugin requirements file 2 stat: - path: /rsc/plugins/{{ remote_plugin.script_folder | basename }}/{{ remote_plugin.path | dirname }}/requirements.yml + path: /rsc/plugins/{{ remote_plugin.script_folder }}/{{ remote_plugin.path | dirname }}/requirements.yml register: register_requirements_2 - name: Set path to file 1 if it exists set_fact: - requirements_path: /rsc/plugins/{{ remote_plugin.script_folder | basename }}/requirements.yml + requirements_path: register_requirements_1.stat.path when: register_requirements_1.stat.exists - name: Set path to file 2 if file 1 does not exist and file 2 exists set_fact: - requirements_path: /rsc/plugins/{{ remote_plugin.script_folder | basename }}/{{ remote_plugin.path | dirname }}/requirements.yml + requirements_path: register_requirements_2.stat.path when: not register_requirements_1.stat.exists and register_requirements_2.stat.exists - name: Install role dependencies for Ansible plugin diff --git a/converge.yml b/converge.yml index 354fe4f..a45d60b 100644 --- a/converge.yml +++ b/converge.yml @@ -15,6 +15,6 @@ script_type: Ansible PlayBook arguments: -i 127.0.0.1, --skip-tags {{ ansible_skip_tags | join(',') }} parameters: "{{ item.parameters | default({}) | to_json }}" - script_folder: "{{ item.name }}/" + script_folder: "{{ item.name }}" path: "{{ item.path }}" with_items: "{{ lookup('env', 'components') }}" From 194effc354bb5c3336d10df10609a9ecfe885bec Mon Sep 17 00:00:00 2001 From: Dawa Ometto Date: Mon, 18 Nov 2024 16:07:03 +0100 Subject: [PATCH 3/6] Fix --- _run_component.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_run_component.yml b/_run_component.yml index 6e306b5..1b9d40a 100644 --- a/_run_component.yml +++ b/_run_component.yml @@ -11,12 +11,12 @@ - name: Set path to file 1 if it exists set_fact: - requirements_path: register_requirements_1.stat.path + requirements_path: "{{ register_requirements_1.stat.path }}" when: register_requirements_1.stat.exists - name: Set path to file 2 if file 1 does not exist and file 2 exists set_fact: - requirements_path: register_requirements_2.stat.path + requirements_path: "{{ register_requirements_2.stat.path }}" when: not register_requirements_1.stat.exists and register_requirements_2.stat.exists - name: Install role dependencies for Ansible plugin From d3c156ded19c9471677ac005ca3025a80aa506db Mon Sep 17 00:00:00 2001 From: Dawa Ometto Date: Mon, 18 Nov 2024 16:12:38 +0100 Subject: [PATCH 4/6] Fix idempotence --- _run_component.yml | 66 +++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/_run_component.yml b/_run_component.yml index 1b9d40a..06d8d9d 100644 --- a/_run_component.yml +++ b/_run_component.yml @@ -1,39 +1,45 @@ --- -- name: Check for plugin requirements file - stat: - path: /rsc/plugins/{{ remote_plugin.script_folder }}/requirements.yml - register: register_requirements_1 +- name: Install component Ansible Galaxy dependencies + block: + - name: Check for plugin requirements file + stat: + path: /rsc/plugins/{{ remote_plugin.script_folder }}/requirements.yml + register: register_requirements_1 -- name: Check for plugin requirements file 2 - stat: - path: /rsc/plugins/{{ remote_plugin.script_folder }}/{{ remote_plugin.path | dirname }}/requirements.yml - register: register_requirements_2 + - name: Check for plugin requirements file 2 + stat: + path: /rsc/plugins/{{ remote_plugin.script_folder }}/{{ remote_plugin.path | dirname }}/requirements.yml + register: register_requirements_2 -- name: Set path to file 1 if it exists - set_fact: - requirements_path: "{{ register_requirements_1.stat.path }}" - when: register_requirements_1.stat.exists + - name: Set path to file 1 if it exists + set_fact: + requirements_path: "{{ register_requirements_1.stat.path }}" + when: register_requirements_1.stat.exists -- name: Set path to file 2 if file 1 does not exist and file 2 exists - set_fact: - requirements_path: "{{ register_requirements_2.stat.path }}" - when: not register_requirements_1.stat.exists and register_requirements_2.stat.exists + - name: Set path to file 2 if file 1 does not exist and file 2 exists + set_fact: + requirements_path: "{{ register_requirements_2.stat.path }}" + when: not register_requirements_1.stat.exists and register_requirements_2.stat.exists -- name: Install role dependencies for Ansible plugin - command: | - ansible-galaxy role install -r {{ requirements_path }} -p /rsc/plugins/{{ remote_plugin.script_folder | basename }} - args: - chdir: /rsc/plugins/{{ remote_plugin.script_folder | basename }} - executable: /bin/bash - when: requirements_path is defined + - name: Install role dependencies for Ansible plugin + when: requirements_path is defined + command: | + ansible-galaxy role install -r {{ requirements_path }} -p /rsc/plugins/{{ remote_plugin.script_folder | basename }} + args: + chdir: /rsc/plugins/{{ remote_plugin.script_folder | basename }} + executable: /bin/bash + register: roles_install + changed_when: 'Nothing to do' not in roles_install.stdout -- name: Install collection dependencies for Ansible plugin - command: | - ansible-galaxy collection install -r {{ requirements_path }} -p /rsc/plugins/{{ remote_plugin.script_folder | basename }}/collections - args: - chdir: /rsc/plugins/{{ remote_plugin.script_folder | basename }} - executable: /bin/bash - when: requirements_path is defined + - name: Install collection dependencies for Ansible plugin + when: requirements_path is defined + command: | + ansible-galaxy collection install -r {{ requirements_path }} -p /rsc/plugins/{{ remote_plugin.script_folder | basename }}/collections + args: + chdir: /rsc/plugins/{{ remote_plugin.script_folder | basename }} + executable: /bin/bash + register: collections_install + changed_when: 'Nothing to do' not in roles_install.stdout - name: Test the component by executing it using ansible on the workspace ansible.builtin.command: > From 57074f225a67fba7d88c19cbcf8c5cc7d040760c Mon Sep 17 00:00:00 2001 From: Dawa Ometto Date: Mon, 18 Nov 2024 16:15:26 +0100 Subject: [PATCH 5/6] fix --- _run_component.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_run_component.yml b/_run_component.yml index 06d8d9d..73fb619 100644 --- a/_run_component.yml +++ b/_run_component.yml @@ -29,7 +29,7 @@ chdir: /rsc/plugins/{{ remote_plugin.script_folder | basename }} executable: /bin/bash register: roles_install - changed_when: 'Nothing to do' not in roles_install.stdout + changed_when: "'Nothing to do' not in roles_install.stdout" - name: Install collection dependencies for Ansible plugin when: requirements_path is defined @@ -39,7 +39,7 @@ chdir: /rsc/plugins/{{ remote_plugin.script_folder | basename }} executable: /bin/bash register: collections_install - changed_when: 'Nothing to do' not in roles_install.stdout + changed_when: "'Nothing to do' not in roles_install.stdout" - name: Test the component by executing it using ansible on the workspace ansible.builtin.command: > From ac62d36d88321f81279c418e4de5db7f9277a817 Mon Sep 17 00:00:00 2001 From: Dawa Ometto Date: Mon, 18 Nov 2024 16:36:27 +0100 Subject: [PATCH 6/6] fix --- _run_component.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/_run_component.yml b/_run_component.yml index 73fb619..70db46c 100644 --- a/_run_component.yml +++ b/_run_component.yml @@ -1,5 +1,6 @@ --- - name: Install component Ansible Galaxy dependencies + tags: molecule-idempotence-notest block: - name: Check for plugin requirements file stat: @@ -28,8 +29,6 @@ args: chdir: /rsc/plugins/{{ remote_plugin.script_folder | basename }} executable: /bin/bash - register: roles_install - changed_when: "'Nothing to do' not in roles_install.stdout" - name: Install collection dependencies for Ansible plugin when: requirements_path is defined @@ -38,8 +37,6 @@ args: chdir: /rsc/plugins/{{ remote_plugin.script_folder | basename }} executable: /bin/bash - register: collections_install - changed_when: "'Nothing to do' not in roles_install.stdout" - name: Test the component by executing it using ansible on the workspace ansible.builtin.command: >