Post

Ansible collection

Ansible collection

Setup

I like to use a virtual environment by using the python module venv.

  1. Setup the environment and upgrade pip + setuptools.
  2. Activate the newly created environment.
  3. Install “ansible-core” or “ansible”, the latter “batteries included”.

python3 -m venv -h outputs:

1
2
3
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear] [--upgrade]
            [--without-pip] [--prompt PROMPT] [--upgrade-deps]
            ENV_DIR [ENV_DIR ...]

I like to have the venv directory hidden so my command is like this.

1
2
3
python3 -m venv .venv --upgrade-deps --prompt "Ansible venv"
source .venv/bin/activate
pip install ansible-core

Ansible configuration

ansible.cfg

Generate a new ansible.cfg file with comments:

1
ansible-config init --disabled > ansible.cfg

Define hosts to manage with inventory file or files. I typically use hosts.ini filename.

Ansible environment

export ANSIBLE_COLLECTIONS_PATH=./collections export ANSIBLE_ROLES_PATH=./roles

Ansible playbook example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
---
- name:
  hosts: workstations
  tasks:
    - name: "Example"   # Best practice to use "", should work without
      fqn.of.module.you.want.to.use:
        options-and-arguments-of-module:
      when: this is true
      # Or use this when having multiple criterias.
      when:
        - this is true
        - and this is true
  
    - name: "Another task"
      another.module:
This post is licensed under CC BY 4.0 by the author.