Skip to content

Commit

Permalink
Reduce code duplication in molecule scenarios prepare phase
Browse files Browse the repository at this point in the history
  • Loading branch information
rpelisse committed Mar 12, 2024
1 parent 268c6a4 commit 85784bb
Show file tree
Hide file tree
Showing 21 changed files with 116 additions and 212 deletions.
12 changes: 2 additions & 10 deletions molecule/account/prepare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,5 @@
gather_facts: yes
tasks:

- name: "Display Ansible version"
ansible.builtin.debug:
msg: "Ansible version is {{ ansible_version.full }}"

- name: "Install sudo if user is root"
ansible.builtin.yum:
name: sudo
state: present
when:
- ansible_user_id == "root"
- name: "Include common prepare tasks"
ansible.builtin.include_tasks: ../prepare.yml
33 changes: 9 additions & 24 deletions molecule/aphrodite/prepare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,19 @@
- vars.yml
tasks:

- name: Display Ansible version
ansible.builtin.debug:
msg: "Ansible version is {{ ansible_version.full }}"

- name: "Install sudo if user is root"
ansible.builtin.yum:
name: sudo
state: present
when:
- ansible_user_id == "root"
- name: Add the user jenkins
ansible.builtin.user:
name: jenkins

- name: Creates directory
become: yes
ansible.builtin.file:
path: /opt/tools
state: directory
owner: jenkins
group: jenkins
mode: 0644
- name: "Include common prepare tasks"
ansible.builtin.include_tasks: ../prepare.yml
vars:
users:
- "{{ aphrodite_user }}"
dirs:
- { path: /opt/tools, owner: "{{ aphrodite_user }}", group: "{{ aphrodite_user }}" }

- name: "Deploy aphrodite configuration to {{ aphrodite.path }}"
become: yes
ansible.builtin.template:
src: "../../roles/aphrodite/templates/aphrodite.json.j2"
dest: "{{ aphrodite.path }}"
owner: jenkins
group: jenkins
owner: "{{ aphrodite_user }}"
group: "{{ aphrodite_user }}"
mode: 0644
1 change: 1 addition & 0 deletions molecule/aphrodite/vars.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
aphrodite_user: jenkins
aphrodite:
trackers:
bugzilla:
Expand Down
12 changes: 2 additions & 10 deletions molecule/bashrc/prepare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,5 @@
gather_facts: yes
tasks:

- name: Display Ansible version
ansible.builtin.debug:
msg: "Ansible version is {{ ansible_version.full }}"

- name: "Install sudo if user is root"
ansible.builtin.yum:
name: sudo
state: present
when:
- ansible_user_id == "root"
- name: "Include common prepare tasks"
ansible.builtin.include_tasks: ../prepare.yml
26 changes: 7 additions & 19 deletions molecule/cron/prepare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,10 @@
- vars.yml
tasks:

- name: Display Ansible version
ansible.builtin.debug:
msg: "Ansible version is {{ ansible_version.full }}"

- name: "Install sudo if user is root"
ansible.builtin.yum:
name: sudo
state: present
when:
- ansible_user_id == "root"

- name: Creates directory
become: yes
ansible.builtin.file:
path: /opt/tools
state: directory
owner: root
group: root
mode: 0745
- name: "Include common prepare tasks"
ansible.builtin.include_tasks: ../prepare.yml
vars:
users:
- "{{ aphrodite_user }}"
dirs:
- { path: '/opt/tools', mode: '0745' }
12 changes: 2 additions & 10 deletions molecule/fast_yum_install/prepare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,5 @@
gather_facts: yes
tasks:

- name: Display Ansible version
ansible.builtin.debug:
msg: "Ansible version is {{ ansible_version.full }}"

- name: "Install sudo if user is root"
ansible.builtin.yum:
name: sudo
state: present
when:
- ansible_user_id == "root"
- name: "Include common prepare tasks"
ansible.builtin.include_tasks: ../prepare.yml
31 changes: 7 additions & 24 deletions molecule/git/prepare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,10 @@
- vars.yml
tasks:

- name: Display Ansible version
ansible.builtin.debug:
msg: "Ansible version is {{ ansible_version.full }}"

- name: "Install sudo if user is root"
ansible.builtin.yum:
name: sudo
state: present
when:
- ansible_user_id == "root"

- name: Add the user jenkins
become: yes
ansible.builtin.user:
name: jenkins

- name: Creates directory
become: yes
ansible.builtin.file:
path: /home/jenkins
state: directory
owner: jenkins
group: jenkins
mode: 0644
- name: "Include common prepare tasks"
ansible.builtin.include_tasks: ../prepare.yml
vars:
users:
- "{{ git_user }}"
dirs:
- { path: /home/jenkins, owner: "{{ git_user }}", group: "{{ git_user }}" }
3 changes: 2 additions & 1 deletion molecule/git/vars.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
git_user: jenkins
git:
ssl:
verify: false
Expand All @@ -10,4 +11,4 @@ git:
- user: jenkins
email: [email protected]
name: "JBoss SET"
home: "/home/jenkins/"
home: "/home/jenkins/"
30 changes: 6 additions & 24 deletions molecule/hosts/prepare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,13 @@
vars_files:
- vars.yml
tasks:
- name: Display Ansible version
ansible.builtin.debug:
msg: "Ansible version is {{ ansible_version.full }}"

- name: "Install sudo if user is root"
ansible.builtin.yum:
name: sudo
state: present
when:
- ansible_user_id == "root"
- name: "Include common prepare tasks"
ansible.builtin.include_tasks: ../prepare.yml
vars:
dirs:
- { path: /etc/fake_hosts, state: touch, mode: 0644 }
- { path: /etc/real_hosts, state: touch, mode: 0644 }

- name: "Include fast_yum_install"
ansible.builtin.include_role:
name: "fast_yum_install"

- name: Creates /etc/fake_hosts directory
become: yes
ansible.builtin.file:
path: /etc/fake_hosts
state: touch
mode: 0644

- name: Creates /etc/real_hosts directory
become: yes
ansible.builtin.file:
path: /etc/real_hosts
state: touch
mode: 0644
12 changes: 2 additions & 10 deletions molecule/java/prepare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,8 @@
vars_files:
- vars.yml
tasks:
- name: Display Ansible version
ansible.builtin.debug:
msg: "Ansible version is {{ ansible_version.full }}"

- name: "Install sudo if user is root"
ansible.builtin.yum:
name: sudo
state: present
when:
- ansible_user_id == "root"
- name: "Include common prepare tasks"
ansible.builtin.include_tasks: ../prepare.yml

- ansible.builtin.assert:
that:
Expand Down
12 changes: 2 additions & 10 deletions molecule/kdump/prepare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,5 @@
gather_facts: yes
tasks:

- name: Display Ansible version
ansible.builtin.debug:
msg: "Ansible version is {{ ansible_version.full }}"

- name: "Install sudo if user is root"
ansible.builtin.yum:
name: sudo
state: present
when:
- ansible_user_id == "root"
- name: "Include common prepare tasks"
ansible.builtin.include_tasks: ../prepare.yml
21 changes: 8 additions & 13 deletions molecule/motd/prepare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,22 @@
- vars.yml
tasks:

- name: Display Ansible version
ansible.builtin.debug:
msg: "Ansible version is {{ ansible_version.full }}"
- name: "Include common prepare tasks"
ansible.builtin.include_tasks: ../prepare.yml

- name: "Install sudo if user is root"
ansible.builtin.yum:
name: sudo
state: present
when:
- ansible_user_id == "root"

- ansible.builtin.assert:
- name: "Check that path to motd file has been defined"
ansible.builtin.assert:
that:
- motd_path is defined
quiet: true

- ansible.builtin.stat:
- name: "Load metadata on {{ motd_path }}"
ansible.builtin.stat:
path: "{{ motd_path }}"
register: file_details

- ansible.builtin.assert:
- name: "Ensure that {{ motd_path }} exists."
ansible.builtin.assert:
that:
- file_details is defined
- file_details.stat is defined
Expand Down
29 changes: 7 additions & 22 deletions molecule/netrc/prepare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,11 @@
vars_files:
- vars.yml
tasks:
- name: Display Ansible version
ansible.builtin.debug:
msg: "Ansible version is {{ ansible_version.full }}"

- name: "Install sudo if user is root"
ansible.builtin.yum:
name: sudo
state: present
when:
- ansible_user_id == "root"

- name: Add the user jenkins
ansible.builtin.user:
name: jenkins

- name: Creates directory
become: yes
ansible.builtin.file:
path: /home/jenkins
state: directory
owner: jenkins
group: jenkins
mode: 0644
- name: "Include common prepare tasks"
ansible.builtin.include_tasks: ../prepare.yml
vars:
users:
- "{{ user_name }}"
dirs:
- { path: "/home/{{ user_name }}", owner: "{{ user_name }}", group: "{{ user_name }}" }
38 changes: 32 additions & 6 deletions molecule/prepare.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
---
- name: Prepare
hosts: all
tasks:
- name: "Display Ansible version"
ansible.builtin.debug:
msg: "Ansible version is {{ ansible_version.full }}"

- name: Display Ansible version
ansible.builtin.debug:
msg: "Ansible version is {{ ansible_version.full }}"
- name: "Install sudo if user is root"
ansible.builtin.yum:
name: sudo
state: present
when:
- ansible_user_id == "root"

- name: Add the user "{{ user }}"
ansible.builtin.user:
name: "{{ user }}"
when:
- users is defined and users | length > 0
loop: "{{ users }}"
loop_control:
loop_var: user

- name: "Creates directory"
become: yes
ansible.builtin.file:
path: "{{ dir.path }}"
state: "{{ dir.state | default('directory') }}"
owner: "{{ dir.owner | default('root') }}"
group: "{{ dir.group | default('root') }}"
mode: "{{ dir.mode | default('0644') }}"
when:
- dirs is defined and dirs | length > 0
loop: "{{ dirs }}"
loop_control:
loop_var: dir
23 changes: 7 additions & 16 deletions molecule/qualys_fix/prepare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,11 @@
hosts: all
gather_facts: yes
tasks:
- name: Display Ansible version
ansible.builtin.debug:
msg: "Ansible version is {{ ansible_version.full }}"

- name: "Install sudo if user is root"
ansible.builtin.yum:
name: sudo
state: present
when:
- ansible_user_id == "root"

- name: Creates directory
become: yes
ansible.builtin.file:
path: /root/.ansible/roles/infosec.qualys-cloud-agent/tasks/
state: directory
mode: 0644
- name: "Include common prepare tasks"
ansible.builtin.include_tasks: ../prepare.yml
vars:
users:
- "{{ user_name }}"
dirs:
- { path: "/root/.ansible/roles/infosec.qualys-cloud-agent/tasks/" }
3 changes: 3 additions & 0 deletions molecule/vault/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
vars_files:
- vars.yml
tasks:
- name: "Ensure scenario can be run in this execution context."
ansible.builtin.include_tasks: is_scenario_enabled.yml

- name: "Include vault tasks"
ansible.builtin.include_tasks: vault.yml
when:
Expand Down
Loading

0 comments on commit 85784bb

Please sign in to comment.