Problem scenario
You want to make and test Ansible roles, and therefore you want to use Molecule. You are using RHEL version 8.x. You want to use Python 3 and Molecule. What should you do?
Solution
Prerequisites
i. You should install Docker. If you need assistance, see this posting.
ii. You should install these packages before hand:
sudo yum -y install gcc python3-devel
sudo dnf install -y redhat-rpm-config
Procedures
1. Run these commands:
sudo python3 -m pip install virtualenv
sudo python3 -m virtualenv my_env
source my_env/bin/activate
sudo yum install -y python3-devel
sudo python3 -m pip install molecule docker
molecule init role -r ansible-apache -d docker
cd ansible-apache
molecule test
2.a. Run this command: vi tasks/main.yml
2.b. Delete what is there. Put this there instead:
---
- name: "Ensure required packages are present"
yum:
name: "{{ pkg_list }}"
state: present
- name: "Ensure latest index.html is present"
template:
src: index.html.j2
dest: /var/www/html/index.html
- name: "Ensure httpd service is started and enabled"
service:
name: "{{ item }}"
state: started
enabled: true
with_items: "{{ svc_list }}"
- name: "Whitelist http in firewalld"
firewalld:
service: http
state: enabled
permanent: true
immediate: true
3. Run this command: mkdir templates
4.a. Run this command: vi templates/index.html.j2
4.b. Put this there and save the file:
<div style="text-align: center">
<h2>Managed by Ansible</h2>
</div>
5.a. Run this command: vi vars/main.yml
5.b. Delete what is there. Place this code starting at the first non-blank line:
---
pkg_list:
- httpd
- firewalld
svc_list:
- httpd
- firewalld
6.a. Run this command: vi molecule/default/tests/test_default.py
6.b. Eliminate the content that you find. Place these lines in the place of the old content:
import os
import pytest
import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
@pytest.mark.parametrize('pkg', [
'httpd',
'firewalld'
])
def test_pkg(host, pkg):
package = host.package(pkg)
assert package.is_installed
@pytest.mark.parametrize('svc', [
'httpd',
'firewalld'
])
def test_svc(host, svc):
service = host.service(svc)
assert service.is_running
assert service.is_enabled
@pytest.mark.parametrize('file, content', [
("/etc/firewalld/zones/public.xml", "<service name=\"http\"/>"),
("/var/www/html/index.html", "Managed by Ansible")
])
def test_files(host, file, content):
file = host.file(file)
assert file.exists
assert file.contains(content)
7. Run this command: molecule test
8. You are done. The above directions were adapted from a DigitalOcean tutorial here.