Home πŸ‡ΊπŸ‡¦ Support Ukraine

Ansible

Created: 2021-10-02

Reading time: 1 min


hosts:

localhost ansible_connection=local ansible_python_interpreter=/usr/bin/python3

ansible.cfg:

[defaults]
inventory = hosts
gathering = smart
fact_caching_connection = localhost:6379:0:
fact_caching = redis
fact_caching_timeout = 7200

collections_paths = ./
roles_path = ./roles

site.yml:

---
- hosts: localhost

  roles:
    - role: name
      tags: name

requirements.yml:

collections:
  - name: ansible.posix
  - name: community.general

To install requirements: ansible-galaxy collection install -r requirements.yml

To play: ansible-playbook site.yml --tags my-task

Optional:

mkdir -pv roles/role-name/{defaults,tasks}
touch roles/role-name/{defaults,tasks}/main.yml

Molecule

Init project

export PY_COLORS=1
export ANSIBLE_FORCE_COLOR=1
python -m venv venv
. ./venv/bin/activate
pip install 'molecule[lint,ansible]' molecule-lxd ansible-lint
mkdir -pv roles
cd roles
molecule init role nginx --driver-name=lxd
cd nginx

./molecule/default/molecule.yml:

---
dependency:
  name: galaxy
driver:
  name: lxd
platforms:
  - name: s2
    source:
      alias: debian/buster/amd64
provisioner:
  name: ansible
verifier:
  name: ansible
lint: |
  set -e
  yamllint .
  ansible-lint .  
# molecule list
molecule create
# lxc list
# molecule destroy
molecule converge

Hint for ./molecule/default/molecule.yml platforms: https://github.com/ansible-community/molecule-lxd/blob/main/molecule_lxd/driver.py

Molecule for playbook

mol init scenario --driver-name=lxd

Everyting as in role’s case, except changing molecule/default/converge.yml file to include playbook file instead of role’s main task. Assuming playbook is named site.yml:

---
- name: Converge
  hosts: all
- import_playbook: ../../site.yml