Ansible: Reusing Playbooks for Multiple Environments
Managing different environments can be a challenge when using Ansible, but you can make this process smooth by using the same playbook for different environments. This approach promotes reusability, reduces error-prone duplication, and simplifies maintenance. Here's a step-by-step guide on how to achieve this.
Before diving into the process, ensure you have a good understanding of Ansible's basics, such as playbooks, tasks, and variables. This guide assumes you're already familiar with these concepts.
Environment-Specific Variables
To use the same playbook for different environments, you need to make your playbook environment-aware. This is accomplished by using environment-specific variables.
First, create a directory structure under your Ansible project's var directory to store environment-specific variables. For instance, create a var directory and inside that, create other directories representing your environments: var/dev, var/staging, and var/production.
YAML soutien
Create a YAML file inside each environment directory, and define your environment-specific variables. For example, in var/dev/app.yml, your file might look like this:
--- dev server_name: dev.example.com db_host: dev-db.example.com
Repeat this process for your other environments, changing the values accordingly.
Include Variables in Playbook
Next, include these variable files in your playbook. You can do this at the top level of your playbook using the include_vars module:
- hosts: all
vars_files:
- ../var/{{ env }}/app.yml
tasks:
- name: Perform some task
debug:
var: server_name
Replace {{ env }} with the name of your environment. You can set this variable in your shell (like source ./setenv.sh production before running your playbook) or dynamically via Ansible tower.
Roles for Reusability
Roles are another essential aspect of making your playbooks environment-aware. Ansible roles help you organize your playbooks into reusable components. They allow you to define tasks, templates, and other files that can be included across multiple playbooks.
Create a role for each common task in your project. For example, if you have a common task like installing a web server, you could create an nginx role. Inside this role, define your tasks in the tasks/main.yml file:
---
- name: Install nginx
apt:
name: nginx
state: present
If your task requires environment-specific parameters, you can use the include_tasks module to include task files based on your environment. For example, in tasks/main.yml, you might have:
---
- name: Include environment-specific tasks
include_tasks: "tasks/{{ env }}.yml"
Then, create environment-specific task files - tasks/dev.yml, tasks/staging.yml, etc. - containing your environment-specific tasks.
Include your roles in your playbooks using the roles keyword:
- hosts: all
roles:
- nginx
vars_files:
- ../var/{{ env }}/app.yml
Using Defaults and Conditionals
You can also use defaults and conditionals to make your roles environment-aware. Define defaults in your role's default/main.yml file:
--- nginx_listen_port: 80
Then, override this default in your environment-specific variable file. If your environment-specific port is not defined, the default will be used. You can also use conditionals, like when or become, to run tasks only in specific environments.
Keeping Secrets Safe
While using environment-specific variables is powerful, avoid hard-coding sensitive data like passwords or API keys into these files. Instead, use Ansible Vault to encrypt these secrets.
Create a encrypted file (encrypted vars) for secret variables and reference it in your playbook:
- hosts: all
vars_files:
- ../var/{{ env }}/app.yml
- ../secret/{{ env }}.yml
tasks:
# ...
Usually, you won't need to edit this file, as it's meant for Ansible to use directly. But if you do, you can decrypt it with the ansible-vault edit command and enter your vault passphrase when prompted.
Finally, to run your playbook, use ansible-playbook command and pass your environment as an extra-vars:
ansible-playbook -e env=production playbook.yml
Now you're reusing the same playbook for different environments. This setup promotes code reusability, reduces errors, and simplifies maintenance. Ansible's powerful dynamic nature allows you to handle various environments efficiently. Happy coding!