From charlesreid1

A best practice is to tag inventory with the type of environment you intend to deploy it to.

Static inventory files

If you are maintaining a static inventory file (see Ansible/Vagrant/Static Inventory or Ansible/EC2/Static Inventory), you can tag your inventory with the environment it shoudl be deployed to by grouping servers.

Group by:

  • purpose of the host (roles)
  • geogaphy/location

Example static hosts file:

production:

[atlanta-webservers]
www-atl-1.example.com
www-atl-2.example.com

[boston-webservers]
www-bos-1.example.com
www-bos-2.example.com

[atlanta-dbservers]
db-atl-1.example.com
db-atl-2.example.com

[boston-dbservers]
db-bos-1.example.com


# webservers in all geos
[webservers:children]
atlanta-webservers
boston-webservers

# dbservers in all geos
[dbservers:children]
atlanta-dbservers
boston-dbservers

# everything in the atlanta geo
[atlanta:children]
atlanta-webservers
atlanta-dbservers

# everything in the boston geo
[boston:children]
boston-webservers
boston-dbservers


Dynamic inventory files

If you're managing your inventory dynamically, you should have a way to apply tags to each machine that can indicate the environment into which the machine should be deployed.

In EC2 for example, you can apply tags to machines, so you could apply the tag environment:production. This should allow Ansible to automatically discover a group of systems named ec2_tag_environment_production.

Applying Variables to Groups

Once you've defined groups for the static/dynamic inventory file, you can define variables for each gorup.

Example: all servers in the "atlanta" group can have a network time protocol server and backup URL server set in their group vars file:

group_vars/atlanta:

ntp: ntp-atlanta.example.com
backup: backup-atlanta.example.com

You can also define variables for all web servers:

group_vars/webservers

apacheMaxRequestsPerChild: 3000
apacheMaxClients: 900

All group

The all group applies default values or universal values:

group_vars/all:

ntp: ntp-boston.example.com
backup: backup-boston.example.com


Flags