Ansible Build
The Mint System collection of Ansible playbooks and roles.
Requirements
- Setup uv
- bash/zsh alias
task='./task'with optional completion
Usage
Clone this repository.
git clone git@github.com:Mint-System/Ansible-Build.gitSee task help or task for details about the project commands.
Setup
Navigate into the project folder.
cd Ansible-BuildGenerate an Ansible vault id and password.
task generate-vault-password $VAULT_ID $PASSWORDCreate an Ansible configuration from the template.
cp ansible.cfg.template ansible.cfgInstall Ansible and Python dependencies.
task installCreate an inventory folder and configure a role.
Ansbile Documentation > Build Your Inventory
Roles
See roles for details or list the roles with:
task list-rolesTargets
All Ansible roles can be deployed to a Linux Server via SSH.
flowchart TD A[Host] -->|SSH| B[Server]
Ansible Vault
If you encrypt secrets with multiple vault identities, you can specificy the vault list in the ansible.cfg like this:
[defaults]
vault_identity_list = mint_system@.vault_pass_mint_system, sozialinfo@.vault_pass_sozialinfoOr as an environment variable:
export ANSIBLE_VAULT_IDENTITY_LIST="mint_system@.vault_pass_mint_system, sozialinfo@.vault_pass_sozialinfo"Alternatively you can configure the --vault-id parameter of the Ansible playbook command:
task play --vault-id mint_system@.vault_pass_mint_system ...To decrypt single strings run this command:
task encrypt-string sozialinfo "vault_rolename_varname: secret"Deploy
Deploy the roles to the target hosts with the following commands.
List hosts in inventory.
task list-hosts inventories/setupLoad virtualenv.
source task sourceTest connection.
ansible all -m ping -i inventories/odooDeploy multiple inventories.
task play -i inventories/setup -i inventories/odoo plays/odoo.ymlDeploy Odoo stack.
task play -i inventories/odoo plays/odoo.ymlDeploy role only.
task play -i inventories/odoo plays/odoo.yml -t postgresDeploy without dependencies.
task play -i inventories/setup plays/setup.yml --skip-tags dependsDeploy role to specific host.
task play -i inventories/setup plays/setup.yml -t docker -l host.example.comDeploy role to specific group with non-default user.
task play -i inventories/setup plays/setup.yml -t docker -l host.example.com -u usernameClean Odoo stack.
task play -i inventories/odoo plays/clean.yml.yml -t odoo,odoo_volume,odoo_data,postgres,postgres_volumeClean role only.
task play -i inventories/setup plays/clean.yml.yml -t docker_networkClean dry run.
task play -i inventories/odoo plays/odoo.yml -t odoo --checkList all Odoo databases.
ansible all -i inventories/odoo -a "docker-postgres-list -c {{ postgres_hostname }}"Development
This section is about developing the Ansible Build project.
Quality
Lint the project using Ansible lint.
task lintConfiguration
Whenever possible use env variables to configure the container.
Env Config
env:
POSTGRES_USER: "{{ postgres_user }}"
POSTGRES_PASSWORD: "{{ postgres_password }}"
POSTGRES_DB: "{{ postgres_db }}"Data
To persist data use Docker volumes.
Volume Mount
Mount the folder without subfolder.
volumes:
- "{{ postgres_volume_name }}:/var/lib/postgresql/data"For Ansible config files use file mounts.
Bind Mount
volumes:
- "{{ nginx_data_dir }}/:/etc/nginx/conf.d/:ro"Docs
Every role folder must contain a README.md file.
Mark fix-me-comments with # FIXME: <your text>.
Naming Conventions
Role names must be lower case and may contain an _.
Vars that are stored in vaults are prefixed with vault_.
Template for role vars:
# Url to Docker repsitory
rolename_image:
rolename_hostname:
rolename_port:
rolename_volume_name: "{{ rolename_hostname }}"
rolename_data_dir: "/usr/share/{{ rolename_hostname }}"
rolename_password: "{{ vault_rolename_password }}"The reference roles are postgres and odoo.
Role and Tags
Roles can have multiple tags.
Example one tag
To define a Postgres role, you would:
- Create role
postges - Assign the tag
postgres - Create a task file
postgres.yml
Example multiple tags
To define a Nginx role with a config tag, you would:
- Create role
nginx - Assign the tags
nginxandnginx_config - Create the task files
nginx.ymlandnginx_config.yml
In the main.yml you would include the tasks as followed:
- name: "Include {{ role_name }} config tasks"
include_tasks: "{{ role_name }}-config.yml"
when: nginx_data_dir is defined
tags:
- nginx
- nginx_config
- name: "Include {{ role_name }} tasks"
include_tasks: "{{ role_name }}.yml"
when: nginx_image is defined
tags:
- nginxAliases
Whenever a role is applied to the same host multiple times, you can create multiple aliases for the same host. Append a selected suffix to make a distinction between the aliases:
- main: Production environment.
- int: Staging environment.
- dev: Development and test environment.
- upgrade: Upgrade environment.
- dep: Deprecated environment.
Here is an example of an host with two aliases:
all:
hosts:
zeus_web:
ansible_host: zeus.mint-system.com
zeus_main:
ansible_host: zeus.mint-system.com