# Ansible Multiple Environment Best Practices

> Handling multiple infrastructure environments with Ansible by targeting the environment tag that is included in the mandatory AWS tags

- Source: https://www.itwonderlab.com/ansible-multiple-environment-best-practices/
- Published: 2023-09-07
- Updated: 2026-10-05
- Author: Javier Ruiz Jiménez (https://www.javierruizjimenez.com/)
- Site: IT Wonder Lab (https://www.itwonderlab.com/)

---

## Ansible Multiple Environment

Handling multiple infrastructure environments with Ansible is easy when the playbooks target the environment tag that is included in the mandatory AWS Tags. See [AWS Tagging Best Practices](https://www.itwonderlab.com/aws-resource-tagging/).

### Avoid mixing up environments

Use safeguards, redundant configurations and different keys for each environment to reduce the possibility of applying changes to the wrong client or environment.

Applying the PRE environment configuration to the PRO environment would be a serious mistake.

To prevent these errors:

### Environment Tag

Use the tag **tag\_environment\_ENV** as a selector in playbooks, for example:

```json title="ditwl_ENV.yml"
  - hosts: tag_os_ubuntu:&tag_environment_ENV
    become: yes
    roles:
      - { role: linux/pam_limits, tags: [ 'pam_limits'] }
      - { role: linux/hosts_file, tags: [ 'hosts_file'] }
      - { role: linux/host_name, tags: [ 'host_name'] }
    tags:
      - common
  ...
```

### Multiple Ansible Playbooks

Have a **different Ansible playbook** for each environment, e.g.:

-   ditwl\_des.yml
-   ditwl\_pre.yml
-   ditwl\_pro.yml

### Unique Ansible Private Keys

**Have different SSH Key files for each combination of environment and user** that has to have SSH access either by console or using Ansible.

The key is set using the ANSIBLE\_PRIVATE\_KEY\_FILE environment variable. Each <mark>USER</mark> should have their own private key for each environment <mark>ENV</mark>.

E.g.: ditwl\_pk\_<mark>USER</mark>\_<mark>ENV</mark>\_infradmin.pem
