molecule: adapt sudo setup to work when ansible is not connecting as root on the target

This commit is contained in:
Romain Pelisse 2024-02-22 11:14:54 +01:00
parent f62a97709a
commit b3ca517583
2 changed files with 23 additions and 3 deletions

View file

@ -5,12 +5,14 @@
- name: Install sudo - name: Install sudo
ansible.builtin.yum: ansible.builtin.yum:
name: name:
- sudo
- java-1.8.0-openjdk - java-1.8.0-openjdk
state: present state: present
- name: Prepare - name: Prepare
hosts: all hosts: all
gather_facts: yes
vars:
sudo_pkg_name: sudo
tasks: tasks:
- name: "Run preparation common to all scenario" - name: "Run preparation common to all scenario"
ansible.builtin.include_tasks: ../prepare.yml ansible.builtin.include_tasks: ../prepare.yml

View file

@ -3,9 +3,27 @@
ansible.builtin.debug: ansible.builtin.debug:
msg: "Ansible version is {{ ansible_version.full }}" msg: "Ansible version is {{ ansible_version.full }}"
- name: Install sudo
- name: "Ensure {{ sudo_pkg_name }} is installed (if user is root)."
ansible.builtin.yum: ansible.builtin.yum:
name: name: "{{ sudo_pkg_name }}"
when:
- ansible_user_id == 'root'
- name: Gather the package facts
ansible.builtin.package_facts:
manager: auto
- name: "Check if {{ sudo_pkg_name }} is installed."
ansible.builtin.assert:
that:
- sudo_pkg_name in ansible_facts.packages
- name: Install sudo
become: yes
ansible.builtin.yum:
name:
- sudo - sudo
- iproute - iproute
state: present state: present