mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Lighter refresh of the Best Practices page.
This commit is contained in:
parent
d47a833a8d
commit
8328bb7d63
1 changed files with 69 additions and 48 deletions
|
@ -6,15 +6,16 @@ Here are some tips for making the most of Ansible.
|
|||
Always Mention 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.
|
||||
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.
|
||||
|
||||
Group By Roles
|
||||
++++++++++++++
|
||||
|
||||
A system can be in multiple groups. See :doc:`patterns`. Having groups named after things like
|
||||
'webservers' and 'dbservers' is repeated in the examples because it's a very powerful concept.
|
||||
*webservers* and *dbservers* is repeated in the examples because it's a very powerful concept.
|
||||
|
||||
This allows playbooks to target machines based on role, as well as to assign role specific variables
|
||||
using the group variable system.
|
||||
|
@ -23,39 +24,33 @@ Directory Organization
|
|||
++++++++++++++++++++++
|
||||
|
||||
Playbooks should be organized like this::
|
||||
|
||||
(root of source control repository)
|
||||
|
||||
global_vars.yml # variables for all playbooks
|
||||
acme/ # each playbook has a directory
|
||||
# root of source control repository
|
||||
├── acme/
|
||||
│ ├── setup.yml
|
||||
│ └── stop.yml
|
||||
├── files/
|
||||
│ └── some_file_path_foo.conf
|
||||
├── handlers/
|
||||
│ └── main.yml
|
||||
├── tasks/
|
||||
│ ├── setup.yml
|
||||
│ └── stop.yml
|
||||
├── templates/
|
||||
│ ├── etc_acme_conf_acme.conf
|
||||
│ └── etc_other_conf_other.conf
|
||||
├── vars/
|
||||
│ └── main.yml
|
||||
└── global_vars.yml
|
||||
|
||||
setup.yml # playbook to manage the service
|
||||
stop.yml # playbook to halt the service (optional)
|
||||
|
||||
files/
|
||||
some_file_path_foo.conf
|
||||
|
||||
templates/
|
||||
etc_acme_conf_acme.conf
|
||||
etc_other_conf_other.conf
|
||||
|
||||
vars/
|
||||
main.yml # variables specific to this playbook
|
||||
|
||||
handlers/
|
||||
main.yml
|
||||
|
||||
tasks/
|
||||
setup.yml
|
||||
stop.yml
|
||||
|
||||
Any directories or files not needed can be omitted. Not all modules may require `vars` or `files` sections, though most
|
||||
will require `handlers`, `tasks`, and `templates`. To review what each of these sections do, see :doc:`playbooks` and :doc:`playbooks2`.
|
||||
Any directories or files not needed can be omitted. Not all modules
|
||||
may require 'vars' or 'files' sections, though most will require
|
||||
'handlers', 'tasks', and 'templates'. To review what each of
|
||||
these sections do, see :doc:`playbooks` and :doc:`playbooks2`.
|
||||
|
||||
The acme/setup.yml playbook would be as simple as::
|
||||
|
||||
----
|
||||
|
||||
---
|
||||
- hosts: webservers
|
||||
user: root
|
||||
|
||||
|
@ -73,7 +68,9 @@ when things change, and these are described in :doc:`playbooks`.
|
|||
|
||||
Including more than one setup file or more than one handlers file is of course legal.
|
||||
|
||||
Having playbooks be able to include other playbooks is coming in a future release. See Issue 538.
|
||||
Having playbooks be able to include other playbooks is coming in a
|
||||
future release. See `Issue 538
|
||||
<https://github.com/ansible/ansible/issues/538>`_.
|
||||
|
||||
Until then, to manage your entire site, simply execute all of your playbooks together, in the order desired.
|
||||
You don't have to do this though. It's fine to select sections of your infrastructure to manage at a single time.
|
||||
|
@ -82,29 +79,55 @@ You may wish to construct simple shell scripts to wrap calls to ansible-playbook
|
|||
Bundling Ansible Modules With Playbooks
|
||||
+++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
In version 0.5 and later, if a playbook has a "./library" directory relative to it's YAML file, this directory can be used to add ansible modules that will automatically be in the ansible module path. This is a great way to keep modules that
|
||||
go with a playbook together.
|
||||
.. versionadded:: 0.5
|
||||
|
||||
If a playbook has a "./library" directory relative to it's YAML file,
|
||||
this directory can be used to add ansible modules that will
|
||||
automatically be in the ansible module path. This is a great way to
|
||||
keep modules that go with a playbook together.
|
||||
|
||||
Miscellaneous Tips
|
||||
++++++++++++++++++
|
||||
|
||||
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 for you. For example, you should probably not need ``vars``, ``vars_files``, ``vars_prompt`` and ``--extra-vars`` all at once, while also using an external inventory file.
|
||||
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
|
||||
for you. For example, you should probably not need 'vars',
|
||||
'vars_files', 'vars_prompt' and '--extra-vars' all at once,
|
||||
while also using an external inventory file.
|
||||
|
||||
Optimize for readability. Whitespace between sections of YAML documents and in between tasks is strongly encouraged,
|
||||
as is usage of YAML comments, which start with "#". It is also useful to comment at the top of each file the purpose of the individual file and the author, including email address.
|
||||
Optimize for readability. Whitespace between sections of YAML
|
||||
documents and in between tasks is strongly encouraged, as is usage of
|
||||
YAML comments, which start with '#'. It is also useful to comment
|
||||
at the top of each file the purpose of the individual file and the
|
||||
author, including email address.
|
||||
|
||||
It is possible to leave off the "name" for a given task, though it is recommended to provide
|
||||
a descriptive comment about why something is being done instead.
|
||||
It is possible to leave off the 'name' for a given task, though it
|
||||
is recommended to provide a descriptive comment about why something is
|
||||
being done instead.
|
||||
|
||||
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 changed the rules automating your infrastructure.
|
||||
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
|
||||
changed the rules automating your infrastructure.
|
||||
|
||||
Resist the urge to write the same playbooks and configuration files for heterogeneous distributions. While lots of software packages claim to make this easy on you, the configuration files are often quite different, to the point where it would be easier to treat them as different playbooks. This is why, for example, Ansible has a seperate 'yum' and 'apt' module. Yum and apt have different capabilities, and we don't want to code for the least common denominator.
|
||||
Resist the urge to write the same playbooks and configuration files
|
||||
for heterogeneous distributions. While lots of software packages
|
||||
claim to make this easy on you, the configuration files are often
|
||||
quite different, to the point where it would be easier to treat them
|
||||
as different playbooks. This is why, for example, Ansible has a
|
||||
separate :ref:`yum` and :ref:`apt` module. Yum and apt have different
|
||||
capabilities, and we don't want to code for the least common
|
||||
denominator.
|
||||
|
||||
Use variables for user tunable settings versus having constants in the tasks file or templates, so that it is easy to reconfigure a playbook. Think about this as exposing the knobs to things you would like to tweak.
|
||||
Use variables for user tunable settings versus having constants in the
|
||||
tasks file or templates, so that it is easy to reconfigure a playbook.
|
||||
Think about this as exposing the knobs to things you would like to
|
||||
tweak.
|
||||
|
||||
Since a system can be in more than one group, if you have multiple datacenters or sites, consider putting systems into groups by role, but also different groups by geography. This allows you to assign different variables to different geographies.
|
||||
Since a system can be in more than one group, if you have multiple
|
||||
datacenters or sites, consider putting systems into groups by role,
|
||||
but also different groups by geography. This allows you to assign
|
||||
different variables to different geographies.
|
||||
|
||||
.. seealso::
|
||||
|
||||
|
@ -122,5 +145,3 @@ Since a system can be in more than one group, if you have multiple datacenters o
|
|||
Complete playbook files from the github project source
|
||||
`Mailing List <http://groups.google.com/group/ansible-project>`_
|
||||
Questions? Help? Ideas? Stop by the list on Google Groups
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue