2018-03-14 20:44:21 +01:00
.. _playbooks_best_practices:
2012-05-13 17:56:09 +02:00
Best Practices
==============
2014-12-01 20:24:41 +01:00
Here are some tips for making the most of Ansible and Ansible playbooks.
2012-05-13 17:56:09 +02:00
2013-10-05 20:57:45 +02:00
You can find some example playbooks illustrating these best practices in our `ansible-examples repository <https://github.com/ansible/ansible-examples> `_ . (NOTE: These may not use all of the features in the latest release, but are still an excellent reference!).
2013-03-20 17:45:41 +01:00
2013-12-26 20:32:01 +01:00
.. contents :: Topics
2013-10-05 00:34:39 +02:00
.. _content_organization:
2013-02-19 05:29:27 +01:00
Content Organization
++++++++++++++++++++++
2018-10-31 15:36:35 +01:00
The following section shows one of many possible ways to organize playbook content.
2013-02-19 05:29:27 +01:00
2014-12-01 20:24:41 +01:00
Your usage of Ansible should fit your needs, however, not ours, so feel free to modify this approach and organize as you see fit.
2017-06-06 23:39:48 +02:00
One crucial way to organize your playbook content is Ansible's "roles" organization feature, which is documented as part
2019-06-26 23:07:27 +02:00
of the main playbooks page. You should take the time to read and understand the roles documentation which is available here: :ref: `playbooks_reuse_roles` .
2013-04-13 00:26:17 +02:00
2013-10-05 00:34:39 +02:00
.. _directory_layout:
2013-02-19 05:29:27 +01:00
Directory Layout
`` ` ` ` ` ` ` ` ` ` ` ` ` ``
The top level of the directory would contain files and directories like so::
2016-10-27 09:39:46 +02:00
production # inventory file for production servers
staging # inventory file for staging environment
2013-02-19 05:29:27 +01:00
group_vars/
2018-05-16 18:19:21 +02:00
group1.yml # here we assign variables to particular groups
group2.yml
2013-02-19 05:29:27 +01:00
host_vars/
2018-05-16 18:19:21 +02:00
hostname1.yml # here we assign variables to particular systems
hostname2.yml
2013-04-13 00:26:17 +02:00
2014-12-01 20:24:41 +01:00
library/ # if any custom modules, put them here (optional)
2017-07-19 02:09:32 +02:00
module_utils/ # if any custom module_utils to support modules, put them here (optional)
2014-12-01 20:24:41 +01:00
filter_plugins/ # if any custom filter plugins, put them here (optional)
2013-04-13 00:26:17 +02:00
site.yml # master playbook
webservers.yml # playbook for webserver tier
dbservers.yml # playbook for dbserver tier
roles/
common/ # this hierarchy represents a "role"
tasks/ #
main.yml # <-- tasks file can include smaller files if warranted
2018-10-31 15:36:35 +01:00
handlers/ #
2013-04-13 00:26:17 +02:00
main.yml # <-- handlers file
templates/ # <-- files for use with the template resource
ntp.conf.j2 # <------- templates end in .j2
files/ #
bar.txt # <-- files for use with the copy resource
2013-05-25 16:51:59 +02:00
foo.sh # <-- script files for use with the script resource
2013-07-16 22:53:20 +02:00
vars/ #
main.yml # <-- variables associated with this role
2014-09-13 13:25:07 +02:00
defaults/ #
main.yml # <-- default lower priority variables for this role
2014-03-27 21:08:20 +01:00
meta/ #
main.yml # <-- role dependencies
2017-01-13 02:14:53 +01:00
library/ # roles can also include custom modules
2017-07-19 02:09:32 +02:00
module_utils/ # roles can also include custom module_utils
2017-01-13 02:14:53 +01:00
lookup_plugins/ # or other types of plugins, like lookup in this case
2013-04-13 00:26:17 +02:00
webtier/ # same kind of structure as "common" was above, done for the webtier role
monitoring/ # ""
2018-10-31 15:36:35 +01:00
fooapp/ # ""
2013-02-19 05:29:27 +01:00
2014-12-16 21:25:41 +01:00
.. note: If you find yourself having too many top level playbooks (for instance you have a playbook you wrote for a specific hotfix, etc), it may make sense to have a playbooks/ directory instead. This can be a good idea as you get larger. If you do this, configure your roles_path in ansible.cfg to find your roles location.
2014-12-01 20:24:41 +01:00
2016-09-01 21:36:27 +02:00
.. _alternative_directory_layout:
Alternative Directory Layout
2016-12-22 18:26:28 +01:00
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2016-09-01 21:36:27 +02:00
Alternatively you can put each inventory file with its `` group_vars `` /`` host_vars `` in a separate directory. This is particularly useful if your `` group_vars `` /`` host_vars `` don't have that much in common in different environments. The layout could look something like this::
inventories/
production/
2016-12-22 18:26:28 +01:00
hosts # inventory file for production servers
2016-09-01 21:36:27 +02:00
group_vars/
2018-05-16 18:19:21 +02:00
group1.yml # here we assign variables to particular groups
group2.yml
2016-09-01 21:36:27 +02:00
host_vars/
2018-05-16 18:19:21 +02:00
hostname1.yml # here we assign variables to particular systems
hostname2.yml
2016-09-01 21:36:27 +02:00
staging/
2016-12-22 18:26:28 +01:00
hosts # inventory file for staging environment
2016-09-01 21:36:27 +02:00
group_vars/
2018-05-16 18:19:21 +02:00
group1.yml # here we assign variables to particular groups
group2.yml
2016-09-01 21:36:27 +02:00
host_vars/
2018-05-16 18:19:21 +02:00
stagehost1.yml # here we assign variables to particular systems
stagehost2.yml
2016-09-01 21:36:27 +02:00
library/
2017-07-19 02:09:32 +02:00
module_utils/
2016-09-01 21:36:27 +02:00
filter_plugins/
site.yml
webservers.yml
dbservers.yml
roles/
common/
webtier/
monitoring/
fooapp/
This layout gives you more flexibility for larger environments, as well as a total separation of inventory variables between different environments. The downside is that it is harder to maintain, because there are more files.
2014-12-01 20:24:41 +01:00
.. _use_dynamic_inventory_with_clouds:
Use Dynamic Inventory With Clouds
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2019-06-26 23:07:27 +02:00
If you are using a cloud provider, you should not be managing your inventory in a static file. See :ref: `intro_dynamic_inventory` .
2014-12-01 20:24:41 +01:00
2014-12-11 09:10:44 +01:00
This does not just apply to clouds -- If you have another system maintaining a canonical list of systems
2014-12-01 20:24:41 +01:00
in your infrastructure, usage of dynamic inventory is a great idea in general.
2015-06-27 08:38:06 +02:00
.. _staging_vs_prod:
2013-10-05 00:34:39 +02:00
2015-06-29 20:13:17 +02:00
How to Differentiate Staging vs Production
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2014-12-01 20:24:41 +01:00
If managing static inventory, it is frequently asked how to differentiate different types of environments. The following example
shows a good way to do this. Similar methods of grouping could be adapted to dynamic inventory (for instance, consider applying the AWS
tag "environment:production", and you'll get a group of systems automatically discovered named "ec2_tag_environment_production".
2013-02-19 05:29:27 +01:00
2018-10-31 15:36:35 +01:00
Let's show a static inventory example though. Below, the *production* file contains the inventory of all of your production hosts.
2013-10-05 20:57:45 +02:00
It is suggested that you define groups based on purpose of the host (roles) and also geography or datacenter location (if applicable)::
2013-02-19 05:29:27 +01:00
# file: production
2019-03-11 20:12:14 +01:00
[atlanta_webservers]
2013-02-19 05:29:27 +01:00
www-atl-1.example.com
www-atl-2.example.com
2019-03-11 20:12:14 +01:00
[boston_webservers]
2013-02-19 05:29:27 +01:00
www-bos-1.example.com
www-bos-2.example.com
2019-03-11 20:12:14 +01:00
[atlanta_dbservers]
2013-02-19 05:29:27 +01:00
db-atl-1.example.com
db-atl-2.example.com
2019-03-11 20:12:14 +01:00
[boston_dbservers]
2013-02-19 05:29:27 +01:00
db-bos-1.example.com
# webservers in all geos
[webservers:children]
2019-03-11 20:12:14 +01:00
atlanta_webservers
boston_webservers
2013-02-19 05:29:27 +01:00
# dbservers in all geos
[dbservers:children]
2019-03-11 20:12:14 +01:00
atlanta_dbservers
boston_dbservers
2013-02-19 05:29:27 +01:00
# everything in the atlanta geo
[atlanta:children]
2019-03-11 20:12:14 +01:00
atlanta_webservers
atlanta_dbservers
2013-02-19 05:29:27 +01:00
# everything in the boston geo
[boston:children]
2019-03-11 20:12:14 +01:00
boston_webservers
boston_dbservers
2013-02-19 05:29:27 +01:00
2013-10-05 00:34:39 +02:00
.. _groups_and_hosts:
2013-02-19 05:29:27 +01:00
Group And Host Variables
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2014-12-01 20:24:41 +01:00
This section extends on the previous example.
Groups are nice for organization, but that's not all groups are good for. You can also assign variables to them! For instance, atlanta has its own NTP servers, so when setting up ntp.conf, we should use them. Let's set those now::
2013-02-19 05:29:27 +01:00
---
# file: group_vars/atlanta
ntp: ntp-atlanta.example.com
backup: backup-atlanta.example.com
2013-10-05 20:57:45 +02:00
Variables aren't just for geographic information either! Maybe the webservers have some configuration that doesn't make sense for the database servers::
2013-02-19 05:29:27 +01:00
---
# file: group_vars/webservers
apacheMaxRequestsPerChild: 3000
apacheMaxClients: 900
If we had any default values, or values that were universally true, we would put them in a file called group_vars/all::
---
# file: group_vars/all
ntp: ntp-boston.example.com
2018-10-31 15:36:35 +01:00
backup: backup-boston.example.com
2013-02-19 05:29:27 +01:00
We can define specific hardware variance in systems in a host_vars file, but avoid doing this unless you need to::
---
# file: host_vars/db-bos-1.example.com
foo_agent_port: 86
bar_agent_port: 99
2018-10-31 15:36:35 +01:00
Again, if we are using dynamic inventory sources, many dynamic groups are automatically created. So a tag like "class:webserver" would load in
variables from the file "group_vars/ec2_tag_class_webserver" automatically.
2014-12-01 20:24:41 +01:00
2013-10-05 00:34:39 +02:00
.. _split_by_role:
2013-04-21 21:33:51 +02:00
Top Level Playbooks Are Separated By Role
2013-02-19 05:29:27 +01:00
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2017-09-17 20:02:46 +02:00
In site.yml, we import a playbook that defines our entire infrastructure. This is a very short example, because it's just importing
some other playbooks::
2013-02-19 05:29:27 +01:00
---
# file: site.yml
2017-09-26 23:27:21 +02:00
- import_playbook: webservers.yml
- import_playbook: dbservers.yml
2013-02-19 05:29:27 +01:00
2017-09-17 20:02:46 +02:00
In a file like webservers.yml (also at the top level), we map the configuration of the webservers group to the roles performed by the webservers group::
2013-02-19 05:29:27 +01:00
---
# file: webservers.yml
- hosts: webservers
2013-04-13 00:26:17 +02:00
roles:
- common
- webtier
2013-02-19 05:29:27 +01:00
2014-12-01 20:24:41 +01:00
The idea here is that we can choose to configure our whole infrastructure by "running" site.yml or we could just choose to run a subset by running
webservers.yml. This is analogous to the "--limit" parameter to ansible but a little more explicit::
ansible-playbook site.yml --limit webservers
ansible-playbook webservers.yml
2013-10-05 00:34:39 +02:00
.. _role_organization:
2013-02-19 05:29:27 +01:00
Task And Handler Organization For A Role
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2013-07-16 22:53:20 +02:00
Below is an example tasks file that explains how a role works. Our common role here just sets up NTP, but it could do more if we wanted::
2013-02-19 05:29:27 +01:00
---
2013-04-13 00:26:17 +02:00
# file: roles/common/tasks/main.yml
2013-02-19 05:29:27 +01:00
- name: be sure ntp is installed
2018-02-03 12:28:22 +01:00
yum:
name: ntp
2018-10-31 15:36:35 +01:00
state: present
2013-02-19 05:29:27 +01:00
tags: ntp
- name: be sure ntp is configured
2018-02-03 12:28:22 +01:00
template:
src: ntp.conf.j2
dest: /etc/ntp.conf
2013-02-19 05:29:27 +01:00
notify:
- restart ntpd
tags: ntp
2012-08-01 06:52:48 +02:00
2013-02-23 19:13:26 +01:00
- name: be sure ntpd is running and enabled
2018-02-03 12:28:22 +01:00
service:
name: ntpd
state: started
enabled: yes
2013-02-19 05:29:27 +01:00
tags: ntp
Here is an example handlers file. As a review, handlers are only fired when certain tasks report changes, and are run at the end
of each play::
---
2013-04-13 00:26:17 +02:00
# file: roles/common/handlers/main.yml
2013-02-23 19:13:26 +01:00
- name: restart ntpd
2018-02-03 12:28:22 +01:00
service:
name: ntpd
state: restarted
2013-02-19 05:29:27 +01:00
2019-06-26 23:07:27 +02:00
See :ref: `playbooks_reuse_roles` for more information.
2013-10-05 20:57:45 +02:00
2013-10-05 00:34:39 +02:00
.. _organization_examples:
2013-02-19 05:29:27 +01:00
What This Organization Enables (Examples)
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2013-10-05 20:57:45 +02:00
Above we've shared our basic organizational structure.
2013-02-19 05:29:27 +01:00
Now what sort of use cases does this layout enable? Lots! If I want to reconfigure my whole infrastructure, it's just::
ansible-playbook -i production site.yml
2017-11-22 05:14:27 +01:00
To reconfigure NTP on everything::
2013-02-19 05:29:27 +01:00
ansible-playbook -i production site.yml --tags ntp
2017-11-22 05:14:27 +01:00
To reconfigure just my webservers::
2013-02-19 05:29:27 +01:00
ansible-playbook -i production webservers.yml
2017-11-22 05:14:27 +01:00
For just my webservers in Boston::
2013-02-19 05:29:27 +01:00
ansible-playbook -i production webservers.yml --limit boston
2018-02-02 13:21:21 +01:00
For just the first 10, and then the next 10::
2018-10-31 15:36:35 +01:00
2018-02-02 13:21:21 +01:00
ansible-playbook -i production webservers.yml --limit boston[0:9]
ansible-playbook -i production webservers.yml --limit boston[10:19]
2013-02-19 05:29:27 +01:00
2017-11-22 05:14:27 +01:00
And of course just basic ad-hoc stuff is also possible::
2013-02-19 05:29:27 +01:00
2014-03-10 22:49:45 +01:00
ansible boston -i production -m ping
ansible boston -i production -m command -a '/sbin/reboot'
2013-02-19 05:29:27 +01:00
2017-11-22 05:14:27 +01:00
And there are some useful commands to know::
2013-02-19 05:29:27 +01:00
# confirm what task names would be run if I ran this command and said "just ntp tasks"
ansible-playbook -i production webservers.yml --tags ntp --list-tasks
# confirm what hostnames might be communicated with if I said "limit to boston"
ansible-playbook -i production webservers.yml --limit boston --list-hosts
2013-10-05 00:34:39 +02:00
.. _dep_vs_config:
2013-02-19 05:29:27 +01:00
Deployment vs Configuration Organization
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2013-10-05 20:57:45 +02:00
The above setup models a typical configuration topology. When doing multi-tier deployments, there are going
2013-02-19 05:29:27 +01:00
to be some additional playbooks that hop between tiers to roll out an application. In this case, 'site.yml'
may be augmented by playbooks like 'deploy_exampledotcom.yml' but the general concepts can still apply.
2013-10-05 20:57:45 +02:00
Consider "playbooks" as a sports metaphor -- you don't have to just have one set of plays to use against your infrastructure
all the time -- you can have situational plays that you use at different times and for different purposes.
2013-02-19 05:29:27 +01:00
Ansible allows you to deploy and configure using the same tool, so you would likely reuse groups and just
2013-04-21 21:33:51 +02:00
keep the OS configuration in separate playbooks from the app deployment.
2013-02-19 05:29:27 +01:00
2015-06-27 08:38:06 +02:00
.. _staging_vs_production:
2013-10-05 00:34:39 +02:00
2015-06-27 08:38:06 +02:00
Staging vs Production
2015-07-17 06:45:33 +02:00
+++++++++++++++++++++
2013-02-19 05:29:27 +01:00
2015-06-27 08:38:06 +02:00
As also mentioned above, a good way to keep your staging (or testing) and production environments separate is to use a separate inventory file for staging and production. This way you pick with -i what you are targeting. Keeping them all in one file can lead to surprises!
2013-02-19 05:29:27 +01:00
2015-06-27 08:38:06 +02:00
Testing things in a staging environment before trying in production is always a great idea. Your environments need not be the same
2013-02-19 05:29:27 +01:00
size and you can use group variables to control the differences between those environments.
2013-10-05 00:34:39 +02:00
.. _rolling_update:
2013-02-19 05:29:27 +01:00
Rolling Updates
+++++++++++++++
Understand the 'serial' keyword. If updating a webserver farm you really want to use it to control how many machines you are
updating at once in the batch.
2019-06-26 23:07:27 +02:00
See :ref: `playbooks_delegation` .
2013-10-05 20:57:45 +02:00
2013-10-05 00:34:39 +02:00
.. _mention_the_state:
2013-02-19 05:29:27 +01:00
Always Mention The State
++++++++++++++++++++++++
The 'state' parameter is optional to a lot of modules. Whether 'state=present' or 'state=absent', it's always best to leave that
parameter in your playbooks to make it clear, especially as some modules support additional states.
2012-08-01 06:52:48 +02:00
2013-10-05 00:34:39 +02:00
.. _group_by_roles:
2012-05-13 17:56:09 +02:00
Group By Roles
++++++++++++++
2019-06-26 23:07:27 +02:00
We're somewhat repeating ourselves with this tip, but it's worth repeating. A system can be in multiple groups. See :ref: `intro_inventory` and :ref: `intro_patterns` . Having groups named after things like
2012-08-07 04:21:23 +02:00
*webservers* and *dbservers* is repeated in the examples because it's a very powerful concept.
2012-05-13 17:56:09 +02:00
This allows playbooks to target machines based on role, as well as to assign role specific variables
using the group variable system.
2019-06-26 23:07:27 +02:00
See :ref: `playbooks_reuse_roles` .
2013-10-05 20:57:45 +02:00
2013-10-05 00:34:39 +02:00
.. _os_variance:
2013-02-19 05:29:27 +01:00
Operating System and Distribution Variance
++++++++++++++++++++++++++++++++++++++++++
2014-12-01 20:24:41 +01:00
When dealing with a parameter that is different between two different operating systems, a great way to handle this is
2013-02-19 05:29:27 +01:00
by using the group_by module.
This makes a dynamic group of hosts matching certain criteria, even if that group is not defined in the inventory file::
---
2018-09-06 17:26:58 +02:00
- name: talk to all hosts just so we can learn about them
hosts: all
2018-02-03 12:28:22 +01:00
tasks:
2018-09-06 17:26:58 +02:00
- name: Classify hosts depending on their OS distribution
group_by:
key: os_{{ ansible_facts['distribution'] }}
2013-02-19 05:29:27 +01:00
2018-02-03 12:28:22 +01:00
# now just on the CentOS hosts...
2012-05-13 17:56:09 +02:00
2018-02-03 12:28:22 +01:00
- hosts: os_CentOS
gather_facts: False
tasks:
2013-02-19 05:29:27 +01:00
- # tasks that only happen on CentOS go here
2014-12-01 20:24:41 +01:00
This will throw all systems into a dynamic group based on the operating system name.
2013-07-16 22:53:20 +02:00
If group-specific settings are needed, this can also be done. For example::
2012-05-13 17:56:09 +02:00
2012-08-07 04:21:23 +02:00
---
2013-02-19 05:29:27 +01:00
# file: group_vars/all
asdf: 10
2012-05-13 17:56:09 +02:00
2013-02-19 05:29:27 +01:00
---
2014-12-01 20:24:41 +01:00
# file: group_vars/os_CentOS
2013-02-19 05:29:27 +01:00
asdf: 42
2012-05-13 17:56:09 +02:00
2013-07-16 22:53:20 +02:00
In the above example, CentOS machines get the value of '42' for asdf, but other machines get '10'.
2014-12-01 20:24:41 +01:00
This can be used not only to set variables, but also to apply certain roles to only certain systems.
2014-12-16 21:25:41 +01:00
Alternatively, if only variables are needed::
2014-12-01 20:24:41 +01:00
- hosts: all
tasks:
2018-09-06 17:26:58 +02:00
- name: Set OS distribution dependant variables
include_vars: "os_{{ ansible_facts['distribution'] }}.yml"
2018-02-03 12:28:22 +01:00
- debug:
var: asdf
2014-12-01 20:24:41 +01:00
2018-10-31 15:36:35 +01:00
This will pull in variables based on the OS name.
2012-05-13 17:56:09 +02:00
2013-10-05 00:34:39 +02:00
.. _ship_modules_with_playbooks:
2012-05-13 17:56:09 +02:00
2012-07-04 23:44:39 +02:00
Bundling Ansible Modules With Playbooks
+++++++++++++++++++++++++++++++++++++++
2017-07-19 02:09:32 +02:00
If a playbook has a :file: `./library` directory relative to its YAML file, this directory can be used to add ansible modules that will
2014-12-01 20:24:41 +01:00
automatically be in the ansible module path. This is a great way to keep modules that go with a playbook together. This is shown
in the directory structure example at the start of this section.
2013-02-19 05:29:27 +01:00
2013-10-05 00:34:39 +02:00
.. _whitespace:
2013-02-19 05:29:27 +01:00
Whitespace and Comments
+++++++++++++++++++++++
2012-07-04 23:44:39 +02:00
2013-02-19 05:29:27 +01:00
Generous use of whitespace to break things up, and use of comments (which start with '#'), is encouraged.
2013-10-05 00:34:39 +02:00
.. _name_tasks:
2013-02-19 05:29:27 +01:00
Always Name Tasks
+++++++++++++++++
2018-10-31 15:36:35 +01:00
It is possible to leave off the 'name' for a given task, though it is recommended to provide a description
2013-02-19 05:29:27 +01:00
about why something is being done instead. This name is shown when the playbook is run.
2013-10-05 00:34:39 +02:00
.. _keep_it_simple:
2013-02-19 05:29:27 +01:00
Keep It Simple
++++++++++++++
2012-05-13 17:56:09 +02:00
2012-08-07 04:21:23 +02:00
When you can do something simply, do something simply. Do not reach
to use every feature of Ansible together, all at once. Use what works
2014-01-22 06:05:21 +01:00
for you. For example, you will probably not need `` vars `` ,
`` vars_files `` , `` vars_prompt `` and `` --extra-vars `` all at once,
2012-08-07 04:21:23 +02:00
while also using an external inventory file.
2014-12-10 18:41:31 +01:00
If something feels complicated, it probably is, and may be a good opportunity to simplify things.
2014-12-01 20:24:41 +01:00
2013-10-05 00:34:39 +02:00
.. _version_control:
2013-02-19 05:29:27 +01:00
Version Control
+++++++++++++++
2012-08-07 04:21:23 +02:00
Use version control. Keep your playbooks and inventory file in git
(or another version control system), and commit when you make changes
to them. This way you have an audit trail describing when and why you
2013-07-16 22:53:20 +02:00
changed the rules that are automating your infrastructure.
2012-08-07 04:21:23 +02:00
2015-10-15 15:55:17 +02:00
.. _best_practices_for_variables_and_vaults:
Variables and Vaults
++++++++++++++++++++++++++++++++++++++++
For general maintenance, it is often easier to use `` grep `` , or similar tools, to find variables in your Ansible setup. Since vaults obscure these variables, it is best to work with a layer of indirection. When running a playbook, Ansible finds the variables in the unencrypted file and all sensitive variables come from the encrypted file.
2017-09-01 23:56:51 +02:00
A best practice approach for this is to start with a `` group_vars/ `` subdirectory named after the group. Inside of this subdirectory, create two files named `` vars `` and `` vault `` . Inside of the `` vars `` file, define all of the variables needed, including any sensitive ones. Next, copy all of the sensitive variables over to the `` vault `` file and prefix these variables with `` vault_ `` . You should adjust the variables in the `` vars `` file to point to the matching `` vault_ `` variables using jinja2 syntax, and ensure that the `` vault `` file is vault encrypted.
2015-10-15 15:55:17 +02:00
This best practice has no limit on the amount of variable and vault files or their names.
2012-05-13 17:56:09 +02:00
.. seealso ::
2018-03-14 20:44:21 +01:00
:ref: `yaml_syntax`
2012-05-13 17:56:09 +02:00
Learn about YAML syntax
2018-03-14 20:44:21 +01:00
:ref: `working_with_playbooks`
2012-05-13 17:56:09 +02:00
Review the basic playbook features
2018-03-14 20:44:21 +01:00
:ref: `all_modules`
2012-05-13 17:56:09 +02:00
Learn about available modules
2018-03-14 20:44:21 +01:00
:ref: `developing_modules`
2012-05-13 17:56:09 +02:00
Learn how to extend Ansible by writing your own modules
2018-03-14 20:44:21 +01:00
:ref: `intro_patterns`
2012-05-13 17:56:09 +02:00
Learn about how to select hosts
2017-02-12 09:01:43 +01:00
`GitHub examples directory <https://github.com/ansible/ansible-examples> `_
2012-05-13 17:56:09 +02:00
Complete playbook files from the github project source
2018-07-21 15:48:47 +02:00
`Mailing List <https://groups.google.com/group/ansible-project> `_
2012-05-13 17:56:09 +02:00
Questions? Help? Ideas? Stop by the list on Google Groups