Skip to content

Commit

Permalink
Use SSH key to molecule tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vitabaks committed Dec 10, 2024
1 parent e4db810 commit 5d56206
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 79 deletions.
4 changes: 4 additions & 0 deletions .config/molecule/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ provisioner:
group_vars: ../../group_vars/
playbooks:
prepare: prepare.yml
connection_options:
ansible_ssh_common_args: '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'
ansible_private_key_file: "~/.ssh/molecule_rsa"
ansible_user: "root"

scenario:
create_sequence:
Expand Down
77 changes: 0 additions & 77 deletions automation/molecule/default/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,83 +4,6 @@
gather_facts: true

tasks:
- name: Ensure sudo group exists
become: true
become_method: su
ansible.builtin.group:
name: sudo
state: present
when: ansible_os_family == "RedHat"

- name: Allow passwordless sudo for users in sudo group
become: true
become_method: su
ansible.builtin.lineinfile:
path: /etc/sudoers
state: present
regexp: '^%sudo'
line: '%sudo ALL=(ALL) NOPASSWD: ALL'
validate: 'visudo -cf %s'
when: ansible_os_family == "RedHat"

- name: Ensure ansible user is part of sudo group
become: true
become_method: su
ansible.builtin.user:
name: "{{ ansible_facts.user | default('root') }}"
groups: sudo
append: true
when: ansible_os_family == "RedHat"

- name: Ensure authselect is installed
become: true
become_method: su
ansible.builtin.package:
name: authselect
state: present
when: ansible_os_family == "RedHat"

- name: Apply authselect changes if available
become: true
become_method: su
ansible.builtin.command: authselect apply-changes
ignore_errors: true
when: ansible_os_family == "RedHat"

- name: Check and fix PAM configuration for sudo
become: true
become_method: su
ansible.builtin.lineinfile:
path: /etc/pam.d/sudo
state: present
regexp: '^auth.*pam_unix.so'
line: 'auth sufficient pam_unix.so'
when: ansible_os_family == "RedHat"

- name: Test sudo without password
ansible.builtin.command: sudo -n true
register: sudo_test
ignore_errors: true
when: ansible_os_family == "RedHat"

- name: Debug sudo test result
ansible.builtin.debug:
var: sudo_test
when: ansible_os_family == "RedHat"

- name: Check if SELinux is installed
become: true
ansible.builtin.command: which setenforce
register: selinux_check
ignore_errors: true
when: ansible_os_family == "RedHat"

- name: Ensure SELinux is permissive (if installed)
become: true
ansible.builtin.command: setenforce 0
ignore_errors: true
when: ansible_os_family == "RedHat"

- name: Set variables for PostgreSQL Cluster deployment test
ansible.builtin.set_fact:
firewall_enabled_at_boot: false
Expand Down
30 changes: 28 additions & 2 deletions automation/molecule/default/prepare.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
- name: "Update docker network(s)"
hosts: localhost
gather_facts: false
gather_facts: true
become: false
tasks:
- name: "Create docker network: test_docker_network"
Expand All @@ -23,8 +23,34 @@
- name: "Install netaddr dependency on controlling host"
ansible.builtin.pip:
name: netaddr
become: false
environment:
PIP_BREAK_SYSTEM_PACKAGES: "1"

- name: Generate molecule SSH key on control node
ansible.builtin.user:
name: "{{ ansible_facts.user }}"
generate_ssh_key: true
ssh_key_bits: 2048
ssh_key_file: ~/.ssh/molecule_rsa

- name: Prepare instances for Molecule
hosts: all
become: true
become_method: su
tasks:
- name: Ensure SSH client package is installed
ansible.builtin.package:
name: "{{ ssh_client_package }}"
state: present
vars:
ssh_client_package: "{{ 'openssh-client' if ansible_os_family == 'Debian' else 'openssh-clients' }}"

- name: Copy public SSH key to molecule instances
ansible.builtin.copy:
src: ~/.ssh/molecule_rsa.pub
dest: /root/.ssh/authorized_keys
owner: root
group: root
mode: '0600'

...

0 comments on commit 5d56206

Please sign in to comment.