mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-10-03 23:14:02 -07:00
Remove extra "latest/" subdirectory in docs build structure.
This commit is contained in:
parent
d22529bad0
commit
0cd09dd54f
79 changed files with 1 additions and 1 deletions
119
docsite/rst/YAMLSyntax.rst
Normal file
119
docsite/rst/YAMLSyntax.rst
Normal file
|
@ -0,0 +1,119 @@
|
|||
YAML Syntax
|
||||
===========
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
This page provides a basic overview of correct YAML syntax, which is how Ansible
|
||||
playbooks (our configuration management language) are expressed.
|
||||
|
||||
We use YAML because it is easier for humans to read and write than other common
|
||||
data formats like XML or JSON. Further, there are libraries available in most
|
||||
programming languages for working with YAML.
|
||||
|
||||
You may also wish to read :doc:`playbooks` at the same time to see how this
|
||||
is used in practice.
|
||||
|
||||
|
||||
YAML Basics
|
||||
-----------
|
||||
|
||||
For Ansible, nearly every YAML file starts with a list.
|
||||
Each item in the list is a list of key/value pairs, commonly
|
||||
called a "hash" or a "dictionary". So, we need to know how
|
||||
to write lists and dictionaries in YAML.
|
||||
|
||||
There's another small quirk to YAML. All YAML files (regardless of their association with
|
||||
Ansible or not) should begin with ``---``. This is part of the YAML
|
||||
format and indicates the start of a document.
|
||||
|
||||
All members of a list are lines beginning at the same indentation level starting
|
||||
with a ``-`` (dash) character::
|
||||
|
||||
---
|
||||
# A list of tasty fruits
|
||||
- Apple
|
||||
- Orange
|
||||
- Strawberry
|
||||
- Mango
|
||||
|
||||
A dictionary is represented in a simple ``key:`` and ``value`` form::
|
||||
|
||||
---
|
||||
# An employee record
|
||||
name: Example Developer
|
||||
job: Developer
|
||||
skill: Elite
|
||||
|
||||
Dictionaries can also be represented in an abbreviated form if you really want to::
|
||||
|
||||
---
|
||||
# An employee record
|
||||
{name: Example Developer, job: Developer, skill: Elite}
|
||||
|
||||
.. _truthiness:
|
||||
|
||||
Ansible doesn't really use these too much, but you can also specify a
|
||||
boolean value (true/false) in several forms::
|
||||
|
||||
---
|
||||
create_key: yes
|
||||
needs_agent: no
|
||||
knows_oop: True
|
||||
likes_emacs: TRUE
|
||||
uses_cvs: false
|
||||
|
||||
Let's combine what we learned so far in an arbitrary YAML example. This really
|
||||
has nothing to do with Ansible, but will give you a feel for the format::
|
||||
|
||||
---
|
||||
# An employee record
|
||||
name: Example Developer
|
||||
job: Developer
|
||||
skill: Elite
|
||||
employed: True
|
||||
foods:
|
||||
- Apple
|
||||
- Orange
|
||||
- Strawberry
|
||||
- Mango
|
||||
languages:
|
||||
ruby: Elite
|
||||
python: Elite
|
||||
dotnet: Lame
|
||||
|
||||
That's all you really need to know about YAML to start writing
|
||||
`Ansible` playbooks.
|
||||
|
||||
Gotchas
|
||||
-------
|
||||
|
||||
While YAML is generally friendly, the following is going to result in a YAML syntax error:
|
||||
|
||||
foo: somebody said I should put a colon here: so I did
|
||||
|
||||
You will want to quote any hash values using colons, like so:
|
||||
|
||||
foo: "somebody said I should put a colon here: so I did"
|
||||
|
||||
And then the colon will be preserved.
|
||||
|
||||
Further, Ansible uses "{{ var }}" for variables. If a value after a colon starts
|
||||
with a "{", YAML will think it is a dictionary, so you must quote it, like so::
|
||||
|
||||
foo: "{{ variable }}"
|
||||
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`playbooks`
|
||||
Learn what playbooks can do and how to write/run them.
|
||||
`YAMLLint <http://yamllint.com/>`_
|
||||
YAML Lint (online) helps you debug YAML syntax if you are having problems
|
||||
`Github examples directory <https://github.com/ansible/ansible/tree/devel/examples/playbooks>`_
|
||||
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
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
107
docsite/rst/developing_api.rst
Normal file
107
docsite/rst/developing_api.rst
Normal file
|
@ -0,0 +1,107 @@
|
|||
Python API
|
||||
==========
|
||||
|
||||
There are several interesting ways to use Ansible from an API perspective. You can use
|
||||
the Ansible python API to control nodes, you can extend Ansible to respond to various python events, you can
|
||||
write various plugins, and you can plug in inventory data from external data sources. This document
|
||||
covers the Runner and Playbook API at a basic level.
|
||||
|
||||
If you are looking to use Ansible programmatically from something other than Python, trigger events asynchronously,
|
||||
or have access control and logging demands, take a look at `AnsibleWorks AWX <http://ansibleworks.com/ansibleworks-awx>`_
|
||||
as it has a very nice REST API that provides all of these things at a higher level.
|
||||
|
||||
Ansible is written in its own API so you have a considerable amount of power across the board.
|
||||
This chapter discusses the Python API.
|
||||
|
||||
.. contents:: `Table of contents`
|
||||
:depth: 2
|
||||
|
||||
.. _python_api:
|
||||
|
||||
Python API
|
||||
----------
|
||||
|
||||
The Python API is very powerful, and is how the ansible CLI and ansible-playbook
|
||||
are implemented.
|
||||
|
||||
It's pretty simple::
|
||||
|
||||
import ansible.runner
|
||||
|
||||
runner = ansible.runner.Runner(
|
||||
module_name='ping',
|
||||
module_args='',
|
||||
pattern='web*',
|
||||
forks=10
|
||||
)
|
||||
datastructure = runner.run()
|
||||
|
||||
The run method returns results per host, grouped by whether they
|
||||
could be contacted or not. Return types are module specific, as
|
||||
expressed in the 'ansible-modules' documentation.::
|
||||
|
||||
{
|
||||
"dark" : {
|
||||
"web1.example.com" : "failure message"
|
||||
},
|
||||
"contacted" : {
|
||||
"web2.example.com" : 1
|
||||
}
|
||||
}
|
||||
|
||||
A module can return any type of JSON data it wants, so Ansible can
|
||||
be used as a framework to rapidly build powerful applications and scripts.
|
||||
|
||||
.. _detailed_api_example:
|
||||
|
||||
Detailed API Example
|
||||
````````````````````
|
||||
|
||||
The following script prints out the uptime information for all hosts::
|
||||
|
||||
#!/usr/bin/python
|
||||
|
||||
import ansible.runner
|
||||
import sys
|
||||
|
||||
# construct the ansible runner and execute on all hosts
|
||||
results = ansible.runner.Runner(
|
||||
pattern='*', forks=10,
|
||||
module_name='command', module_args='/usr/bin/uptime',
|
||||
).run()
|
||||
|
||||
if results is None:
|
||||
print "No hosts found"
|
||||
sys.exit(1)
|
||||
|
||||
print "UP ***********"
|
||||
for (hostname, result) in results['contacted'].items():
|
||||
if not 'failed' in result:
|
||||
print "%s >>> %s" % (hostname, result['stdout'])
|
||||
|
||||
print "FAILED *******"
|
||||
for (hostname, result) in results['contacted'].items():
|
||||
if 'failed' in result:
|
||||
print "%s >>> %s" % (hostname, result['msg'])
|
||||
|
||||
print "DOWN *********"
|
||||
for (hostname, result) in results['dark'].items():
|
||||
print "%s >>> %s" % (hostname, result)
|
||||
|
||||
Advanced programmers may also wish to read the source to ansible itself, for
|
||||
it uses the Runner() API (with all available options) to implement the
|
||||
command line tools ``ansible`` and ``ansible-playbook``.
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`developing_inventory`
|
||||
Developing dynamic inventory integrations
|
||||
:doc:`developing_modules`
|
||||
How to develop modules
|
||||
:doc:`developing_plugins`
|
||||
How to develop plugins
|
||||
`Development Mailing List <http://groups.google.com/group/ansible-devel>`_
|
||||
Mailing list for development topics
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
99
docsite/rst/developing_inventory.rst
Normal file
99
docsite/rst/developing_inventory.rst
Normal file
|
@ -0,0 +1,99 @@
|
|||
Developing Dynamic Inventory Sources
|
||||
====================================
|
||||
|
||||
.. contents:: `Table of contents`
|
||||
:depth: 2
|
||||
|
||||
As described in `intro_inventory_dynamic`, ansible can pull inventory information from dynamic sources, including cloud sources.
|
||||
|
||||
How do we write a new one?
|
||||
|
||||
Simple! We just create a script or program that can return JSON in the right format when fed the proper arguments.
|
||||
You can do this in any language.
|
||||
|
||||
.. _inventory_script_conventions:
|
||||
|
||||
Script Conventions
|
||||
``````````````````
|
||||
|
||||
When the external node script is called with the single argument '--list', the script must return a JSON hash/dictionary of all the groups to be managed. Each group's value should be either a hash/dictionary containing a list of each host/IP, potential child groups, and potential group variables, or simply a list of host/IP addresses, like so::
|
||||
|
||||
{
|
||||
"databases" : {
|
||||
"hosts" : [ "host1.example.com", "host2.example.com" ],
|
||||
"vars" : {
|
||||
"a" : true
|
||||
}
|
||||
},
|
||||
"webservers" : [ "host2.example.com", "host3.example.com" ],
|
||||
"atlanta" : {
|
||||
"hosts" : [ "host1.example.com", "host4.example.com", "host5.example.com" ],
|
||||
"vars" : {
|
||||
"b" : false
|
||||
},
|
||||
"children": [ "marietta", "5points" ],
|
||||
},
|
||||
"marietta" : [ "host6.example.com" ],
|
||||
"5points" : [ "host7.example.com" ]
|
||||
}
|
||||
|
||||
.. versionadded:: 1.0
|
||||
|
||||
Before version 1.0, each group could only have a list of hostnames/IP addresses, like the webservers, marietta, and 5points groups above.
|
||||
|
||||
When called with the arguments '--host <hostname>' (where <hostname> is a host from above), the script must return either an empty JSON
|
||||
hash/dictionary, or a hash/dictionary of variables to make available to templates and playbooks. Returning variables is optional,
|
||||
if the script does not wish to do this, returning an empty hash/dictionary is the way to go::
|
||||
|
||||
{
|
||||
"favcolor" : "red",
|
||||
"ntpserver" : "wolf.example.com",
|
||||
"monitoring" : "pack.example.com"
|
||||
}
|
||||
|
||||
.. _inventory_script_tuning:
|
||||
|
||||
Tuning the External Inventory Script
|
||||
````````````````````````````````````
|
||||
|
||||
.. versionadded:: 1.3
|
||||
|
||||
The stock inventory script system detailed above works for all versions of Ansible, but calling
|
||||
'--host' for every host can be rather expensive, especially if it involves expensive API calls to
|
||||
a remote subsystemm. In Ansible
|
||||
1.3 or later, if the inventory script returns a top level element called "_meta", it is possible
|
||||
to return all of the host variables in one inventory script call. When this meta element contains
|
||||
a value for "hostvars", the inventory script will not be invoked with "--host" for each host. This
|
||||
results in a significant performance increase for large numbers of hosts, and also makes client
|
||||
side caching easier to implement for the inventory script.
|
||||
|
||||
The data to be added to the top level JSON dictionary looks like this::
|
||||
|
||||
{
|
||||
|
||||
# results of inventory script as above go here
|
||||
# ...
|
||||
|
||||
"_meta" : {
|
||||
"hostvars" : {
|
||||
"moocow.example.com" : { "asdf" : 1234 },
|
||||
"llama.example.com" : { "asdf" : 5678 },
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
:doc:`developing_api`
|
||||
Python API to Playbooks and Ad Hoc Task Execution
|
||||
:doc:`developing_modules`
|
||||
How to develop modules
|
||||
:doc:`developing_plugins`
|
||||
How to develop plugins
|
||||
`AnsibleWorks AWX <http://ansibleworks.com/ansibleworks-awx`_
|
||||
REST API endpoint and GUI for Ansible, syncs with dynamic inventory
|
||||
`Development Mailing List <http://groups.google.com/group/ansible-devel>`_
|
||||
Mailing list for development topics
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
||||
|
477
docsite/rst/developing_modules.rst
Normal file
477
docsite/rst/developing_modules.rst
Normal file
|
@ -0,0 +1,477 @@
|
|||
Developing Modules
|
||||
==================
|
||||
|
||||
Ansible modules are reusable units of magic that can be used by the Ansible API,
|
||||
or by the `ansible` or `ansible-playbook` programs.
|
||||
|
||||
See :doc:`modules` for a list of various ones developed in core.
|
||||
|
||||
Modules can be written in any language and are found in the path specified
|
||||
by `ANSIBLE_LIBRARY` or the ``--module-path`` command line option.
|
||||
|
||||
Should you develop an interesting Ansible module, consider sending a pull request to the
|
||||
`github project <http://github.com/ansible/ansible>`_ to see about getting your module
|
||||
included in the core project.
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
.. _module_dev_tutorial:
|
||||
|
||||
Tutorial
|
||||
````````
|
||||
|
||||
Let's build a very-basic module to get and set the system time. For starters, let's build
|
||||
a module that just outputs the current time.
|
||||
|
||||
We are going to use Python here but any language is possible. Only File I/O and outputting to standard
|
||||
out are required. So, bash, C++, clojure, Python, Ruby, whatever you want
|
||||
is fine.
|
||||
|
||||
Now Python Ansible modules contain some extremely powerful shortcuts (that all the core modules use)
|
||||
but first we are going to build a module the very hard way. The reason we do this is because modules
|
||||
written in any language OTHER than Python are going to have to do exactly this. We'll show the easy
|
||||
way later.
|
||||
|
||||
So, here's an example. You would never really need to build a module to set the system time,
|
||||
the 'command' module could already be used to do this. Though we're going to make one.
|
||||
|
||||
Reading the modules that come with ansible (linked above) is a great way to learn how to write
|
||||
modules. Keep in mind, though, that some modules in ansible's source tree are internalisms,
|
||||
so look at `service` or `yum`, and don't stare too close into things like `async_wrapper` or
|
||||
you'll turn to stone. Nobody ever executes async_wrapper directly.
|
||||
|
||||
Ok, let's get going with an example. We'll use Python. For starters, save this as a file named `time`::
|
||||
|
||||
#!/usr/bin/python
|
||||
|
||||
import datetime
|
||||
import json
|
||||
|
||||
date = str(datetime.datetime.now())
|
||||
print json.dumps({
|
||||
"time" : date
|
||||
})
|
||||
|
||||
.. _module_testing:
|
||||
|
||||
Testing Modules
|
||||
```````````````
|
||||
|
||||
There's a useful test script in the source checkout for ansible::
|
||||
|
||||
git clone git@github.com:ansible/ansible.git
|
||||
chmod +x ansible/hacking/test-module
|
||||
|
||||
Let's run the script you just wrote with that::
|
||||
|
||||
ansible/hacking/test-module -m ./time
|
||||
|
||||
You should see output that looks something like this::
|
||||
|
||||
{u'time': u'2012-03-14 22:13:48.539183'}
|
||||
|
||||
If you did not, you might have a typo in your module, so recheck it and try again.
|
||||
|
||||
.. _reading_input:
|
||||
|
||||
Reading Input
|
||||
`````````````
|
||||
|
||||
Let's modify the module to allow setting the current time. We'll do this by seeing
|
||||
if a key value pair in the form `time=<string>` is passed in to the module.
|
||||
|
||||
Ansible internally saves arguments to an arguments file. So we must read the file
|
||||
and parse it. The arguments file is just a string, so any form of arguments are legal.
|
||||
Here we'll do some basic parsing to treat the input as key=value.
|
||||
|
||||
The example usage we are trying to achieve to set the time is::
|
||||
|
||||
time time="March 14 22:10"
|
||||
|
||||
If no time parameter is set, we'll just leave the time as is and return the current time.
|
||||
|
||||
.. note:
|
||||
This is obviously an unrealistic idea for a module. You'd most likely just
|
||||
use the shell module. However, it probably makes a decent tutorial.
|
||||
|
||||
Let's look at the code. Read the comments as we'll explain as we go. Note that this
|
||||
is highly verbose because it's intended as an educational example. You can write modules
|
||||
a lot shorter than this::
|
||||
|
||||
#!/usr/bin/python
|
||||
|
||||
# import some python modules that we'll use. These are all
|
||||
# available in Python's core
|
||||
|
||||
import datetime
|
||||
import sys
|
||||
import json
|
||||
import os
|
||||
import shlex
|
||||
|
||||
# read the argument string from the arguments file
|
||||
args_file = sys.argv[1]
|
||||
args_data = file(args_file).read()
|
||||
|
||||
# for this module, we're going to do key=value style arguments
|
||||
# this is up to each module to decide what it wants, but all
|
||||
# core modules besides 'command' and 'shell' take key=value
|
||||
# so this is highly recommended
|
||||
|
||||
arguments = shlex.split(args_data)
|
||||
for arg in arguments:
|
||||
|
||||
# ignore any arguments without an equals in it
|
||||
if arg.find("=") != -1:
|
||||
|
||||
(key, value) = arg.split("=")
|
||||
|
||||
# if setting the time, the key 'time'
|
||||
# will contain the value we want to set the time to
|
||||
|
||||
if key == "time":
|
||||
|
||||
# now we'll affect the change. Many modules
|
||||
# will strive to be 'idempotent', meaning they
|
||||
# will only make changes when the desired state
|
||||
# expressed to the module does not match
|
||||
# the current state. Look at 'service'
|
||||
# or 'yum' in the main git tree for an example
|
||||
# of how that might look.
|
||||
|
||||
rc = os.system("date -s \"%s\"" % value)
|
||||
|
||||
# always handle all possible errors
|
||||
#
|
||||
# when returning a failure, include 'failed'
|
||||
# in the return data, and explain the failure
|
||||
# in 'msg'. Both of these conventions are
|
||||
# required however additional keys and values
|
||||
# can be added.
|
||||
|
||||
if rc != 0:
|
||||
print json.dumps({
|
||||
"failed" : True,
|
||||
"msg" : "failed setting the time"
|
||||
})
|
||||
sys.exit(1)
|
||||
|
||||
# when things do not fail, we do not
|
||||
# have any restrictions on what kinds of
|
||||
# data are returned, but it's always a
|
||||
# good idea to include whether or not
|
||||
# a change was made, as that will allow
|
||||
# notifiers to be used in playbooks.
|
||||
|
||||
date = str(datetime.datetime.now())
|
||||
print json.dumps({
|
||||
"time" : date,
|
||||
"changed" : True
|
||||
})
|
||||
sys.exit(0)
|
||||
|
||||
# if no parameters are sent, the module may or
|
||||
# may not error out, this one will just
|
||||
# return the time
|
||||
|
||||
date = str(datetime.datetime.now())
|
||||
print json.dumps({
|
||||
"time" : date
|
||||
})
|
||||
|
||||
Let's test that module::
|
||||
|
||||
ansible/hacking/test-module -m ./time -a time=\"March 14 12:23\"
|
||||
|
||||
This should return something like::
|
||||
|
||||
{"changed": true, "time": "2012-03-14 12:23:00.000307"}
|
||||
|
||||
.. _module_provided_facts:
|
||||
|
||||
Module Provided 'Facts'
|
||||
```````````````````````
|
||||
|
||||
The 'setup' module that ships with Ansible provides many variables about a system that can be used in playbooks
|
||||
and templates. However, it's possible to also add your own facts without modifying the system module. To do
|
||||
this, just have the module return a `ansible_facts` key, like so, along with other return data::
|
||||
|
||||
{
|
||||
"changed" : True,
|
||||
"rc" : 5,
|
||||
"ansible_facts" : {
|
||||
"leptons" : 5000
|
||||
"colors" : {
|
||||
"red" : "FF0000",
|
||||
"white" : "FFFFFF"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
These 'facts' will be available to all statements called after that module (but not before) in the playbook.
|
||||
A good idea might be make a module called 'site_facts' and always call it at the top of each playbook, though
|
||||
we're always open to improving the selection of core facts in Ansible as well.
|
||||
|
||||
.. _common_module_boilerplate:
|
||||
|
||||
Common Module Boilerplate
|
||||
`````````````````````````
|
||||
|
||||
As mentioned, if you are writing a module in Python, there are some very powerful shortcuts you can use.
|
||||
Modules are still transferred as one file, but an arguments file is no longer needed, so these are not
|
||||
only shorter in terms of code, they are actually FASTER in terms of execution time.
|
||||
|
||||
Rather than mention these here, the best way to learn is to read some of the `source of the modules <https://github.com/ansible/ansible/tree/devel/library>`_ that come with Ansible.
|
||||
|
||||
The 'group' and 'user' modules are reasonably non-trivial and showcase what this looks like.
|
||||
|
||||
Key parts include always ending the module file with::
|
||||
|
||||
# include magic from lib/ansible/module_common.py
|
||||
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
||||
main()
|
||||
|
||||
And instantiating the module class like::
|
||||
|
||||
module = AnsibleModule(
|
||||
argument_spec = dict(
|
||||
state = dict(default='present', choices=['present', 'absent']),
|
||||
name = dict(required=True),
|
||||
enabled = dict(required=True, choices=BOOLEANS),
|
||||
something = dict(aliases=['whatever'])
|
||||
)
|
||||
)
|
||||
|
||||
The AnsibleModule provides lots of common code for handling returns, parses your arguments
|
||||
for you, and allows you to check inputs.
|
||||
|
||||
Successful returns are made like this::
|
||||
|
||||
module.exit_json(changed=True, something_else=12345)
|
||||
|
||||
And failures are just as simple (where 'msg' is a required parameter to explain the error)::
|
||||
|
||||
module.fail_json(msg="Something fatal happened")
|
||||
|
||||
There are also other useful functions in the module class, such as module.md5(path). See
|
||||
lib/ansible/module_common.py in the source checkout for implementation details.
|
||||
|
||||
Again, modules developed this way are best tested with the hacking/test-module script in the git
|
||||
source checkout. Because of the magic involved, this is really the only way the scripts
|
||||
can function outside of Ansible.
|
||||
|
||||
If submitting a module to ansible's core code, which we encourage, use of the AnsibleModule
|
||||
class is required.
|
||||
|
||||
.. _developing_for_check_mode:
|
||||
|
||||
Check Mode
|
||||
``````````
|
||||
.. versionadded:: 1.1
|
||||
|
||||
Modules may optionally support check mode. If the user runs Ansible in check
|
||||
mode, the module should try to predict whether changes will occur.
|
||||
|
||||
For your module to support check mode, you must pass ``supports_check_mode=True``
|
||||
when instantiating the AnsibleModule object. The AnsibleModule.check_mode attribute
|
||||
will evaluate to True when check mode is enabled. For example::
|
||||
|
||||
module = AnsibleModule(
|
||||
argument_spec = dict(...),
|
||||
supports_check_mode=True
|
||||
)
|
||||
|
||||
if module.check_mode:
|
||||
# Check if any changes would be made by don't actually make those changes
|
||||
module.exit_json(changed=check_if_system_state_would_be_changed())
|
||||
|
||||
Remember that, as module developer, you are responsible for ensuring that no
|
||||
system state is altered when the user enables check mode.
|
||||
|
||||
If your module does not support check mode, when the user runs Ansible in check
|
||||
mode, your module will simply be skipped.
|
||||
|
||||
.. _module_dev_pitfalls:
|
||||
|
||||
Common Pitfalls
|
||||
```````````````
|
||||
|
||||
You should also never do this in a module::
|
||||
|
||||
print "some status message"
|
||||
|
||||
Because the output is supposed to be valid JSON. Except that's not quite true,
|
||||
but we'll get to that later.
|
||||
|
||||
Modules must not output anything on standard error, because the system will merge
|
||||
standard out with standard error and prevent the JSON from parsing. Capturing standard
|
||||
error and returning it as a variable in the JSON on standard out is fine, and is, in fact,
|
||||
how the command module is implemented.
|
||||
|
||||
If a module returns stderr or otherwise fails to produce valid JSON, the actual output
|
||||
will still be shown in Ansible, but the command will not succeed.
|
||||
|
||||
Always use the hacking/test-module script when developing modules and it will warn
|
||||
you about these kind of things.
|
||||
|
||||
.. _module_dev_conventions:
|
||||
|
||||
Conventions/Recommendations
|
||||
```````````````````````````
|
||||
|
||||
As a reminder from the example code above, here are some basic conventions
|
||||
and guidelines:
|
||||
|
||||
* If the module is addressing an object, the parameter for that object should be called 'name' whenever possible, or accept 'name' as an alias.
|
||||
|
||||
* If you have a company module that returns facts specific to your installations, a good name for this module is `site_facts`.
|
||||
|
||||
* Modules accepting boolean status should generally accept 'yes', 'no', 'true', 'false', or anything else a user may likely throw at them. The AnsibleModule common code supports this with "choices=BOOLEANS" and a module.boolean(value) casting function.
|
||||
|
||||
* Include a minimum of dependencies if possible. If there are dependencies, document them at the top of the module file, and have the module raise JSON error messages when the import fails.
|
||||
|
||||
* Modules must be self contained in one file to be auto-transferred by ansible.
|
||||
|
||||
* If packaging modules in an RPM, they only need to be installed on the control machine and should be dropped into /usr/share/ansible. This is entirely optional and up to you.
|
||||
|
||||
* Modules should return JSON or key=value results all on one line. JSON is best if you can do JSON. All return types must be hashes (dictionaries) although they can be nested. Lists or simple scalar values are not supported, though they can be trivially contained inside a dictionary.
|
||||
|
||||
* In the event of failure, a key of 'failed' should be included, along with a string explanation in 'msg'. Modules that raise tracebacks (stacktraces) are generally considered 'poor' modules, though Ansible can deal with these returns and will automatically convert anything unparseable into a failed result. If you are using the AnsibleModule common Python code, the 'failed' element will be included for you automatically when you call 'fail_json'.
|
||||
|
||||
* Return codes from modules are not actually not signficant, but continue on with 0=success and non-zero=failure for reasons of future proofing.
|
||||
|
||||
* As results from many hosts will be aggregated at once, modules should return only relevant output. Returning the entire contents of a log file is generally bad form.
|
||||
|
||||
.. _module_dev_shorthand:
|
||||
|
||||
Shorthand Vs JSON
|
||||
`````````````````
|
||||
|
||||
To make it easier to write modules in bash and in cases where a JSON
|
||||
module might not be available, it is acceptable for a module to return
|
||||
key=value output all on one line, like this. The Ansible parser
|
||||
will know what to do::
|
||||
|
||||
somekey=1 somevalue=2 rc=3 favcolor=red
|
||||
|
||||
If you're writing a module in Python or Ruby or whatever, though, returning
|
||||
JSON is probably the simplest way to go.
|
||||
|
||||
.. _module_documenting:
|
||||
|
||||
Documenting Your Module
|
||||
```````````````````````
|
||||
|
||||
All modules included in the CORE distribution must have a
|
||||
``DOCUMENTATION`` string. This string MUST be a valid YAML document
|
||||
which conforms to the schema defined below. You may find it easier to
|
||||
start writing your ``DOCUMENTATION`` string in an editor with YAML
|
||||
syntax highlighting before you include it in your Python file.
|
||||
|
||||
.. _module_doc_example:
|
||||
|
||||
Example
|
||||
+++++++
|
||||
|
||||
To print a basic documentation string, run ``./hacking/module_formatter.py -G``.
|
||||
|
||||
You can copy it into your module and use it as a starting point
|
||||
when writing your own docs.
|
||||
|
||||
Include it in your module file like this::
|
||||
|
||||
#!/usr/bin/env python
|
||||
# Copyright header....
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: modulename
|
||||
short_description: This is a sentence describing the module
|
||||
# ... snip ...
|
||||
'''
|
||||
|
||||
The ``description``, and ``notes``
|
||||
support formatting in some of the output formats (e.g. ``rst``, ``man``).
|
||||
These formatting functions are ``U()``, ``M()``, ``I()``, and ``C()``
|
||||
for URL, module, italic, and constant-width respectively. It is suggested
|
||||
to use ``C()`` for file and option names, and ``I()`` when referencing
|
||||
parameters; module names should be specifies as ``M(module)``.
|
||||
|
||||
Examples (which typically contain colons, quotes, etc.) are difficult
|
||||
to format with YAML, so these must be
|
||||
written in plain text in an ``EXAMPLES`` string within the module
|
||||
like this::
|
||||
|
||||
EXAMPLES = '''
|
||||
- action: modulename opt1=arg1 opt2=arg2
|
||||
'''
|
||||
|
||||
The ``module_formatter.py`` script and ``ansible-doc(1)`` append the
|
||||
``EXAMPLES`` blob after any existing (deprecated) ``examples`` you may have in the
|
||||
YAML ``DOCUMENTATION`` string.
|
||||
|
||||
.. _module_dev_testing:
|
||||
|
||||
Building & Testing
|
||||
++++++++++++++++++
|
||||
|
||||
Put your completed module file into the 'library' directory and then
|
||||
run the command: ``make webdocs``. The new 'modules.html' file will be
|
||||
built and appear in the 'docsite/' directory.
|
||||
|
||||
You can also test-build your docs one-by-one using the
|
||||
``module_formatter.py`` script:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ ./hacking/module_formatter.py -t man -M library/ -m git > ansible-git.1
|
||||
$ man ./ansible-git.1
|
||||
|
||||
This will build a manpage for the git module, and look in the
|
||||
'library/' directory for the module source. To see all the other
|
||||
output formats available:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ ./hacking/module_formatter.py -t --help
|
||||
|
||||
|
||||
.. tip::
|
||||
|
||||
If you're having a problem with the syntax of your YAML you can
|
||||
validate it on the `YAML Lint <http://www.yamllint.com/>`_ website.
|
||||
|
||||
.. tip::
|
||||
|
||||
You can use ANSIBLE_KEEP_REMOTE_FILES=1 to prevent ansible from
|
||||
deleting the remote files so you can debug your module.
|
||||
|
||||
.. _module_contribution:
|
||||
|
||||
Getting Your Module Into Core
|
||||
`````````````````````````````
|
||||
|
||||
High-quality modules with minimal dependencies
|
||||
can be included in the core, but core modules (just due to the programming
|
||||
preferences of the developers) will need to be implemented in Python and use
|
||||
the AnsibleModule common code, and should generally use consistent arguments with the rest of
|
||||
the program. Stop by the mailing list to inquire about requirements if you like, and submit
|
||||
a github pull request to the main project.
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`modules`
|
||||
Learn about available modules
|
||||
:doc:`developing_plugins`
|
||||
Learn about developing plugins
|
||||
:doc:`developing_api`
|
||||
Learn about the Python API for playbook and task execution
|
||||
`Github modules directory <https://github.com/ansible/ansible/tree/devel/library>`_
|
||||
Browse source of core modules
|
||||
`Mailing List <http://groups.google.com/group/ansible-devel>`_
|
||||
Development mailing list
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
||||
|
128
docsite/rst/developing_plugins.rst
Normal file
128
docsite/rst/developing_plugins.rst
Normal file
|
@ -0,0 +1,128 @@
|
|||
Developing Plugins
|
||||
==================
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
Ansible is pluggable in a lot of other ways separate from inventory scripts and callbacks. Many of these features are there to cover fringe use cases and are infrequently needed, and others are pluggable simply because they are there to implement core features
|
||||
in ansible and were most convenient to be made pluggable.
|
||||
|
||||
This section will explore these features, though they are generally not common in terms of things people would look to extend quite
|
||||
as often.
|
||||
|
||||
.. _developing_connection_type_plugins:
|
||||
|
||||
Connection Type Plugins
|
||||
-----------------------
|
||||
|
||||
By default, ansible ships with a 'paramiko' SSH, native ssh (just called 'ssh'), and 'local' connection type, and an accelerated connection type named 'fireball' -- there are also some minor players like 'chroot' and 'jail'. All of these can be used
|
||||
in playbooks and with /usr/bin/ansible to decide how you want to talk to remote machines. The basics of these connection types
|
||||
are covered in the 'getting started' section. Should you want to extend Ansible to support other transports (SNMP? Message bus?
|
||||
Carrier Pigeon?) it's as simple as copying the format of one of the existing modules and dropping it into the connection plugins
|
||||
directory. The value of 'smart' for a connection allows selection of paramiko or openssh based on system capabilities, and chooses
|
||||
'ssh' if OpenSSH supports ControlPersist, in Ansible 1.2.1 an later. Previous versions did not support 'smart'.
|
||||
|
||||
More documentation on writing connection plugins is pending, though you can jump into lib/ansible/runner/connection_plugins and figure things out pretty easily.
|
||||
|
||||
.. _developing_lookup_plugins:
|
||||
|
||||
Lookup Plugins
|
||||
--------------
|
||||
|
||||
Language constructs like "with_fileglob" and "with_items" are implemented via lookup plugins. Just like other plugin types, you can write your own.
|
||||
|
||||
More documentation on writing connection plugins is pending, though you can jump into lib/ansible/runner/lookup_plugins and figure
|
||||
things out pretty easily.
|
||||
|
||||
.. _developing_vars_plugins:
|
||||
|
||||
Vars Plugins
|
||||
------------
|
||||
|
||||
Playbook constructs like 'host_vars' and 'group_vars' work via 'vars' plugins. They inject additional variable
|
||||
data into ansible runs that did not come from an inventory, playbook, or command line. Note that variables
|
||||
can also be returned from inventory, so in most cases, you won't need to write or understand vars_plugins.
|
||||
|
||||
More documentation on writing connection plugins is pending, though you can jump into lib/ansible/inventory/vars_plugins and figure
|
||||
things out pretty easily.
|
||||
|
||||
If you find yourself wanting to write a vars_plugin, it's more likely you should write an inventory script instead.
|
||||
|
||||
.. _developing_filter_plugins:
|
||||
|
||||
Filter Plugins
|
||||
--------------
|
||||
|
||||
If you want more Jinja2 filters available in a Jinja2 template (filters like to_yaml and to_json are provided by default), they can be extended by writing a filter plugin. Most of the time, when someone comes up with an idea for a new filter they would like to make available in a playbook, we'll just include them in 'core.py' instead.
|
||||
|
||||
Jump into lib/ansible/runner/filter_plugins/ for details.
|
||||
|
||||
.. _developing_callbacks:
|
||||
|
||||
Callbacks
|
||||
---------
|
||||
|
||||
Callbacks are one of the more interesting plugin types. Adding additional callback plugins to Ansible allows for adding new behaviors when responding to events.
|
||||
|
||||
.. _callback_examples:
|
||||
|
||||
Examples
|
||||
++++++++
|
||||
|
||||
Example callbacks are shown `in github in the callbacks directory <https://github.com/ansible/ansible/tree/devel/plugins/callbacks>_`.
|
||||
|
||||
The 'log_plays' callback is an example of how to intercept playbook events to a log file, and the 'mail' callback sends email
|
||||
when playbooks complete.
|
||||
|
||||
The 'osx_say' callback provided is particularly entertaining -- it will respond with computer synthesized speech on OS X in relation
|
||||
to playbook events, and is guaranteed to entertain and/or annoy coworkers.
|
||||
|
||||
.. _configuring_callbacks:
|
||||
|
||||
Configuring
|
||||
+++++++++++
|
||||
|
||||
To active a callback drop it in a callback directory as configured in ansible.cfg.
|
||||
|
||||
.. _callback_development:
|
||||
|
||||
Development
|
||||
+++++++++++
|
||||
|
||||
More information will come later, though see the source of any of the existing callbacks and you should be able to get started quickly.
|
||||
They should be reasonably self explanatory.
|
||||
|
||||
.. _distributing_plugins:
|
||||
|
||||
Distributing Plugins
|
||||
--------------------
|
||||
|
||||
Plugins are loaded from both Python's site_packages (those that ship with ansible) and a configured plugins directory, which defaults
|
||||
to /usr/share/ansible/plugins, in a subfolder for each plugin type::
|
||||
|
||||
* action_plugins
|
||||
* lookup_plugins
|
||||
* callback_plugins
|
||||
* connection_plugins
|
||||
* filter_plugins
|
||||
* vars_plugins
|
||||
|
||||
To change this path, edit the ansible configuration file.
|
||||
|
||||
In addition, plugins can be shipped in a subdirectory relative to a top-level playbook, in folders named the same as indicated above.
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`modules`
|
||||
List of built-in modules
|
||||
:doc:`developing_api`
|
||||
Learn about the Python API for task execution
|
||||
:doc:`developing_inventory`
|
||||
Learn about how to develop dynamic inventory sources
|
||||
:doc:`developing_modules`
|
||||
Learn about how to write Ansible modules
|
||||
`Mailing List <http://groups.google.com/group/ansible-devel>`_
|
||||
The development mailing list
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
234
docsite/rst/faq.rst
Normal file
234
docsite/rst/faq.rst
Normal file
|
@ -0,0 +1,234 @@
|
|||
Frequently Asked Questions
|
||||
==========================
|
||||
|
||||
Here are some commonly-asked questions and their answers.
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
.. _users_and_ports:
|
||||
|
||||
How do I handle different machines needing different user accounts or ports to log in with?
|
||||
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
Setting inventory variables in the inventory file is the easiest way.
|
||||
|
||||
For instance, suppose these hosts have different usernames and ports::
|
||||
|
||||
[webservers]
|
||||
asdf.example.com ansible_ssh_port=5000 ansible_ssh_user=alice
|
||||
jkl.example.com ansible_ssh_port=5001 ansible_ssh_user=bob
|
||||
|
||||
You can also dictate the connection type to be used, if you want::
|
||||
|
||||
[testcluster]
|
||||
localhost ansible_connection=local
|
||||
/path/to/chroot1 ansible_connection=chroot
|
||||
foo.example.com
|
||||
bar.example.com
|
||||
|
||||
You may also wish to keep these in group variables instead, or file in them in a group_vars/<groupname> file.
|
||||
See the rest of the documentation for more information about how to organize variables.
|
||||
|
||||
.. _use_ssh:
|
||||
|
||||
How do I get ansible to reuse connections, enable Kerberized SSH, or have Ansible pay attention to my local SSH config file?
|
||||
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
Switch your default connection type in the configuration file to 'ssh', or use '-c ssh' to use
|
||||
Native OpenSSH for connections instead of the python paramiko library. In Ansible 1.2.1 and later, 'ssh' will be used
|
||||
by default if OpenSSH is new enough to support ControlPersist as an option.
|
||||
|
||||
Paramiko is great for starting out, but the OpenSSH type offers many advanced options. You will want to run Ansible
|
||||
from a machine new enough to support ControlPersist, if you are using this connection type. You can still manage
|
||||
older clients. If you are using RHEL 6, CentOS 6, SLES 10 or SLES 11 the version of OpenSSH is still a bit old, so
|
||||
consider managing from a Fedora or openSUSE client even though you are managing older nodes, or just use paramiko.
|
||||
|
||||
We keep paramiko as the default as if you are first installing Ansible on an EL box, it offers a better experience
|
||||
for new users.
|
||||
|
||||
.. _ec2_cloud_performance:
|
||||
|
||||
How do I speed up management inside EC2?
|
||||
++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
Don't try to manage a fleet of EC2 machines from your laptop. Connect to a management node inside EC2 first
|
||||
and run Ansible from there.
|
||||
|
||||
.. _python_interpreters:
|
||||
|
||||
How do I handle python pathing not having a Python 2.X in /usr/bin/python on a remote machine?
|
||||
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
While you can write ansible modules in any language, most ansible modules are written in Python, and some of these
|
||||
are important core ones.
|
||||
|
||||
By default Ansible assumes it can find a /usr/bin/python on your remote system that is a 2.X version of Python, specifically
|
||||
2.4 or higher.
|
||||
|
||||
Setting of an inventory variable 'ansible_python_interpreter' on any host will allow Ansible to auto-replace the interpreter
|
||||
used when executing python modules. Thus, you can point to any python you want on the system if /usr/bin/python on your
|
||||
system does not point to a Python 2.X interpreter.
|
||||
|
||||
Some Linux operating systems, such as Arch, may only have Python 3 installed by default. This is not sufficient and you will
|
||||
get syntax errors trying to run modules with Python 3. Python 3 is essentially not the same
|
||||
language as Python 2. Ansible modules currently need to support older Pythons for users that still have Enterprise Linux 5 deployed, so they are not yet ported to run under Python 3.0. This is not a problem though as you can just install Python 2 also on a managed host.
|
||||
|
||||
Python 3.0 support will likely be addressed at a later point in time when usage becomes more mainstream.
|
||||
|
||||
Do not replace the shebang lines of your python modules. Ansible will do this for you automatically at deploy time.
|
||||
|
||||
.. _use_roles:
|
||||
|
||||
What is the best way to make content reusable/redistributable?
|
||||
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
If you have not done so already, read all about "Roles" in the playbooks documentation. This helps you make playbook content
|
||||
self contained, and works will with things like git submodules for sharing content with others.
|
||||
|
||||
If some of these plugin types look strange to you, see the API documentation for more details about ways Ansible can be extended.
|
||||
|
||||
.. _configuration_file:
|
||||
|
||||
Where does the configuration file live and what can I configure in it?
|
||||
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
|
||||
See `intro_configuration`.
|
||||
|
||||
.. _who_would_ever_want_to_disable_cowsay_but_ok_here_is_how:
|
||||
|
||||
How do I disable cowsay?
|
||||
++++++++++++++++++++++++
|
||||
|
||||
If cowsay is installed, Ansible takes it upon itself to make your day happier when running playbooks. If you decide
|
||||
that you would like to work in a professional cow-free environment, you can either uninstall cowsay, or set an environment variable::
|
||||
|
||||
export ANSIBLE_NOCOWS=1
|
||||
|
||||
.. _browse_facts:
|
||||
|
||||
How do I see a list of all of the ansible\_ variables?
|
||||
++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
Ansible by default gathers "facts" about the machines under management, and these facts can be accessed in Playbooks and in templates. To see a list of all of the facts that are available about a machine, you can run the "setup" module as an ad-hoc action::
|
||||
|
||||
ansible -m setup hostname
|
||||
|
||||
This will print out a dictionary of all of the facts that are available for that particular host.
|
||||
|
||||
.. _host_loops:
|
||||
|
||||
How do I loop over a list of hosts in a group, inside of a template?
|
||||
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
A pretty common pattern is to iterate over a list of hosts inside of a host group, perhaps to populate a template configuration
|
||||
file with a list of servers. To do this, you can just access the "$groups" dictionary in your template, like this::
|
||||
|
||||
{% for host in groups['db_servers'] %}
|
||||
{{ host }}
|
||||
{% endfor %}
|
||||
|
||||
If you need to access facts about these hosts, for instance, the IP address of each hostname, you need to make sure that the facts have been populated. For example, make sure you have a play that talks to db_servers::
|
||||
|
||||
- hosts: db_servers
|
||||
tasks:
|
||||
- # doesn't matter what you do, just that they were talked to previously.
|
||||
|
||||
Then you can use the facts inside your template, like this::
|
||||
|
||||
{% for host in groups['db_servers'] %}
|
||||
{{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}
|
||||
{% endfor %}
|
||||
|
||||
.. _file_recursion:
|
||||
|
||||
How do I copy files recursively onto a target host?
|
||||
+++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
The "copy" module doesn't handle recursive copies of directories. A common solution to do this is to use a local action to call 'rsync' to recursively copy files to the managed servers.
|
||||
|
||||
Here is an example::
|
||||
|
||||
---
|
||||
# ...
|
||||
tasks:
|
||||
- name: recursively copy files from management server to target
|
||||
local_action: command rsync -a /path/to/files $inventory_hostname:/path/to/target/
|
||||
|
||||
Note that you'll need passphrase-less SSH or ssh-agent set up to let rsync copy without prompting for a passphrase or password.
|
||||
|
||||
.. _shell_env:
|
||||
|
||||
How do I access shell environment variables?
|
||||
++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
If you just need to access existing variables, use the 'env' lookup plugin. For example, to access the value of the HOME
|
||||
environment variable on management machine::
|
||||
|
||||
---
|
||||
# ...
|
||||
vars:
|
||||
local_home: "{{ lookup('env','HOME') }}"
|
||||
|
||||
If you need to set environment variables, see the Advanced Playbooks section about environments.
|
||||
|
||||
Ansible 1.4 will also make remote environment variables available via facts in the 'ansible_env' variable::
|
||||
|
||||
{{ ansible_env.SOME_VARIABLE }}
|
||||
|
||||
.. _user_passwords:
|
||||
|
||||
How do I generate crypted passwords for the user module?
|
||||
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
Crypted password values can be generated as follows::
|
||||
|
||||
openssl passwd -salt <salt> -1 <plaintext>
|
||||
|
||||
.. _commercial_support:
|
||||
|
||||
Can I get training on Ansible or find commercial support?
|
||||
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
Yes! See `AnsibleWorks.com <http://ansibleworks.com>`_ or email `info@ansibleworks.com <mailto:info@ansibleworks.com>`_.
|
||||
|
||||
.. _web_interface:
|
||||
|
||||
Is there a web interface / REST API / etc?
|
||||
++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
Yes! AnsibleWorks makes a great product that makes Ansible even more powerful
|
||||
and easy to use: `AnsibleWorks AWX <http://ansibleworks.com/ansible-awx/>`
|
||||
|
||||
.. _docs_contributions:
|
||||
|
||||
How do I submit a change to the documentation?
|
||||
++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
Great question! Documentation for Ansible is kept in the main project git repository, and complete instructions for contributing can be found in the docs README `viewable on GitHub <https://github.com/ansible/ansible/tree/devel/docsite/latest#readme>`_. Thanks!
|
||||
|
||||
.. _i_dont_see_my_question:
|
||||
|
||||
I don't see my question here
|
||||
++++++++++++++++++++++++++++
|
||||
|
||||
We're happy to help.
|
||||
|
||||
See the "Resources" section of the documentation home page for a link to the IRC and Google Group.
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`index`
|
||||
The documentation index
|
||||
:doc:`playbooks`
|
||||
An introduction to playbooks
|
||||
:doc:`playbooks_best_practices`
|
||||
Best practices advice
|
||||
`User Mailing List <http://groups.google.com/group/ansible-devel>`_
|
||||
Have a question? Stop by the google group!
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
||||
|
||||
|
444
docsite/rst/glossary.rst
Normal file
444
docsite/rst/glossary.rst
Normal file
|
@ -0,0 +1,444 @@
|
|||
Glossary
|
||||
========
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
The following is a list (and re-explanation) of term definitions used elsewhere in the Ansible documentation.
|
||||
|
||||
Consult the documentation home page for the full documentation and to see the terms in context, but this should be a good resource
|
||||
to check your knowledge of Ansible's components and understand how they fit together. It's something you might wish to read for review or
|
||||
when a term comes up on the mailing list.
|
||||
|
||||
Action
|
||||
++++++
|
||||
|
||||
An action is a part of a task that specifies which of the modules to run and the arguments to pass to that module. Each task can have only one action, but it may also have other parameters.
|
||||
|
||||
Ad Hoc
|
||||
++++++
|
||||
|
||||
Refers to running Ansible to perform some quick command, using /usr/bin/ansible, rather than the orchestration language, which is
|
||||
/usr/bin/ansible-playbook. An example of an ad-hoc command might be rebooting 50 machines in your infrastructure. Anything
|
||||
you can do ad-hoc can be accomplished by writing a playbook, and playbooks can also glue lots of other operations together.
|
||||
|
||||
Async
|
||||
+++++
|
||||
|
||||
Refers to a task that is configured to run in the background rather than waiting for completion. If you have a long process
|
||||
that would run longer than the SSH timeout, it would make sense to launch that task in async mode. Async modes can poll
|
||||
for completion every so many seconds, or can be configured to "fire and forget" in which case Ansible will not even
|
||||
check on the task again, it will just kick it off and proceed to future steps. Async modes work with both /usr/bin/ansible
|
||||
and /usr/bin/ansible-playbook.
|
||||
|
||||
Callback Plugin
|
||||
+++++++++++++++
|
||||
|
||||
Refers to some user-written code that can intercept results from Ansible and do something with them. Some supplied examples
|
||||
in the GitHub project perform custom logging, send email, or even play sound effects.
|
||||
|
||||
Check Mode
|
||||
++++++++++
|
||||
|
||||
Refers to running Ansible with the --check option, which does not make any changes on the remote systems, but only outputs the changes that
|
||||
might occur if the command ran without this flag. This is analogous to so-called "dry run" modes in other systems, though the user should
|
||||
be warned that this does not take into account unexpected command failures or cascade effects (which is true of similar modes in other
|
||||
systems). Use this to get an idea of what might happen, but it is not a substitute for a good staging environment.
|
||||
|
||||
Connection Type, Connection Plugin
|
||||
++++++++++++++++++++++++++++++++++
|
||||
|
||||
By default, Ansible talks to remote machines through pluggable libraries. Ansible supports native OpenSSH ('ssh'), or a Python
|
||||
implementation called 'paramiko'. OpenSSH is preferred if you are using a recent version, and also enables some features
|
||||
like Kerberos and jump hosts. This is covered in the getting started section.
|
||||
There are also other connection types like 'fireball' mode, which must be bootstrapped
|
||||
over one of the SSH-based connection types but is very fast, and local mode, which acts on the local system.
|
||||
Users can also write their own connection plugins.
|
||||
|
||||
Conditionals
|
||||
++++++++++++
|
||||
|
||||
A conditional is an expression that evaluates to true or false that decides whether a given task will be executed on a given
|
||||
machine or not. Ansible's conditionals include 'when_boolean',
|
||||
'when_string', and 'when_integer'. These are discussed in the playbook documentation.
|
||||
|
||||
Diff Mode
|
||||
+++++++++
|
||||
|
||||
A --diff flag can be passed to Ansible to show how template files change when they are overwritten, or how they might change when used
|
||||
with --check mode. These diffs come out in unified diff format.
|
||||
|
||||
Facts
|
||||
+++++
|
||||
|
||||
Facts are simply things that are discovered about remote nodes. While they can be used in playbooks and templates just like variables, facts
|
||||
are things that are inferred, rather than set. Facts are automatically discovered by Ansible when running plays by executing the internal 'setup'
|
||||
module on the remote nodes. You never have to call the setup module explicitly, it just runs, but it can be disabled to save time if it is
|
||||
not needed. For the convenience of users who are switching from other configuration management systems, the fact module will also pull in facts from the 'ohai' and 'facter'
|
||||
tools if they are installed, which are fact libraries from Chef and Puppet, respectively.
|
||||
|
||||
Filter Plugin
|
||||
+++++++++++++
|
||||
|
||||
A filter plugin is something that most users will never need to understand. These allow for the creation of new Jinja2 filters, which
|
||||
are more or less only of use to people who know what Jinja2 filters are. If you need them, you can learn how to write them in the API
|
||||
docs section.
|
||||
|
||||
Fireball Mode
|
||||
+++++++++++++
|
||||
|
||||
By default, Ansible uses SSH for connections -- either paramiko or native OpenSSH, a common alternative. (Ansible tries to use
|
||||
'ssh' by default if possible in Ansible 1.2.1 and later, but previously defaulted to paramiko). Some users
|
||||
may want to execute operations even faster though, and they can if they opt to run their tasks using an ephemeral 'fireball' message bus. What happens in this mode is that Ansible
|
||||
will start talking to a node over SSH, and then set up a secure, temporary message bus that authenticates only a single machine, and that will
|
||||
self destruct after a set period of time. This means the bus does not allow management of any kind after the time interval has expired.
|
||||
|
||||
Forks
|
||||
+++++
|
||||
|
||||
Ansible talks to remote nodes in parallel and the level of parallelism can be set either by passing --forks, or editing the default in a configuration
|
||||
file. The default is a very conservative 5 forks, though if you have a lot of RAM, you can easily set this to a value like 50 for increased
|
||||
parallelism.
|
||||
|
||||
Gather Facts (Boolean)
|
||||
++++++++++++++++++++++
|
||||
|
||||
Facts are mentioned above. Sometimes when running a multi-play playbook, it is desirable to have some plays that don't bother with fact
|
||||
computation if they aren't going to need to utilize any of these values. Setting `gather_facts: False` on a playbook allows this implicit
|
||||
fact gathering to be skipped.
|
||||
|
||||
Globbing
|
||||
++++++++
|
||||
|
||||
Globbing is a way to select lots of hosts based on wildcards, rather than the name of the host specifically, or the name of the group
|
||||
they are in. For instance, it is possible to select "www*" to match all hosts starting with "www". This concept is pulled directly
|
||||
from Func, one of Michael's earlier projects. In addition to basic globbing, various set operations are also possible, such as
|
||||
'hosts in this group and not in another group', and so on.
|
||||
|
||||
Group
|
||||
+++++
|
||||
|
||||
A group consists of several hosts assigned to a pool that can be conveniently targeted together, and also given variables that they share in
|
||||
common.
|
||||
|
||||
Group Vars
|
||||
++++++++++
|
||||
|
||||
The "group_vars/" files are files that live in a directory alongside an inventory file, with an optional filename named after each group.
|
||||
This is a convenient place to put variables that will be provided to a given group, especially complex data structures, so that these
|
||||
variables do not have to be embedded in the inventory file or playbook.
|
||||
|
||||
Handlers
|
||||
++++++++
|
||||
|
||||
Handlers are just like regular tasks in an Ansible playbook (see Tasks), but are only run if the Task contains a "notify" directive and
|
||||
also indicates that it changed something. For example, if a config file is changed then the task referencing the config file templating
|
||||
operation may notify a service restart handler. This means services can be bounced only if they need to be restarted.
|
||||
Handlers can be used for things other than service restarts, but service restarts are the most common usage.
|
||||
|
||||
Host
|
||||
++++
|
||||
|
||||
A host is simply a remote machine that Ansible manages. They can have individual variables assigned to them, and can also be organized
|
||||
in groups. All hosts have a name they can be reached at (which is either an IP address or a domain name) and optionally a port number
|
||||
if they are not to be accessed on the default SSH port.
|
||||
|
||||
Host Specifier
|
||||
++++++++++++++
|
||||
|
||||
Each Play in Ansible maps a series of tasks (which define the role, purpose, or orders of a system) to a set of systems.
|
||||
|
||||
This "hosts:" directive in each play is often called the hosts specifier.
|
||||
|
||||
It may select one system, many systems, one or more groups, or even some hosts that are in one group and explicitly not in another.
|
||||
|
||||
Host Vars
|
||||
+++++++++
|
||||
|
||||
Just like "Group Vars", a directory alongside the inventory file named "host_vars/" can contain a file named after each hostname in
|
||||
the inventory file, in YAML format. This provides a convenient place to assign variables to the host without having to embed
|
||||
them in the inventory file. The Host Vars file can also be used to define complex data structures that can't be represented in the
|
||||
inventory file.
|
||||
|
||||
Lazy Evaluation
|
||||
+++++++++++++++
|
||||
|
||||
In general, Ansible evaluates any variables in playbook content at the last possible second, which means that if you define a data structure
|
||||
that data structure itself can define variable values within it, and everything "just works" as you would expect. This also means variable
|
||||
strings can include other variables inside of those strings.
|
||||
|
||||
Lookup Plugin
|
||||
+++++++++++++
|
||||
|
||||
A lookup plugin is a way to get data into Ansible from the outside world. These are how such things as "with_items", a basic looping plugin, are implemented,
|
||||
but there are also lookup plugins like "with_file" which loads data from a file, and even ones for querying environment variables,
|
||||
DNS text records, or key value stores. Lookup plugins can also be accessed in templates, e.g., ``{{ lookup('file','/path/to/file') }}``.
|
||||
|
||||
Multi-Tier
|
||||
++++++++++
|
||||
|
||||
The concept that IT systems are not managed one system at a time, but by interactions between multiple systems, and groups of systems, in
|
||||
well defined orders. For instance, a web server may need to be updated before a database server, and pieces on the web server may need
|
||||
to be updated after *THAT* database server, and various load balancers and monitoring servers may need to be contacted. Ansible models
|
||||
entire IT topologies and workflows rather than looking at configuration from a "one system at a time" perspective.
|
||||
|
||||
Idempotency
|
||||
+++++++++++
|
||||
|
||||
The concept that change commands should only be applied when they need to be applied, and that it is better to describe the desired
|
||||
state of a system than the process of how to get to that state. As an analogy, the path from North Carolina in the United States to
|
||||
California involves driving a very long way West, but if I were instead in Anchorage, Alaska, driving a long way west is no longer
|
||||
the right way to get to California. Ansible's Resources like you to say "put me in California" and then decide how to get there. If
|
||||
you were already in California, nothing needs to happen, and it will let you know it didn't need to change anything.
|
||||
|
||||
Includes
|
||||
++++++++
|
||||
|
||||
The idea that playbook files (which are nothing more than lists of plays) can include other lists of plays, and task lists
|
||||
can externalize lists of tasks in other files, and similarly with handlers. Includes can be parameterized, which means that the
|
||||
loaded file can pass variables. For instance, an included play for setting up a WordPress blog may take a parameter called "user"
|
||||
and that play could be included more than once to create a blog for both "alice" and "bob".
|
||||
|
||||
Inventory
|
||||
+++++++++
|
||||
|
||||
A file (by default, Ansible uses a simple INI format) that describes Hosts and Groups in Ansible. Inventory can also be provided
|
||||
via an "Inventory Script" (sometimes called an "External Inventory Script").
|
||||
|
||||
Inventory Script
|
||||
++++++++++++++++
|
||||
|
||||
A very simple program (or a complicated one) that looks up hosts, group membership for hosts, and variable information from an external
|
||||
resource -- whether that be a SQL database, a CMDB solution, or something like LDAP. This concept was adapted from Puppet (where it is
|
||||
called an "External Nodes Classifier") and works more or less exactly the same way.
|
||||
|
||||
Jinja2
|
||||
++++++
|
||||
|
||||
Jinja2 is the preferred templating language of Ansible's template module. It is a very simple Python template language that is generally
|
||||
readable and easy to write.
|
||||
|
||||
JSON
|
||||
++++
|
||||
|
||||
Ansible uses JSON for return data from remote modules. This allows modules to be written in any language, not just Python.
|
||||
|
||||
only_if
|
||||
+++++++
|
||||
|
||||
A deprecated form of the "when:" statement. It should no longer be used.
|
||||
|
||||
Library
|
||||
+++++++
|
||||
|
||||
A collection of modules made available to /usr/bin/ansible or an Ansible playbook.
|
||||
|
||||
Limit Groups
|
||||
++++++++++++
|
||||
|
||||
By passing "--limit somegroup" to ansible or ansible-playbook, the commands can be limited to a subset of hosts. For instance,
|
||||
this can be used to run a playbook that normally targets an entire set of servers to one particular server.
|
||||
|
||||
Local Connection
|
||||
++++++++++++++++
|
||||
|
||||
By using "connection: local" in a playbook, or passing "-c local" to /usr/bin/ansible, this indicates that we are managing the local
|
||||
host and not a remote machine.
|
||||
|
||||
Local Action
|
||||
++++++++++++
|
||||
|
||||
A local_action directive in a playbook targeting remote machines means that the given step will actually occur on the local
|
||||
machine, but that the variable '{{ ansible_hostname }}' can be passed in to reference the remote hostname being referred to in
|
||||
that step. This can be used to trigger, for example, an rsync operation.
|
||||
|
||||
Loops
|
||||
+++++
|
||||
|
||||
Generally, Ansible is not a programming language. It prefers to be more declarative, though various constructs like "with_items"
|
||||
allow a particular task to be repeated for multiple items in a list. Certain modules, like yum and apt, are actually optimized
|
||||
for this, and can install all packages given in those lists within a single transaction, dramatically speeding up total
|
||||
time to configuration.
|
||||
|
||||
Modules
|
||||
+++++++
|
||||
|
||||
Modules are the units of work that Ansible ships out to remote machines. Modules are kicked off by either /usr/bin/ansible or
|
||||
/usr/bin/ansible-playbook (where multiple tasks use lots of different modules in conjunction). Modules can be implemented in any
|
||||
language, including Perl, Bash, or Ruby -- but can leverage some useful communal library code if written in Python. Modules just
|
||||
have to return JSON or simple key=value pairs. Once modules are executed on remote machines, they are removed, so no long running
|
||||
daemons are used. Ansible refers to the collection of available modules as a 'library'.
|
||||
|
||||
Notify
|
||||
++++++
|
||||
|
||||
The act of a task registering a change event and informing a handler task that another action needs to be run at the end of the play.
|
||||
If a handler is notified by multiple tasks, it will still be run only once. Handlers are run in the order they are listed, not
|
||||
in the order that they are notified.
|
||||
|
||||
Orchestration
|
||||
+++++++++++++
|
||||
|
||||
Many software automation systems use this word to mean different things. Ansible uses it as a conductor would conduct an orchestra.
|
||||
A datacenter or cloud architecture is full of many systems, playing many parts -- web servers, database servers, maybe load balancers,
|
||||
monitoring systems, continuous integration systems, etc. In performing any process, it is necessary to touch systems in particular orders,
|
||||
often to simulate rolling updates or to deploy software correctly. Some system may perform some steps, then others, then previous systems
|
||||
already processed may need to perform more steps. Along the way, emails may need to be sent or web services contacted. Ansible
|
||||
orchestration is all about modeling that kind of process.
|
||||
|
||||
paramiko
|
||||
++++++++
|
||||
|
||||
By default, Ansible manages machines over SSH. The library that Ansible uses by default to do this is a Python-powered library called
|
||||
paramiko. The paramiko library is generally fast and easy to manage, though users desiring Kerberos or Jump Host support may wish to switch
|
||||
to a native SSH binary such as OpenSSH by specifying the connection type in their playbook, or using the "-c ssh" flag.
|
||||
|
||||
Playbooks
|
||||
+++++++++
|
||||
|
||||
Playbooks are the language by which Ansible orchestrates, configures, administers, or deploys systems. They are called playbooks partially because it's a sports analogy, and it's supposed to be fun using them. They aren't workbooks :)
|
||||
|
||||
Plays
|
||||
+++++
|
||||
|
||||
A playbook is a list of plays. A play is minimally a mapping between a set of hosts selected by a host specifier (usually chosen by groups, but sometimes by hostname
|
||||
globs) and the tasks which run on those hosts to define the role that those systems will perform. There
|
||||
can be one or many plays in a playbook.
|
||||
|
||||
Pull Mode
|
||||
+++++++++
|
||||
|
||||
By default, Ansible runs in push mode, which allows it very fine-grained control over when it talks to each system. Pull mode is
|
||||
provided for when you would rather have nodes check in every N minutes on a particular schedule. It uses a program called ansible-pull and can also be set up (or reconfigured) using a push-mode playbook. Most Ansible users use push mode, but pull mode is included for variety and the sake
|
||||
of having choices.
|
||||
|
||||
ansible-pull works by checking configuration orders out of git on a crontab and then managing the machine locally, using the local
|
||||
connection plugin.
|
||||
|
||||
Push Mode
|
||||
+++++++++
|
||||
|
||||
Push mode is the default mode of Ansible. In fact, it's not really a mode at all -- it's just how Ansible works when you aren't
|
||||
thinking about it. Push mode allows Ansible to be fine-grained and conduct nodes through complex orchestration processes without
|
||||
waiting for them to check in.
|
||||
|
||||
Register Variable
|
||||
+++++++++++++++++
|
||||
|
||||
The result of running any task in Ansible can be stored in a variable for use in a template or a conditional statement.
|
||||
The keyword used to define the variable is called 'register', taking its name from the idea of registers in assembly
|
||||
programming (though Ansible will never feel like assembly programming). There are an infinite number of variable names
|
||||
you can use for registration.
|
||||
|
||||
Resource Model
|
||||
++++++++++++++
|
||||
|
||||
Ansible modules work in terms of resources. For instance, the file module will select a particular file
|
||||
and ensure that the attributes of that resource match a particular model. As an example, we might wish to change the owner of /etc/motd
|
||||
to 'root' if it is not already set to root, or set its mode to '0644' if it is not already set to '0644'. The resource models
|
||||
are 'idempotent' meaning change commands are not run unless needed, and Ansible will bring the system back to a desired
|
||||
state regardless of the actual state -- rather than you having to tell it how to get to the state.
|
||||
|
||||
Roles
|
||||
+++++
|
||||
|
||||
Roles are units of organization in Ansible. Assigning a role to a group of hosts (or a set of groups, or host patterns, etc.) implies that they should implement a specific behavior. A role
|
||||
may include applying certain variable values, certain tasks, and certain handlers -- or just one or more of these things. Because of the file structure associated with a role, roles become
|
||||
redistributable units that allow you to share behavior among playbooks -- or even with other users.
|
||||
|
||||
Rolling Update
|
||||
++++++++++++++
|
||||
|
||||
The act of addressing a number of nodes in a group N at a time to avoid updating them all at once and bringing the system
|
||||
offline. For instance, in a web topology of 500 nodes handling very large volume, it may be reasonable to update 10 or 20
|
||||
machines at a time, moving on to the next 10 or 20 when done. The "serial:" keyword in an Ansible playbook controls the
|
||||
size of the rolling update pool. The default is to address the batch size all at once, so this is something that you must
|
||||
opt-in to. OS configuration (such as making sure config files are correct) does not typically have to use the rolling update
|
||||
model, but can do so if desired.
|
||||
|
||||
Runner
|
||||
++++++
|
||||
|
||||
A core software component of Ansible that is the power behind /usr/bin/ansible directly -- and corresponds to the invocation
|
||||
of each task in a playbook. The Runner is something Ansible developers may talk about, but it's not really user land
|
||||
vocabulary.
|
||||
|
||||
Serial
|
||||
++++++
|
||||
|
||||
See "Rolling Update".
|
||||
|
||||
Sudo
|
||||
++++
|
||||
|
||||
Ansible does not require root logins, and since it's daemonless, definitely does not require root level daemons (which can
|
||||
be a security concern in sensitive environments). Ansible can log in and perform many operations wrapped in a sudo command,
|
||||
and can work with both password-less and password-based sudo. Some operations that don't normally work with sudo (like scp
|
||||
file transfer) can be achieved with Ansible's copy, template, and fetch modules while running in sudo mode.
|
||||
|
||||
SSH (Native)
|
||||
++++++++++++
|
||||
|
||||
Native OpenSSH as an Ansible transport is specified with "-c ssh" (or a config file, or a directive in the playbook)
|
||||
and can be useful if wanting to login via Kerberized SSH or using SSH jump hosts, etc. In 1.2.1, 'ssh' will be used by default if the OpenSSH binary
|
||||
on the control machine is sufficiently new. Previously, Ansible selected 'paramiko' as a default.
|
||||
Using a client that supports ControlMaster and ControlPersist is recommended for maximum performance -- if you don't have that and don't need Kerberos, jump hosts, or other features, paramiko is a good choice. Ansible will warn you if it doesn't detect ControlMaster/ControlPersist capability.
|
||||
|
||||
Tags
|
||||
++++
|
||||
|
||||
Ansible allows tagging resources in a playbook with arbitrary keywords, and then running only the parts of the playbook that
|
||||
correspond to those keywords. For instance, it is possible to have an entire OS configuration, and have certain steps
|
||||
labeled "ntp", and then run just the "ntp" steps to reconfigure the time server information on a remote host.
|
||||
|
||||
Tasks
|
||||
+++++
|
||||
|
||||
Playbooks exist to run tasks. Tasks combine an action (a module and its arguments) with a name and optionally some other keywords (like looping directives). Handlers are also tasks, but they are a special kind of task that do not run unless they are notified by name when a task reports an underlying change on a remote system.
|
||||
|
||||
Templates
|
||||
+++++++++
|
||||
|
||||
Ansible can easily transfer files to remote systems, but often it is desirable to substitute variables in other files. Variables
|
||||
may come from the inventory file, Host Vars, Group Vars, or Facts. Templates use the Jinja2 template engine and can also include logical
|
||||
constructs like loops and if statements.
|
||||
|
||||
Transport
|
||||
+++++++++
|
||||
|
||||
Ansible uses "Connection Plugins" to define types of available transports. These are simply how Ansible will reach out to managed systems. Transports included are paramiko, SSH (using OpenSSH), fireball (an SSH-bootstrapped accelerated connection plugin), and local.
|
||||
|
||||
When
|
||||
++++
|
||||
|
||||
An optional conditional statement attached to a task that is used to determine if the task should run or not. If the expression following the "when:" keyword evaluates to false, the task will be ignored.
|
||||
|
||||
Van Halen
|
||||
+++++++++
|
||||
|
||||
For no particular reason, other than the fact that Michael really likes them, all Ansible releases are codenamed after Van Halen songs. There is no preference given to David Lee Roth vs. Sammy Lee Hagar-era songs, and instrumentals are also allowed. It is unlikely that there will ever be a Jump release, but a Van Halen III codename release is possible. You never know.
|
||||
|
||||
Vars (Variables)
|
||||
++++++++++++++++
|
||||
|
||||
As opposed to Facts, variables are names of values (they can be simple scalar values -- integers, booleans, strings) or complex ones (dictionaries/hashes, lists) that can be used in templates and playbooks. They are declared things, not things that are inferred from the remote system's current state or nature (which is what Facts are).
|
||||
|
||||
YAML
|
||||
++++
|
||||
|
||||
Ansible does not want to force people to write programming language code to automate infrastructure, so Ansible uses YAML to define playbook configuration languages and also variable files. YAML is nice because it has a minimum of syntax and is very clean and easy for people to skim. It is a good data format for configuration files and humans, but also machine readable. Ansible's usage of YAML stemmed from Michael's first use of it inside of Cobbler around 2006. YAML is fairly popular in the dynamic language community and the format has libraries available
|
||||
for serialization in many different languages (Python, Perl, Ruby, etc.).
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`faq`
|
||||
Frequently asked questions
|
||||
:doc:`playbooks`
|
||||
An introduction to playbooks
|
||||
:doc:`playbooks_best_practices`
|
||||
Best practices advice
|
||||
`User Mailing List <http://groups.google.com/group/ansible-devel>`_
|
||||
Have a question? Stop by the google group!
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
208
docsite/rst/guide_aws.rst
Normal file
208
docsite/rst/guide_aws.rst
Normal file
|
@ -0,0 +1,208 @@
|
|||
Amazon Web Services Guide
|
||||
=========================
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
.. _aws_intro:
|
||||
|
||||
Introduction
|
||||
````````````
|
||||
|
||||
.. note:: This section of the documentation is under construction. We are in the process of adding more examples about all of the EC2 modules
|
||||
and how they work together. There's also an ec2 example in the language_features directory of `the ansible-examples github repository <http://github.com/ansible/ansible-examples/>`_ that you may wish to consult. Once complete, there will also be new examples of ec2 in ansible-examples.
|
||||
|
||||
Ansible contains a number of core modules for interacting with Amazon Web Services (AWS). These also work with Eucalyptus, which is an AWS compatible private cloud solution. There are other supported cloud types, but this documentation chapter is about AWS API clouds. The purpose of this
|
||||
section is to explain how to put Ansible modules together (and use inventory scripts) to use Ansible in AWS context.
|
||||
|
||||
Requirements for the AWS modules are minimal. All of the modules require and are tested against boto 2.5 or higher. You'll need this Python module installed on the execution host. If you are using Red Hat Enterprise Linux or CentOS, install boto from `EPEL <http://fedoraproject.org/wiki/EPEL>`_:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ yum install python-boto
|
||||
|
||||
You can also install it via pip if you want.
|
||||
|
||||
The following steps will often execute outside the host loop, so it makes sense to add localhost to inventory. Ansible
|
||||
may not require this step in the future::
|
||||
|
||||
[local]
|
||||
localhost
|
||||
|
||||
And in your playbook steps we'll typically be using the following pattern for provisioning steps::
|
||||
|
||||
- hosts: localhost
|
||||
connection: local
|
||||
gather_facts: False
|
||||
|
||||
.. _aws_provisioning:
|
||||
|
||||
Provisioning
|
||||
````````````
|
||||
|
||||
The ec2 module provides the ability to provision instances within EC2. Typically the provisioning task will be performed against your Ansible master server as a local_action statement.
|
||||
|
||||
.. note::
|
||||
|
||||
Authentication with the AWS-related modules is handled by either
|
||||
specifying your access and secret key as ENV variables or passing
|
||||
them as module arguments.
|
||||
|
||||
.. note::
|
||||
|
||||
To talk to specific endpoints, the environmental variable EC2_URL
|
||||
can be set. This is useful if using a private cloud like Eucalyptus,
|
||||
exporting the variable as EC2_URL=https://myhost:8773/services/Eucalyptus.
|
||||
This can be set using the 'environment' keyword in Ansible if you like.
|
||||
|
||||
Here is an example of provisioning a number of instances in ad-hoc mode mode:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
# ansible localhost -m ec2 -a "image=ami-6e649707 instance_type=m1.large keypair=mykey group=webservers wait=yes" -c local
|
||||
|
||||
In a play, this might look like (assuming the parameters are held as vars)::
|
||||
|
||||
tasks:
|
||||
- name: Provision a set of instances
|
||||
local_action: ec2
|
||||
keypair={{mykeypair}}
|
||||
group={{security_group}}
|
||||
instance_type={{instance_type}}
|
||||
image={{image}}
|
||||
wait=true
|
||||
count={{number}}
|
||||
register: ec2
|
||||
|
||||
|
||||
By registering the return its then possible to dynamically create a host group consisting of these new instances. This facilitates performing configuration actions on the hosts immediately in a subsequent task::
|
||||
|
||||
- name: Add all instance public IPs to host group
|
||||
local_action: add_host hostname={{ item.public_ip }} groupname=ec2hosts
|
||||
with_items: ec2.instances
|
||||
|
||||
With the host group now created, a second play in your provision playbook might now have some configuration steps::
|
||||
|
||||
- name: Configuration play
|
||||
hosts: ec2hosts
|
||||
user: ec2-user
|
||||
gather_facts: true
|
||||
|
||||
tasks:
|
||||
- name: Check NTP service
|
||||
action: service name=ntpd state=started
|
||||
|
||||
Rather than include configuration inline, you may also choose to just do it as a task include or a role.
|
||||
|
||||
The method above ties the configuration of a host with the provisioning step. This isn't always ideal and leads us onto the next section.
|
||||
|
||||
:: _aws_advanced:
|
||||
|
||||
Advanced Usage
|
||||
``````````````
|
||||
|
||||
:: _aws_host_inventory:
|
||||
|
||||
Host Inventory
|
||||
++++++++++++++
|
||||
|
||||
Once your nodes are spun up, you'll probably want to talk to them again. The best way to handle his is to use the ec2 inventory plugin.
|
||||
|
||||
Even for larger environments, you might have nodes spun up from Cloud Formations or other tooling. You don't have to use Ansible to spin up guests. Once these are created and you wish to configure them, the EC2 API can be used to return system grouping with the help of the EC2 inventory script. This script can be used to group resources by their security group or tags. Tagging is highly recommended in EC2 and can provide an easy way to sort between host groups and roles. The inventory script is documented `in the API chapter <http://www.ansibleworks.com/docs/api.html#external-inventory-scripts>`_.
|
||||
|
||||
You may wish to schedule a regular refresh of the inventory cache to accommodate for frequent changes in resources:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
# ./ec2.py --refresh-cache
|
||||
|
||||
Put this into a crontab as appropriate to make calls from your Ansible master server to the EC2 API endpoints and gather host information. The aim is to keep the view of hosts as up-to-date as possible, so schedule accordingly. Playbook calls could then also be scheduled to act on the refreshed hosts inventory after each refresh. This approach means that machine images can remain "raw", containing no payload and OS-only. Configuration of the workload is handled entirely by Ansible.
|
||||
|
||||
:: _aws_pull:
|
||||
|
||||
Pull Configuration
|
||||
++++++++++++++++++
|
||||
|
||||
For some the delay between refreshing host information and acting on that host information (i.e. running Ansible tasks against the hosts) may be too long. This may be the case in such scenarios where EC2 AutoScaling is being used to scale the number of instances as a result of a particular event. Such an event may require that hosts come online and are configured as soon as possible (even a 1 minute delay may be undesirable). Its possible to pre-bake machine images which contain the necessary ansible-pull script and components to pull and run a playbook via git. The machine images could be configured to run ansible-pull upon boot as part of the bootstrapping procedure.
|
||||
|
||||
More information on pull-mode playbooks can be found `here <http://www.ansibleworks.com/docs/playbooks2.html#pull-mode-playbooks>`_.
|
||||
|
||||
(Various developments around Ansible are also going to make this easier in the near future. Stay tuned!)
|
||||
|
||||
:: _aws_autoscale:
|
||||
|
||||
AWX Autoscaling
|
||||
+++++++++++++++
|
||||
|
||||
AnsibleWorks's "AWX" product also contains a very nice feature for auto-scaling use cases. In this mode, a simple curl script can call
|
||||
a defined URL and the server will "dial out" to the requester and configure an instance that is spinning up. This can be a great way
|
||||
to reconfigure ephmeral nodes. See the AWX documentation for more details. Click on the AWX link in the sidebar for details.
|
||||
|
||||
A benefit of using the callback in AWX over pull mode is that job results are still centrally recorded and less information has to be shared
|
||||
with remote hosts.
|
||||
|
||||
:: _aws_use_cases:
|
||||
|
||||
Use Cases
|
||||
`````````
|
||||
|
||||
This section covers some usage examples built around a specific use case.
|
||||
|
||||
:: _aws_cloudformation_example:
|
||||
|
||||
Example 1
|
||||
+++++++++
|
||||
|
||||
Example 1: I'm using CloudFormation to deploy a specific infrastructure stack. I'd like to manage configuration of the instances with Ansible.
|
||||
|
||||
Provision instances with your tool of choice and consider using the inventory plugin to group hosts based on particular tags or security group. Consider tagging instances you wish to managed with Ansible with a suitably unique key=value tag.
|
||||
|
||||
.. note:: Ansible also has a cloudformation module you may wish to explore.
|
||||
|
||||
:: _aws_autoscale_example:
|
||||
|
||||
Example 2
|
||||
+++++++++
|
||||
|
||||
Example 2: I'm using AutoScaling to dynamically scale up and scale down the number of instances. This means the number of hosts is constantly fluctuating but I'm letting EC2 automatically handle the provisioning of these instances. I don't want to fully bake a machine image, I'd like to use Ansible to configure the hosts.
|
||||
|
||||
There are several approaches to this use case. The first is to use the inventory plugin to regularly refresh host information and then target hosts based on the latest inventory data. The second is to use ansible-pull triggered by a user-data script (specified in the launch configuration) which would then mean that each instance would fetch Ansible and the latest playbook from a git repository and run locally to configure itself. You could also use the AWX callback feature.
|
||||
|
||||
:: _aws_builds:
|
||||
|
||||
Example 3
|
||||
+++++++++
|
||||
|
||||
Example 3: I don't want to use Ansible to manage my instances but I'd like to consider using Ansible to build my fully-baked machine images.
|
||||
|
||||
There's nothing to stop you doing this. If you like working with Ansible's playbook format then writing a playbook to create an image; create an image file with dd, give it a filesystem and then install packages and finally chroot into it for further configuration. Ansible has the 'chroot' plugin for this purpose, just add the following to your inventory file::
|
||||
|
||||
/chroot/path ansible_connection=chroot
|
||||
|
||||
And in your playbook::
|
||||
|
||||
hosts: /chroot/path
|
||||
|
||||
.. note:: more examples of this are pending. You may also be interested in the ec2_ami module for taking AMIs of running instances.
|
||||
|
||||
:: _aws_pending:
|
||||
|
||||
Pending Information
|
||||
```````````````````
|
||||
|
||||
In the future look here for more topics.
|
||||
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`modules`
|
||||
All the documentation for Ansible modules
|
||||
:doc:`playbooks`
|
||||
An introduction to playbooks
|
||||
:doc:`playbooks_delegation`
|
||||
Delegation, useful for working with loud balancers, clouds, and locally executed steps.
|
||||
`User Mailing List <http://groups.google.com/group/ansible-devel>`_
|
||||
Have a question? Stop by the google group!
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
186
docsite/rst/index.rst
Normal file
186
docsite/rst/index.rst
Normal file
|
@ -0,0 +1,186 @@
|
|||
|
||||
Ansible Documentation
|
||||
=====================
|
||||
|
||||
About Ansible
|
||||
`````````````
|
||||
|
||||
Welcome to the Ansible documentation!
|
||||
|
||||
Ansible is an IT automation tool. It can configure systems, deploy software, and orchestrate more advanced IT tasks
|
||||
such as continuous deployments or zero downtime rolling updates.
|
||||
|
||||
Ansible's goals are foremost those of simplicity and maximum ease of use. It also has a strong focus on security and reliability, featuring a minimum of moving parts, usage of Open SSH for transport (with an accelerated socket mode and pull modes as alternatives), and a language that is designed around auditability by humans -- even those not familiar with the program.
|
||||
|
||||
We believe simplicity is relevant to all sizes of environments and design for busy users of all types -- whether this means developers, sysadmins, release engineers, IT managers, and everywhere in between. Ansible is appropriate for managing small setups with a handful of instances as well as enterprise environments with many thousands.
|
||||
|
||||
Ansible manages machines in an agentless manner. There is never a question of how to
|
||||
upgrade remote daemons or the problem of not being able to manage systems because daemons are uninstalled. As OpenSSH is one of the most peer reviewed open source components, the security exposure of using the tool is greatly reduced. Ansible is decentralized -- it relies on your existing OS credentials to control access to remote machines; if needed it can easily connect with Kerberos, LDAP, and other centralized authentication management systems.
|
||||
|
||||
You may be interested in reading about `some notable Ansible users <http://www.ansibleworks.com/users/>`_.
|
||||
|
||||
This documentation covers the current released version of Ansible (1.3.X) and also some development version features (1.4). For recent features, in each section, the version of Ansible where the feature is added is indicated. AnsibleWorks releases a new major release of Ansible approximately every 2 months. The core application evolves somewhat conservatively, valuing simplicity in language design and setup, while the community around new modules and plugins being developed and contributed moves very very quickly, typically adding 20 or so new modules in each release.
|
||||
|
||||
.. _an_introduction:
|
||||
|
||||
The Basics
|
||||
``````````
|
||||
|
||||
Before we dive into the really fun parts -- playbooks, configuration management, deployment, and orchestration, we'll learn how to get Ansible installed and some basic concepts. We'll go over how to execute ad-hoc commands in parallel across your nodes using /usr/bin/ansible. We'll also see what sort of modules are available in Ansible's core (though you can also write your own, which we'll also show later).
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
|
||||
intro_installation
|
||||
intro_getting_started
|
||||
intro_inventory
|
||||
intro_dynamic_inventory
|
||||
intro_patterns
|
||||
intro_adhoc
|
||||
intro_configuration
|
||||
|
||||
Modules
|
||||
```````
|
||||
|
||||
Ansible modules are resources that are distributed to remote nodes to make them perform particular tasks or match a particular
|
||||
state. Ansible follows a "batteries included" philosophy, so you have a lot of great modules for all manner of
|
||||
IT tasks in the core distribution. This means modules are well up-to-date and you don't have to hunt for an implementation
|
||||
that will work on your platform. You may think of the module library as a toolbox full of useful system management tools,
|
||||
and playbooks as the instructions for building something using those tools.
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
|
||||
modules
|
||||
|
||||
.. _overview:
|
||||
|
||||
Architecture Diagram
|
||||
````````````````````
|
||||
|
||||
.. image:: http://www.ansibleworks.com/wp-content/uploads/2013/06/ANSIBLE_DIAGRAM.jpg
|
||||
:alt: ansible architecture diagram
|
||||
:width: 788px
|
||||
:height: 436px
|
||||
|
||||
.. _introduction_to_playbooks:
|
||||
|
||||
Playbooks
|
||||
`````````
|
||||
|
||||
Playbooks are Ansible's configuration, deployment, and orchestration language. They can describe a policy you want your remote systems to enforce, or a set of steps in a general IT process.
|
||||
|
||||
If Ansible modules are your the tools in your workshop, playbooks are your design plans.
|
||||
|
||||
At a basic level, playbooks can be used to manage configurations of and deployments to remote machines. At a more advanced level, they can sequence multi-tier rollouts involving rolling updates, and can delegate actions to other hosts, interacting with monitoring servers and load balancers along the way.
|
||||
|
||||
While there's a lot of information here, there's no need to learn everything at once. You can start small and pick up more features
|
||||
over time as you need them.
|
||||
|
||||
Playbooks are designed to be human-readable and are developed in a basic text language. There are multiple
|
||||
ways to organize playbooks and the files they include, and we'll offer up some suggestions on that and making the most out of Ansible.
|
||||
|
||||
It is recommended to look at `Example Playbooks <https://github.com/ansible/ansible-examples>`_ while reading along with the playbook documentation. These illustrate best practices as well as how to put many of the various concepts together.
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
|
||||
playbooks
|
||||
playbooks_roles
|
||||
playbooks_variables
|
||||
playbooks_conditionals
|
||||
playbooks_loops
|
||||
playbooks_best_practices
|
||||
|
||||
.. _advanced_topics_in_playbooks:
|
||||
|
||||
Special Topics In Playbooks
|
||||
```````````````````````````
|
||||
|
||||
Here are some playbook features that not everyone may need to learn, but can be quite useful for particular applications.
|
||||
Browsing these topics is recommended as you may find some useful tips here, but feel free to learn the basics of Ansible first
|
||||
and adopt these only if they seem relevant or useful to your environment.
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
|
||||
playbooks_acceleration
|
||||
playbooks_async
|
||||
playbooks_checkmode
|
||||
playbooks_delegation
|
||||
playbooks_environment
|
||||
playbooks_error_handling
|
||||
playbooks_lookups
|
||||
playbooks_prompts
|
||||
playbooks_tags
|
||||
|
||||
.. _ansibleworks_awx:
|
||||
|
||||
AnsibleWorks AWX
|
||||
````````````````
|
||||
|
||||
`AnsibleWorks <http://ansibleworks.com>`_, who also sponsors the Ansible community, also produces 'AWX', which is a web-based solution that makes Ansible even more easy to use for IT teams of all kinds. It's designed to be the hub for all of your automation tasks.
|
||||
|
||||
AWX allows you to control access to who can access what, even allowing sharing of SSH credentials without someone being able to transfer those credentials. Inventory can be graphically managed or synced with a wide variety of cloud sources. It logs all of your jobs, integrates well with LDAP, and has an amazing browsable REST API. Command line tools are available for easy integration
|
||||
with Jenkins as well.
|
||||
|
||||
Find out more about AWX features and how to download it on the `AWX webpage <http://ansibleworks.com/ansibleworks-awx>`_. AWX
|
||||
is free for usage for up to 10 nodes, and comes bundled with amazing support from AnsibleWorks. As you would expect, AWX is
|
||||
installed using Ansible playbooks!
|
||||
|
||||
.. _detailed_guides:
|
||||
|
||||
Detailed Guides
|
||||
```````````````
|
||||
|
||||
This section is new and evolving. The idea here is explore particular use cases in greater depth and provide a more "top down" explanation of some basic features.
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
|
||||
guide_aws
|
||||
|
||||
Pending topics may include: Vagrant, Docker, Jenkins, Rackspace Cloud, Google Compute Engine, Linode/Digital Ocean, Continous Deployment, and more.
|
||||
|
||||
.. _community_information:
|
||||
|
||||
Community Information
|
||||
`````````````````````
|
||||
|
||||
Ansible is an open source project designed to bring together developers and administrators of all kinds to collaborate on building
|
||||
IT automation solutions that work well for them. Should you wish to get more involved -- whether in terms of just asking a question, helping other users, introducing new people to Ansible, or helping with the software or documentation, we welcome your contributions to the project.
|
||||
|
||||
`Ways to interact <https://github.com/ansible/ansible/blob/devel/CONTRIBUTING.md>`_
|
||||
|
||||
.. _developer_information:
|
||||
|
||||
Developer Information
|
||||
`````````````````````
|
||||
|
||||
Learn how to build modules of your own in any language, and also how to extend Ansible through several kinds of plugins. Explore Ansible's Python API and write Python plugins to integrate with other solutions in your environment.
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
|
||||
developing_api
|
||||
developing_inventory
|
||||
developing_modules
|
||||
developing_plugins
|
||||
|
||||
Developers will also likely be interested in the fully-discoverable `REST API <http://ansibleworks.com/ansibleworks-awx>`_ that is part of AnsibleWorks AWX. It's great for embedding Ansible in all manner of applications.
|
||||
|
||||
.. _misc:
|
||||
|
||||
Miscellaneous
|
||||
`````````````
|
||||
|
||||
Some additional topics you may be interested in:
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
|
||||
faq
|
||||
glossary
|
||||
YAMLSyntax
|
||||
|
||||
|
302
docsite/rst/intro_adhoc.rst
Normal file
302
docsite/rst/intro_adhoc.rst
Normal file
|
@ -0,0 +1,302 @@
|
|||
|
||||
Introduction To Ad-Hoc Commands
|
||||
===============================
|
||||
|
||||
.. highlight:: bash
|
||||
|
||||
The following examples show how to use `/usr/bin/ansible` for running
|
||||
ad hoc tasks.
|
||||
|
||||
What's an ad-hoc command?
|
||||
|
||||
An ad-hoc command is something that you might type in to do something really
|
||||
quick, but don't want to save for later.
|
||||
|
||||
This is a good place to start to understand the basics of what Ansible can do
|
||||
prior to learning the playbooks language -- ad-hoc commands can also be used
|
||||
to do quick things that you might not necessarily want to write a full playbook
|
||||
for.
|
||||
|
||||
Generally speaking, the true power of Ansible lies in playbooks.
|
||||
Why would you use ad-hoc tasks versus playbooks?
|
||||
|
||||
For instance, if you wanted to power off all of your lab for Christmas vacation,
|
||||
you could execute a quick one-liner in Ansible without writing a playbook.
|
||||
|
||||
For configuration management and deployments, though, you'll want to pick up on
|
||||
using '/usr/bin/ansible-playbook' -- the concepts you will learn here will
|
||||
port over directly to the playbook language.
|
||||
|
||||
(See :doc:`playbooks` for more information about those)
|
||||
|
||||
If you haven't read :doc:`intro_inventory` already, please look that over a bit first
|
||||
and then we'll get going.
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
.. _parallelism_and_shell_commands:
|
||||
|
||||
Parallelism and Shell Commands
|
||||
``````````````````````````````
|
||||
|
||||
Arbitrary example.
|
||||
|
||||
Let's use Ansible's command line tool to reboot all web servers in Atlanta, 10 at a time. First, let's
|
||||
set up SSH-agent so it can remember our credentials::
|
||||
|
||||
$ ssh-agent bash
|
||||
$ ssh-add ~/.ssh/id_rsa
|
||||
|
||||
If you don't want to use ssh-agent and want to instead SSH with a
|
||||
password instead of keys, you can with ``--ask-pass`` (``-k``), but
|
||||
it's much better to just use ssh-agent.
|
||||
|
||||
Now to run the command on all servers in a group, in this case,
|
||||
*atlanta*, in 10 parallel forks::
|
||||
|
||||
$ ansible atlanta -a "/sbin/reboot" -f 10
|
||||
|
||||
/usr/bin/ansible will default to running from your user account. If you do not like this
|
||||
behavior, pass in "-u username". If you want to run commands as a different user, it looks like this::
|
||||
|
||||
$ ansible atlanta -a "/usr/bin/foo" -u username
|
||||
|
||||
Often you'll not want to just do things from your user account. If you want to run commands through sudo::
|
||||
|
||||
$ ansible atlanta -a "/usr/bin/foo" -u username --sudo [--ask-sudo-pass]
|
||||
|
||||
Use ``--ask-sudo-pass`` (``-K``) if you are not using passwordless
|
||||
sudo. This will interactively prompt you for the password to use.
|
||||
Use of passwordless sudo makes things easier to automate, but it's not
|
||||
required.
|
||||
|
||||
It is also possible to sudo to a user other than root using
|
||||
``--sudo-user`` (``-U``)::
|
||||
|
||||
$ ansible atlanta -a "/usr/bin/foo" -u username -U otheruser [--ask-sudo-pass]
|
||||
|
||||
Ok, so those are basics. If you didn't read about patterns and groups yet, go back and read :doc:`intro_patterns`.
|
||||
|
||||
The ``-f 10`` in the above specifies the usage of 10 simultaneous
|
||||
processes to use. You can also set this in :doc:`intro_configuration` to avoid setting it again. The default is actually 5, which
|
||||
is really small and conservative. You are probably going to want to talk to a lot more simultaneous hosts so feel free
|
||||
to crank this up. If you have more hosts than the value set for the fork count, Ansible will talk to them, but it will
|
||||
take a little longer. Feel free to push this value as high as your system can handle it!
|
||||
|
||||
You can also select what Ansible "module" you want to run. Normally commands also take a ``-m`` for module name, but
|
||||
the default module name is 'command', so we didn't need to
|
||||
specify that all of the time. We'll use ``-m`` in later examples to
|
||||
run some other :doc:`modules`.
|
||||
|
||||
.. note::
|
||||
The :ref:`command` module does not
|
||||
support shell variables and things like piping. If we want to execute a module using a
|
||||
shell, use the 'shell' module instead. Read more about the differences on the :doc:`modules`
|
||||
page.
|
||||
|
||||
Using the :ref:`shell` module looks like this::
|
||||
|
||||
$ ansible raleigh -m shell -a 'echo $TERM'
|
||||
|
||||
When running any command with the Ansible *ad hoc* CLI (as opposed to
|
||||
:doc:`playbooks`), pay particular attention to shell quoting rules, so
|
||||
the local shell doesn't eat a variable before it gets passed to Ansible.
|
||||
For example, using double vs single quotes in the above example would
|
||||
evaluate the variable on the box you were on.
|
||||
|
||||
So far we've been demoing simple command execution, but most Ansible modules usually do not work like
|
||||
simple scripts. They make the remote system look like you state, and run the commands necessary to
|
||||
get it there. This is commonly referred to as 'idempotence', and is a core design goal of Ansible.
|
||||
However, we also recognize that running arbitrary commands is equally important, so Ansible easily supports both.
|
||||
|
||||
.. _file_transfer:
|
||||
|
||||
File Transfer
|
||||
`````````````
|
||||
|
||||
Here's another use case for the `/usr/bin/ansible` command line. Ansible can SCP lots of files to multiple machines in parallel.
|
||||
|
||||
To transfer a file directly to many different servers::
|
||||
|
||||
$ ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts"
|
||||
|
||||
If you use playbooks, you can also take advantage of the ``template`` module,
|
||||
which takes this another step further. (See module and playbook documentation).
|
||||
|
||||
The ``file`` module allows changing ownership and permissions on files. These
|
||||
same options can be passed directly to the ``copy`` module as well::
|
||||
|
||||
$ ansible webservers -m file -a "dest=/srv/foo/a.txt mode=600"
|
||||
$ ansible webservers -m file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"
|
||||
|
||||
The ``file`` module can also create directories, similar to ``mkdir -p``::
|
||||
|
||||
$ ansible webservers -m file -a "dest=/path/to/c mode=644 owner=mdehaan group=mdehaan state=directory"
|
||||
|
||||
As well as delete directories (recursively) and delete files::
|
||||
|
||||
$ ansible webservers -m file -a "dest=/path/to/c state=absent"
|
||||
|
||||
.. _managing_packages:
|
||||
|
||||
Managing Packages
|
||||
`````````````````
|
||||
|
||||
There are modules available for yum and apt. Here are some examples
|
||||
with yum.
|
||||
|
||||
Ensure a package is installed, but don't update it::
|
||||
|
||||
$ ansible webservers -m yum -a "name=acme state=installed"
|
||||
|
||||
Ensure a package is installed to a specific version::
|
||||
|
||||
$ ansible webservers -m yum -a "name=acme-1.5 state=installed"
|
||||
|
||||
Ensure a package is at the latest version::
|
||||
|
||||
$ ansible webservers -m yum -a "name=acme state=latest"
|
||||
|
||||
Ensure a package is not installed::
|
||||
|
||||
$ ansible webservers -m yum -a "name=acme state=removed"
|
||||
|
||||
Ansible has modules for managing packages under many platforms. If your package manager
|
||||
does not have a module available for it, you can install
|
||||
for other packages using the command module or (better!) contribute a module
|
||||
for other package managers. Stop by the mailing list for info/details.
|
||||
|
||||
.. _users_and_groups:
|
||||
|
||||
Users and Groups
|
||||
````````````````
|
||||
|
||||
The 'user' module allows easy creation and manipulation of
|
||||
existing user accounts, as well as removal of user accounts that may
|
||||
exist::
|
||||
|
||||
$ ansible all -m user -a "name=foo password=<crypted password here>"
|
||||
|
||||
$ ansible all -m user -a "name=foo state=absent"
|
||||
|
||||
See the :doc:`modules` section for details on all of the available options, including
|
||||
how to manipulate groups and group membership.
|
||||
|
||||
.. _from_source_control:
|
||||
|
||||
Deploying From Source Control
|
||||
`````````````````````````````
|
||||
|
||||
Deploy your webapp straight from git::
|
||||
|
||||
$ ansible webservers -m git -a "repo=git://foo.example.org/repo.git dest=/srv/myapp version=HEAD"
|
||||
|
||||
Since Ansible modules can notify change handlers it is possible to
|
||||
tell Ansible to run specific tasks when the code is updated, such as
|
||||
deploying Perl/Python/PHP/Ruby directly from git and then restarting
|
||||
apache.
|
||||
|
||||
.. _managing_services:
|
||||
|
||||
Managing Services
|
||||
`````````````````
|
||||
|
||||
Ensure a service is started on all webservers::
|
||||
|
||||
$ ansible webservers -m service -a "name=httpd state=started"
|
||||
|
||||
Alternatively, restart a service on all webservers::
|
||||
|
||||
$ ansible webservers -m service -a "name=httpd state=restarted"
|
||||
|
||||
Ensure a service is stopped::
|
||||
|
||||
$ ansible webservers -m service -a "name=httpd state=stopped"
|
||||
|
||||
.. _time_limited_background_operations:
|
||||
|
||||
Time Limited Background Operations
|
||||
``````````````````````````````````
|
||||
|
||||
Long running operations can be backgrounded, and their status can be
|
||||
checked on later. The same job ID is given to the same task on all
|
||||
hosts, so you won't lose track. If you kick hosts and don't want
|
||||
to poll, it looks like this::
|
||||
|
||||
$ ansible all -B 3600 -a "/usr/bin/long_running_operation --do-stuff"
|
||||
|
||||
If you do decide you want to check on the job status later, you can::
|
||||
|
||||
$ ansible all -m async_status -a "jid=123456789"
|
||||
|
||||
Polling is built-in and looks like this::
|
||||
|
||||
$ ansible all -B 1800 -P 60 -a "/usr/bin/long_running_operation --do-stuff"
|
||||
|
||||
The above example says "run for 30 minutes max (``-B``: 30*60=1800),
|
||||
poll for status (``-P``) every 60 seconds".
|
||||
|
||||
Poll mode is smart so all jobs will be started before polling will begin on any machine.
|
||||
Be sure to use a high enough ``--forks`` value if you want to get all of your jobs started
|
||||
very quickly. After the time limit (in seconds) runs out (``-B``), the process on
|
||||
the remote nodes will be terminated.
|
||||
|
||||
Typically you'll be only be backgrounding long-running
|
||||
shell commands or software upgrades only. Backgrounding the copy module does not do a background file transfer. :doc:`playbooks` also support polling, and have a simplified syntax for this.
|
||||
|
||||
.. _checking_facts:
|
||||
|
||||
Gathering Facts
|
||||
```````````````
|
||||
|
||||
Facts are described in the playbooks section and represent discovered variables about a
|
||||
system. These can be used to implement conditional execution of tasks but also just to get ad-hoc information about your system. You can see all facts via::
|
||||
|
||||
$ ansible all -m setup
|
||||
|
||||
Its also possible to filter this output to just export certain facts, see the "setup" module documentation for details.
|
||||
|
||||
Read more about facts at :doc:`playbooks_variables` once you're ready to read up on :doc:`playbooks`.
|
||||
|
||||
.. _limiting_hosts:
|
||||
|
||||
Limiting Selected Hosts
|
||||
```````````````````````
|
||||
|
||||
What hosts you select to manage can be additionally constrained by using the '--limit' parameter or
|
||||
by using 'batch' (or 'range') selectors.
|
||||
|
||||
As mentioned above, patterns can be strung together to select hosts in more than one group::
|
||||
|
||||
$ ansible webservers:dbservers -m command -a "/bin/foo xyz"
|
||||
|
||||
This is an "or" condition. If you want to further constrain the selection, use --limit, which
|
||||
also works with ``ansible-playbook``::
|
||||
|
||||
$ ansible webservers:dbservers -m command -a "/bin/foo xyz" --limit region
|
||||
|
||||
Assuming version 0.9 or later, as with other host patterns, values to limit can be separated with ";", ":", or ",".
|
||||
|
||||
Now let's talk about range selection. Suppose you have 1000 servers in group 'datacenter', but only want to target one at a time. This is also easy::
|
||||
|
||||
$ ansible webservers[0-99] -m command -a "/bin/foo xyz"
|
||||
$ ansible webservers[100-199] -m command -a "/bin/foo xyz"
|
||||
|
||||
This will select the first 100, then the second 100, host entries in the webservers group. (It does not matter
|
||||
what their names or IP addresses are).
|
||||
|
||||
Both of these methods can be used at the same time, and ranges can also be passed to the --limit parameter.
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`intro_configuration`
|
||||
All about the Ansible config file
|
||||
:doc:`modules`
|
||||
A list of available modules
|
||||
:doc:`playbooks`
|
||||
Using Ansible for configuration management & deployment
|
||||
`Mailing List <http://groups.google.com/group/ansible-project>`_
|
||||
Questions? Help? Ideas? Stop by the list on Google Groups
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
600
docsite/rst/intro_configuration.rst
Normal file
600
docsite/rst/intro_configuration.rst
Normal file
|
@ -0,0 +1,600 @@
|
|||
|
||||
The Ansible Configuration File
|
||||
++++++++++++++++++++++++++++++
|
||||
|
||||
.. highlight:: bash
|
||||
|
||||
Certain things in Ansible are adjustable in a configuration file. In general, the stock configuration is probably
|
||||
right for most users, but that doesn't mean you might not want to change them.
|
||||
|
||||
The mechanism for doing this is the "ansible.cfg" file, which is looked for in the following locations:
|
||||
|
||||
* /etc/ansible/ansible.cfg
|
||||
* ~/.ansible.cfg
|
||||
* ansible.cfg (in the playbook directory)
|
||||
|
||||
If multiple file locations matching the above exist, the last location on the above list is used. Settings in files
|
||||
are not merged together.
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
.. _getting_the_latest_configuration:
|
||||
|
||||
Getting the latest configuration
|
||||
````````````````````````````````
|
||||
|
||||
If installing ansible from a package manager, the latest ansible.cfg should be present in /etc/ansible, possibly
|
||||
as a ".rpmnew" file (or other) as appropriate in the case of updates.
|
||||
|
||||
If you have installed from pip or from source, however, you may want to create this file in order to override
|
||||
default settings in Ansible.
|
||||
|
||||
You may wish to consult the `ansible.cfg in source control <https://raw.github.com/ansible/ansible/devel/examples/ansible.cfg>_` for
|
||||
all of the possible latest values.
|
||||
|
||||
.. _environmental_configuration:
|
||||
|
||||
Environmental configuration
|
||||
```````````````````````````
|
||||
|
||||
Ansible also allows configuration of settings via environment variables. If these environment variables are set, they will
|
||||
override any setting loaded from the configuration file. These variables are for brevity not defined here, but look in 'constants.py' in the source tree if you want to use these. They are mostly considered to be a legacy system as compared to the config file, but are equally valid.
|
||||
|
||||
.. _config_values_by_section:
|
||||
|
||||
Explanation of values by section
|
||||
````````````````````````````````
|
||||
|
||||
The configuration file is broken up into sections. Most options are in the "general" section but some sections of the file
|
||||
are specific to certain connection types.
|
||||
|
||||
.. _general_defaults:
|
||||
|
||||
General defaults
|
||||
----------------
|
||||
|
||||
In the [defaults] section of ansible.cfg, the following settings are tunable:
|
||||
|
||||
.. _action_plugins:
|
||||
|
||||
action_plugins
|
||||
==============
|
||||
|
||||
Actions are pieces of code in ansible that enable things like module execution, templating, and so forth.
|
||||
|
||||
This is a developer-centric feature that allows low-level extensions around Ansible to be loaded from
|
||||
different locations::
|
||||
|
||||
action_plugins = /usr/share/ansible_plugins/action_plugins
|
||||
|
||||
Most users will not need to use this feature. See :doc:`developing_plugins` for more details.
|
||||
|
||||
.. _ansible_managed:
|
||||
|
||||
ansible_managed
|
||||
===============
|
||||
|
||||
Ansible-managed is a string that can be inserted into files written by Ansible's config templating system, if you use
|
||||
a string like::
|
||||
|
||||
{{ ansible_managed }}
|
||||
|
||||
The default configuration shows who modified a file and when::
|
||||
|
||||
ansible_managed = Ansible managed: {file} modified on %Y-%m-%d %H:%M:%S by {uid} on {host}
|
||||
|
||||
This is useful to tell users that a file has been placed by Ansible and manual changes are likely to be overwritten.
|
||||
|
||||
.. _ask_pass:
|
||||
|
||||
ask_pass
|
||||
========
|
||||
|
||||
This controls whether an Ansible playbook should prompt for a password by default. The default behavior is no::
|
||||
|
||||
#ask_pass=True
|
||||
|
||||
If using SSH keys for authentication, it's probably not needed to change this setting.
|
||||
|
||||
.. _ask_sudo_pass:
|
||||
|
||||
ask_sudo_pass
|
||||
=============
|
||||
|
||||
Similar to ask_pass, this controls whether an Ansible playbook should prompt for a sudo password by default when
|
||||
sudoing. The default behavior is also no::
|
||||
|
||||
#ask_sudo_pass=True
|
||||
|
||||
Users on platforms where sudo passwords are enabled should consider changing this setting.
|
||||
|
||||
.. _callback_plugins:
|
||||
|
||||
callback_plugins
|
||||
================
|
||||
|
||||
This is a developer-centric feature that allows low-level extensions around Ansible to be loaded from
|
||||
different locations::
|
||||
|
||||
action_plugins = /usr/share/ansible_plugins/action_plugins
|
||||
|
||||
Most users will not need to use this feature. See :doc:`developing_plugins` for more details
|
||||
|
||||
.. _connection_plugins:
|
||||
|
||||
connection_plugins
|
||||
==================
|
||||
|
||||
This is a developer-centric feature that allows low-level extensions around Ansible to be loaded from
|
||||
different locations::
|
||||
|
||||
action_plugins = /usr/share/ansible_plugins/action_plugins
|
||||
|
||||
Most users will not need to use this feature. See :doc:`developing_plugins` for more details
|
||||
|
||||
.. _deprecation_warnings:
|
||||
|
||||
deprecation_warnings
|
||||
====================
|
||||
|
||||
.. versionadded:: 1.3
|
||||
|
||||
Allows disabling of deprecating warnings in ansible-playbook output::
|
||||
|
||||
deprecation_warnings = True
|
||||
|
||||
Deprecation warnings indicate usage of legacy features that are slated for removal in a future release of Ansible.
|
||||
|
||||
.. _display_skipped_hosts:
|
||||
|
||||
display_skipped_hosts
|
||||
=====================
|
||||
|
||||
If set to `False`, ansible will not display any status for a task that is skipped. The default behavior is to display skipped tasks::
|
||||
|
||||
#display_skipped_hosts=True
|
||||
|
||||
Note that Ansible will always show the task header for any task, regardless of whether or not the task is skipped.
|
||||
|
||||
.. _error_on_undefined_vars:
|
||||
|
||||
error_on_undefined_vars
|
||||
=======================
|
||||
|
||||
On by default since Ansible 1.3, this causes ansible to fail steps that reference variable names that are likely
|
||||
typoed::
|
||||
|
||||
#error_on_undefined_vars=True
|
||||
|
||||
If set to False, any '{{ template_expression }}' that contains undefined variables will be rendered in a template
|
||||
or ansible action line exactly as written.
|
||||
|
||||
.. _executable:
|
||||
|
||||
executable
|
||||
==========
|
||||
|
||||
This indicates the command to use to spawn a shell under a sudo environment. Users may need to change this in
|
||||
rare instances to /bin/bash in rare instances wehn sudo is constrained, but in most cases it may be left as is::
|
||||
|
||||
#executable = /bin/bash
|
||||
|
||||
.. _filter_plugins:
|
||||
|
||||
filter_plugins
|
||||
==============
|
||||
|
||||
This is a developer-centric feature that allows low-level extensions around Ansible to be loaded from
|
||||
different locations::
|
||||
|
||||
action_plugins = /usr/share/ansible_plugins/action_plugins
|
||||
|
||||
Most users will not need to use this feature. See :doc:`developing_plugins` for more details
|
||||
|
||||
.. _forks:
|
||||
|
||||
forks
|
||||
=====
|
||||
|
||||
This is the default number of parallel processes to spawn when communicating with remote hosts. Since Ansible 1.3,
|
||||
the fork number is automatically limited to the number of possible hosts, so this is really a limit of how much
|
||||
network and CPU load you think you can handle. Many users may set this to 50, some set it to 500 or more. If you
|
||||
have a large number of hosts, higher values will make actions across all of those hosts complete faster. The default
|
||||
is very very conservative::
|
||||
|
||||
forks=5
|
||||
|
||||
|
||||
hash_behavior
|
||||
=============
|
||||
|
||||
Ansible by default will override variables in specific precedence orders, as described in :doc:`playbooks_variables`. When a variable
|
||||
of higher precedence wins, it will replace the other value.
|
||||
|
||||
Some users prefer that variables that are hashes (aka 'dictionaries' in Python terms) are merged together. This setting is called 'merge'. This is not the default behavior and it does not affect variables whose values are scalars (integers, strings) or
|
||||
arrays. We generally recommend not using this setting unless you think you have an absolute need for it, and playbooks in the
|
||||
official examples repos do not use this setting::
|
||||
|
||||
#hash_behavior=replace
|
||||
|
||||
The valid values are either 'replace' (the default) or 'merge'.
|
||||
|
||||
.. _hostfile:
|
||||
|
||||
hostfile
|
||||
========
|
||||
|
||||
This is the default location of the inventory file, script, or directory that Ansible will use to determine what hosts it has available
|
||||
to talk to::
|
||||
|
||||
hostfile = /etc/ansible/hosts
|
||||
|
||||
.. _host_key_checking:
|
||||
|
||||
host_key_checking
|
||||
=================
|
||||
|
||||
As described in :doc:`intro_getting_started`, host key checking is on by default in Ansible 1.3 and later. If you understand the
|
||||
implications and wish to disable it, you may do so here by setting the value to False::
|
||||
|
||||
host_key_checking=True
|
||||
|
||||
.. _jinja2_extensions:
|
||||
|
||||
jinja2_extensions
|
||||
=================
|
||||
|
||||
This is a developer-specific feature that allows enabling additional Jinja2 extensions::
|
||||
|
||||
jinja2_extensions = jinja2.ext.do,jinja2.ext.i18n
|
||||
|
||||
If you do not know what these do, you probably don't need to change this setting :)
|
||||
|
||||
.. _legacy_playbook_variables:
|
||||
|
||||
legacy_playbook_variables
|
||||
=========================
|
||||
|
||||
Ansible prefers to use Jinja2 syntax '{{ like_this }}' to indicate a variable should be substituted in a particular string. However,
|
||||
older versions of playbooks used a more Perl-style syntax. This syntax was undesirable as it frequently conflicted with bash and
|
||||
was hard to explain to new users when referencing complicated variable hierarchies, so we have standardized on the '{{ jinja2 }}' way.
|
||||
|
||||
To ensure a string like '$foo' is not indadvertedly replaced in a Perl or Bash script template, the old form of templating (which is
|
||||
still enabled as of Ansible 1.4) can be disabled like so ::
|
||||
|
||||
legacy_playbook_variables = no
|
||||
|
||||
.. _library:
|
||||
|
||||
library
|
||||
=======
|
||||
|
||||
This is the default location Ansible looks to find modules::
|
||||
|
||||
library = /usr/share/ansible
|
||||
|
||||
Ansible knows how to look in multiple locations if you feed it a colon separated path, and it also will look for modules in the
|
||||
"./library" directory alongside a playbook.
|
||||
|
||||
.. _log_path:
|
||||
|
||||
log_path
|
||||
========
|
||||
|
||||
If present and configured in ansible.cfg, Ansible will log information about executions at the designated location. Be sure
|
||||
the user running Ansible has permissions on the logfile.
|
||||
|
||||
log_path=/var/log/ansible.log
|
||||
|
||||
This behavior is not on by default. Note that ansible will, without this setting, record module arguments called to the
|
||||
syslog of managed machines. Password arguments are excluded.
|
||||
|
||||
For Enterprise users seeking more detailed logging history, you may be interested in `AnsibleWorks AWX <http://ansibleworks.com/ansibleworks-awx>`_.
|
||||
|
||||
.. _lookup_plugins:
|
||||
|
||||
lookup_plugins
|
||||
==============
|
||||
|
||||
This is a developer-centric feature that allows low-level extensions around Ansible to be loaded from
|
||||
different locations::
|
||||
|
||||
action_plugins = /usr/share/ansible_plugins/action_plugins
|
||||
|
||||
Most users will not need to use this feature. See :doc:`developing_plugins` for more details
|
||||
|
||||
.. _module_name:
|
||||
|
||||
module_name
|
||||
===========
|
||||
|
||||
This is the default module name (-m) value for /usr/bin/ansible. The default is the 'command' module.
|
||||
Remember the command module doesn't support shell variables, pipes, or quotes, so you might wish to change
|
||||
it to 'shell'::
|
||||
|
||||
module_name = command
|
||||
|
||||
.. _nocolor:
|
||||
|
||||
nocolor
|
||||
=======
|
||||
|
||||
By default ansible will try to colorize output to give a better indication of failure and status information.
|
||||
If you dislike this behavior you can turn it off by setting 'nocolor' to 0::
|
||||
|
||||
nocolor=0
|
||||
|
||||
.. _nocows:
|
||||
|
||||
nocows
|
||||
======
|
||||
|
||||
By default ansible will take advantage of cowsay if installed to make /usr/bin/ansible-playbook runs more exciting.
|
||||
Why? We believe systems management should be a happy experience. If you do not like the cows, you can disable them
|
||||
by setting 'nocows' to 1::
|
||||
|
||||
nocows=0
|
||||
|
||||
.. _pattern:
|
||||
|
||||
pattern
|
||||
=======
|
||||
|
||||
This is the default group of hosts to talk to in a playbook if no "hosts:" stanza is supplied. The default is to talk
|
||||
to all hosts. You may wish to change this to protect yourself from surprises::
|
||||
|
||||
hosts=*
|
||||
|
||||
Note that /usr/bin/ansible always requires a host pattern and does not use this setting, only /usr/bin/ansible-playbook.
|
||||
|
||||
.. _poll_interval:
|
||||
|
||||
poll_interval
|
||||
=============
|
||||
|
||||
For asynchronous tasks in Ansible (covered in :doc:`playbooks_async`), this is how often to check back on the status of those
|
||||
tasks when an explicit poll interval is not supplied. The default is a reasonably moderate 15 seconds which is a tradeoff
|
||||
between checking in frequently and providing a quick turnaround when something may have completed::
|
||||
|
||||
poll_interval=15
|
||||
|
||||
.. _private_key_file:
|
||||
|
||||
private_key_file
|
||||
================
|
||||
|
||||
If you are using a pem file to authenticate with machines rather than SSH agent or passwords, you can set the default
|
||||
value here to avoid re-specifying --ansible-private-keyfile with every invocation::
|
||||
|
||||
private_key_file=/path/to/file.pem
|
||||
|
||||
.. _remote_port:
|
||||
|
||||
remote_port
|
||||
===========
|
||||
|
||||
This sets the default SSH port on all of your systems, for systems that didn't specify an alternative value in inventory.
|
||||
The default is the standard 22::
|
||||
|
||||
remote_port = 22
|
||||
|
||||
.. _remote_tmp:
|
||||
|
||||
remote_tmp
|
||||
==========
|
||||
|
||||
Ansible works by transferring modules to your remote machines, running them, and then cleaning up after itself. In some
|
||||
cases, you may not wish to use the default location and would like to change the path. You can do so by altering this
|
||||
setting::
|
||||
|
||||
remote_temp = $HOME/.ansible/tmp
|
||||
|
||||
The default is to use a subdirectory of the user's home directory. Ansible will then choose a random directory name
|
||||
inside this location.
|
||||
|
||||
.. _remote_user:
|
||||
|
||||
remote_user
|
||||
===========
|
||||
|
||||
This is the default username ansible will connect as for /usr/bin/ansible-playbook. Note that /usr/bin/ansible will
|
||||
always default to the current user::
|
||||
|
||||
remote_user = root
|
||||
|
||||
.. _roles_path:
|
||||
|
||||
roles_path
|
||||
==========
|
||||
|
||||
.. versionadded: '1.4'
|
||||
|
||||
The roles path indicate additional directories beyond the 'roles/' subdirectory of a playbook project to search to find Ansible
|
||||
roles. For instance, if there was a source control repository of common roles and a different repository of playbooks, you might
|
||||
choose to establish a convention to checkout roles in /opt/mysite/roles like so::
|
||||
|
||||
roles_path = /opt/mysite/roles
|
||||
|
||||
Roles will be first searched for in the playbook directory. Should a role not be found, it will indicate all the possible paths
|
||||
that were searched.
|
||||
|
||||
.. _sudo_exe:
|
||||
|
||||
sudo_exe
|
||||
========
|
||||
|
||||
If using an alternative sudo implementation on remote machines, the path to sudo can be replaced here provided
|
||||
the sudo implementation is matching CLI flags with the standard sudo::
|
||||
|
||||
sudo_exe=sudo
|
||||
|
||||
.. _sudo_flags:
|
||||
|
||||
sudo_flags
|
||||
==========
|
||||
|
||||
Additional flags to pass to sudo when engaging sudo support. The default is '-H' which preserves the environment
|
||||
of the original user. In some situations you may wish to add or remote flags, but in general most users
|
||||
will not need to change this setting::
|
||||
|
||||
sudo_flags=-H
|
||||
|
||||
.. _sudo_user:
|
||||
|
||||
sudo_user
|
||||
=========
|
||||
|
||||
This is the default user to sudo to if --sudo-user is not specified or 'sudo_user' is not specified in an Ansible
|
||||
playbook. The default is the most logical: 'root'::
|
||||
|
||||
sudo_user=root
|
||||
|
||||
.. _timeout:
|
||||
|
||||
timeout
|
||||
=======
|
||||
|
||||
This is the default SSH timeout to use on connection attempts::
|
||||
|
||||
timeout = 10
|
||||
|
||||
.. _transport:
|
||||
|
||||
transport
|
||||
=========
|
||||
|
||||
This is the default transport ot use if "-c <transport_name>" is not specified to /usr/bin/ansible or /usr/bin/ansible-playbook.
|
||||
The default is 'smart', which will use 'ssh' (OpenSSH based) if the local operating system is new enough to support ControlPersist
|
||||
technology, and then will otherwise use 'paramiko'. Other transport options include 'local', 'chroot', 'jail', and so on.
|
||||
|
||||
Users should usually leave this setting as 'smart' and let their playbooks choose an alternate setting when needed with the
|
||||
'connection:' play parameter.
|
||||
|
||||
.. _vars_plugins:
|
||||
|
||||
vars_plugins
|
||||
============
|
||||
|
||||
This is a developer-centric feature that allows low-level extensions around Ansible to be loaded from
|
||||
different locations::
|
||||
|
||||
action_plugins = /usr/share/ansible_plugins/action_plugins
|
||||
|
||||
Most users will not need to use this feature. See :doc:`developing_plugins` for more details
|
||||
|
||||
.. _paramiko_settings:
|
||||
|
||||
Paramiko Specific Settings
|
||||
--------------------------
|
||||
|
||||
Paramiko is the default SSH connection implementation on Enterprise Linux 6 or earlier, and is not used by default on other
|
||||
platforms. Settings live under the [paramiko] header.
|
||||
|
||||
.. _record_host_keys:
|
||||
|
||||
record_host_keys
|
||||
================
|
||||
|
||||
The default setting of yes will record newly discovered and approved (if host key checking is enabled) hosts in the user's hostfile.
|
||||
This setting may be inefficient for large numbers of hosts, and in those situations, using the ssh transport is definitely recommended
|
||||
instead. Setting it to False will improve performance and is recommended when host key checking is disabled::
|
||||
|
||||
record_host_keys=True
|
||||
|
||||
.. _openssh_settings:
|
||||
|
||||
OpenSSH Specific Settings
|
||||
-------------------------
|
||||
|
||||
Under the [ssh] header, the following settings are tunable for SSH connections. OpenSSH is the default connection type for Ansible
|
||||
on OSes that are new enough to support ControlPersist. (This means basically all operating systems except Enterprise Linux 6 or earlier).
|
||||
|
||||
.. _ssh_args:
|
||||
|
||||
ssh_args
|
||||
========
|
||||
|
||||
If set, this will pass a specific set of options to Ansible rather than Ansible's usual defaults::
|
||||
|
||||
ssh_args = -o ControlMaster=auto -o ControlPersist=60s
|
||||
|
||||
In particular, users may wish to raise the ControlPersist time to encourage performance. A value of 30 minutes may
|
||||
be appropriate.
|
||||
|
||||
.. _control_path:
|
||||
|
||||
control_path
|
||||
============
|
||||
|
||||
This is the location to save ControlPath sockets. This defaults to::
|
||||
|
||||
control_path=%(directory)s/ansible-ssh-%%h-%%p-%%r
|
||||
|
||||
On some systems with very long hostnames or very long path names (caused by long user names or
|
||||
deeply nested home directories) this can exceed the character limit on
|
||||
file socket names (108 characters for most platforms). In that case, you
|
||||
may wish to shorten the string to something like the below::
|
||||
|
||||
control_path = %(directory)s/%%h-%%r
|
||||
|
||||
Ansible 1.4 and later will instruct users to run with "-vvvv" in situations where it hits this problem
|
||||
and if so it is easy to tell there is too long of a Control Path filename. This may be frequently
|
||||
encountered on EC2.
|
||||
|
||||
.. _scp_if_ssh:
|
||||
|
||||
scp_if_ssh
|
||||
==========
|
||||
|
||||
Occasionally users may be managing a remote system that doesn't have SFTP enabled. If set to True, we can
|
||||
cause scp to be used to transfer remote files instead::
|
||||
|
||||
scp_if_ssh=False
|
||||
|
||||
There's really no reason to change this unless problems are encountered, and then there's also no real drawback
|
||||
to managing the switch. Most environments support SFTP by default and this doesn't usually need to be changed.
|
||||
|
||||
.. _accelerate_settings:
|
||||
|
||||
Accelerate Mode Settings
|
||||
------------------------
|
||||
|
||||
Under the [accelerate] header, the following settings are tunable for :doc:`playbooks_acceleration`
|
||||
|
||||
.. _accelerate_port:
|
||||
|
||||
accelerate_port
|
||||
===============
|
||||
|
||||
.. versionadded:: 1.3
|
||||
|
||||
This is the port to use for accelerate mode::
|
||||
|
||||
accelerate_port = 5099
|
||||
|
||||
.. _accelerate_timeout:
|
||||
|
||||
accelerate_timeout
|
||||
==================
|
||||
|
||||
.. versionadded:: 1.4
|
||||
|
||||
This setting controls the timeout for receiving data from a client. If no data is received during this time, the socket connection will be closed. A keepalive packet is sent back to the controller every 15 seconds, so this timeout should not be set lower than 15 (by default, the timeout is 30 seconds)::
|
||||
|
||||
accelerate_timeout = 30
|
||||
|
||||
.. _accelerate_connect_timeout:
|
||||
|
||||
accelerate_connect_timeout
|
||||
==========================
|
||||
|
||||
.. versionadded:: 1.4
|
||||
|
||||
This setting controls the timeout for the socket connect call, and should be kept relatively low. The connection to the `accelerate_port` will be attempted 3 times before Ansible will fall back to ssh or paramiko (depending on your default connection setting) to try and start the accelerate daemon remotely. The default setting is 1.0 seconds::
|
||||
|
||||
accelerate_connect_timeout = 1.0
|
||||
|
||||
Note, this value can be set to less than one second, however it is probably not a good idea to do so unless you're on a very fast and reliable LAN. If you're connecting to systems over the internet, it may be necessary to increase this timeout.
|
||||
|
||||
|
234
docsite/rst/intro_dynamic_inventory.rst
Normal file
234
docsite/rst/intro_dynamic_inventory.rst
Normal file
|
@ -0,0 +1,234 @@
|
|||
.. _dynamic_inventory:
|
||||
|
||||
Dynamic Inventory
|
||||
=================
|
||||
|
||||
Often a user of a configuration management system will want to keep inventory
|
||||
in a different software system. Ansible provides a basic text-based system as described in
|
||||
:doc:`intro_inventory` but what if you want to use something else?
|
||||
|
||||
Frequent examples include pulling inventory from a cloud provider, LDAP, `Cobbler <http://cobbler.github.com>`_,
|
||||
or a piece of expensive enterprisey CMDB software.
|
||||
|
||||
Ansible easily supports all of these options via an external inventory system. The plugins directory contains some of these already -- including options for EC2/Eucalyptus, Rackspace Cloud, and OpenStack, examples of some of which will be detailed below.
|
||||
|
||||
`AnsibleWorks AWX <http://ansibleworks.com/ansibleworks-awx/>`_ also provides a database to store inventory results that is both web and REST Accessible. AWX syncs with all Ansible dynamic inventory sources. By having a database record of all of your hosts, it's easy to correlate past event history and see which ones have had failures on their last playbook runs.
|
||||
|
||||
For information about writing your own dynamic inventory source, see :doc:`developing_inventory`.
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
|
||||
.. _cobbler_example:
|
||||
|
||||
Example: The Cobbler External Inventory Script
|
||||
``````````````````````````````````````````````
|
||||
|
||||
It is expected that many Ansible users with a reasonable amount of physical hardware may also be `Cobbler <http://cobbler.github.com>`_ users. (note: Cobbler was originally written by Michael DeHaan and is now lead by James Cammarata, who also works for AnsibleWorks).
|
||||
|
||||
While primarily used to kickoff OS installations and manage DHCP and DNS, Cobbler has a generic
|
||||
layer that allows it to represent data for multiple configuration management systems (even at the same time), and has
|
||||
been referred to as a 'lightweight CMDB' by some admins. This particular script will communicate with Cobbler
|
||||
using Cobbler's XMLRPC API.
|
||||
|
||||
To tie Ansible's inventory to Cobbler (optional), copy `this script <https://raw.github.com/ansible/ansible/devel/plugins/inventory/cobbler.py>`_ to /etc/ansible and `chmod +x` the file. cobblerd will now need
|
||||
to be running when you are using Ansible.
|
||||
|
||||
When running Ansible, to use this inventory, then path the script with the "-i /etc/ansible/cobbler.py" parameter.
|
||||
|
||||
First test the script by running `./etc/ansible/hosts` directly. You should see some JSON data output, but it may not have
|
||||
anything in it just yet.
|
||||
|
||||
Let's explore what this does. In cobbler, assume a scenario somewhat like the following::
|
||||
|
||||
cobbler profile add --name=webserver --distro=CentOS6-x86_64
|
||||
cobbler profile edit --name=webserver --mgmt-classes="webserver" --ksmeta="a=2 b=3"
|
||||
cobbler system edit --name=foo --dns-name="foo.example.com" --mgmt-classes="atlanta" --ksmeta="c=4"
|
||||
cobbler system edit --name=bar --dns-name="bar.example.com" --mgmt-classes="atlanta" --ksmeta="c=5"
|
||||
|
||||
In the example above, the system 'foo.example.com' will be addressable by ansible directly, but will also be addressable when using the group names 'webserver' or 'atlanta'. Since Ansible uses SSH, we'll try to contract system foo over 'foo.example.com', only, never just 'foo'. Similarly, if you try "ansible foo" it wouldn't find the system... but "ansible 'foo*'" would, because the system DNS name starts with 'foo'.
|
||||
|
||||
The script doesn't just provide host and group info. In addition, as a bonus, when the 'setup' module is run (which happens automatically when using playbooks), the variables 'a', 'b', and 'c' will all be auto-populated in the templates::
|
||||
|
||||
# file: /srv/motd.j2
|
||||
Welcome, I am templated with a value of a={{ a }}, b={{ b }}, and c={{ c }}
|
||||
|
||||
Which could be executed just like this::
|
||||
|
||||
ansible webserver -m setup
|
||||
ansible webserver -m template -a "src=/tmp/motd.j2 dest=/etc/motd"
|
||||
|
||||
.. note::
|
||||
The name 'webserver' came from cobbler, as did the variables for
|
||||
the config file. You can still pass in your own variables like
|
||||
normal in Ansible, but variables from the external inventory script
|
||||
will override any that have the same name.
|
||||
|
||||
So, with the template above (motd.j2), this would result in the following data being written to /etc/motd for system 'foo'::
|
||||
|
||||
Welcome, I am templated with a value of a=2, b=3, and c=4
|
||||
|
||||
And on system 'bar' (bar.example.com)::
|
||||
|
||||
Welcome, I am templated with a value of a=2, b=3, and c=5
|
||||
|
||||
And technically, though there is no major good reason to do it, this also works too::
|
||||
|
||||
ansible webserver -m shell -a "echo {{ a }}"
|
||||
|
||||
So in other words, you can use those variables in arguments/actions as well.
|
||||
|
||||
.. _aws_example:
|
||||
|
||||
Example: AWS EC2 External Inventory Script
|
||||
``````````````````````````````````````````
|
||||
|
||||
If you use Amazon Web Services EC2, maintaining an inventory file might not be the best approach, because hosts may come and go over time, be managed by external applications, or you might even be using AWS autoscaling. For this reason, you can use the `EC2 external inventory <https://raw.github.com/ansible/ansible/devel/plugins/inventory/ec2.py>`_ script.
|
||||
|
||||
You can use this script in one of two ways. The easiest is to use Ansible's ``-i`` command line option and specify the path to the script after
|
||||
marking it executable.
|
||||
|
||||
ansible -i ec2.py -u ubuntu us-east-1d -m ping
|
||||
|
||||
The second option is to copy the script to `/etc/ansible/hosts` and `chmod +x` it. You will also need to copy the `ec2.ini <https://raw.github.com/ansible/ansible/devel/plugins/inventory/ec2.ini>`_ file to `/etc/ansible/ec2.ini`. Then you can run ansible as you would normally.
|
||||
|
||||
To successfully make an API call to AWS, you will need to configure Boto (the Python interface to AWS). There are a `variety of methods <http://docs.pythonboto.org/en/latest/boto_config_tut.html>`_ available, but the simplest is just to export two environment variables:
|
||||
|
||||
export AWS_ACCESS_KEY_ID='AK123'
|
||||
export AWS_SECRET_ACCESS_KEY='abc123'
|
||||
|
||||
You can test the script by itself to make sure your config is correct
|
||||
|
||||
cd plugins/inventory
|
||||
./ec2.py --list
|
||||
|
||||
After a few moments, you should see your entire EC2 inventory across all regions in JSON.
|
||||
|
||||
Since each region requires its own API call, if you are only using a small set of regions, feel free to edit ``ec2.ini`` and list only the regions you are interested in. There are other config options in ``ec2.ini`` including cache control, and destination variables.
|
||||
|
||||
At their heart, inventory files are simply a mapping from some name to a destination address. The default ``ec2.ini`` settings are configured for running Ansible from outside EC2 (from your laptop for example) -- and this is not the most efficient way to manage EC2.
|
||||
|
||||
If you are running Ansible from within EC2, internal DNS names and IP addresses may make more sense than public DNS names. In this case, you can modify the ``destination_variable`` in ``ec2.ini`` to be the private DNS name of an instance. This is particularly important when running Ansible within a private subnet inside a VPC, where the only way to access an instance is via its private IP address. For VPC instances, `vpc_destination_variable` in ``ec2.ini`` provides a means of using which ever `boto.ec2.instance variable <http://docs.pythonboto.org/en/latest/ref/ec2.html#module-boto.ec2.instance>`_ makes the most sense for your use case.
|
||||
|
||||
The EC2 external inventory provides mappings to instances from several groups:
|
||||
|
||||
Instance ID
|
||||
These are groups of one since instance IDs are unique.
|
||||
e.g.
|
||||
``i-00112233``
|
||||
``i-a1b1c1d1``
|
||||
|
||||
Region
|
||||
A group of all instances in an AWS region.
|
||||
e.g.
|
||||
``us-east-1``
|
||||
``us-west-2``
|
||||
|
||||
Availability Zone
|
||||
A group of all instances in an availability zone.
|
||||
e.g.
|
||||
``us-east-1a``
|
||||
``us-east-1b``
|
||||
|
||||
Security Group
|
||||
Instances belong to one or more security groups. A group is created for each security group, with all characters except alphanumerics, dashes (-) converted to underscores (_). Each group is prefixed by ``security_group_``
|
||||
e.g.
|
||||
``security_group_default``
|
||||
``security_group_webservers``
|
||||
``security_group_Pete_s_Fancy_Group``
|
||||
|
||||
Tags
|
||||
Each instance can have a variety of key/value pairs associated with it called Tags. The most common tag key is 'Name', though anything is possible. Each key/value pair is its own group of instances, again with special characters converted to underscores, in the format ``tag_KEY_VALUE``
|
||||
e.g.
|
||||
``tag_Name_Web``
|
||||
``tag_Name_redis-master-001``
|
||||
``tag_aws_cloudformation_logical-id_WebServerGroup``
|
||||
|
||||
When the Ansible is interacting with a specific server, the EC2 inventory script is called again with the ``--host HOST`` option. This looks up the HOST in the index cache to get the instance ID, and then makes an API call to AWS to get information about that specific instance. It then makes information about that instance available as variables to your playbooks. Each variable is prefixed by ``ec2_``. Here are some of the variables available:
|
||||
|
||||
- ec2_architecture
|
||||
- ec2_description
|
||||
- ec2_dns_name
|
||||
- ec2_id
|
||||
- ec2_image_id
|
||||
- ec2_instance_type
|
||||
- ec2_ip_address
|
||||
- ec2_kernel
|
||||
- ec2_key_name
|
||||
- ec2_launch_time
|
||||
- ec2_monitored
|
||||
- ec2_ownerId
|
||||
- ec2_placement
|
||||
- ec2_platform
|
||||
- ec2_previous_state
|
||||
- ec2_private_dns_name
|
||||
- ec2_private_ip_address
|
||||
- ec2_public_dns_name
|
||||
- ec2_ramdisk
|
||||
- ec2_region
|
||||
- ec2_root_device_name
|
||||
- ec2_root_device_type
|
||||
- ec2_security_group_ids
|
||||
- ec2_security_group_names
|
||||
- ec2_spot_instance_request_id
|
||||
- ec2_state
|
||||
- ec2_state_code
|
||||
- ec2_state_reason
|
||||
- ec2_status
|
||||
- ec2_subnet_id
|
||||
- ec2_tag_Name
|
||||
- ec2_tenancy
|
||||
- ec2_virtualization_type
|
||||
- ec2_vpc_id
|
||||
|
||||
Both ``ec2_security_group_ids`` and ``ec2_security_group_names`` are comma-separated lists of all security groups. Each EC2 tag is a variable in the format ``ec2_tag_KEY``.
|
||||
|
||||
To see the complete list of variables available for an instance, run the script by itself::
|
||||
|
||||
cd plugins/inventory
|
||||
./ec2.py --host ec2-12-12-12-12.compute-1.amazonaws.com
|
||||
|
||||
Note that the AWS inventory script will cache results to avoid repeated API calls, and this cache setting is configurable in ec2.ini. To
|
||||
explicitly clear the cache, you can run the ec2.py script with the '--refresh-cache' parameter.
|
||||
|
||||
.. _other_inventory_scripts:
|
||||
|
||||
Other inventory scripts
|
||||
```````````````````````
|
||||
|
||||
In addition to Cobbler and EC2, inventory scripts are also available for::
|
||||
|
||||
BSD Jails
|
||||
Digital Ocean
|
||||
Linode
|
||||
OpenShift
|
||||
OpenStack Nova
|
||||
Red Hat's SpaceWalk
|
||||
Vagrant (not to be confused with the provisioner in vagrant, which is preferred)
|
||||
Zabbix
|
||||
|
||||
Sections on how to use these in more detail will be added over time, but by looking at the "plugins/" directory of the Ansible checkout
|
||||
it should be very obvious how to use them. The process for the AWS inventory script is the same.
|
||||
|
||||
If you develop an interesting inventory script that might be general purpose, please submit a pull request -- we'd likely be glad
|
||||
to include it in the project.
|
||||
|
||||
.. _using_multiple_sources:
|
||||
|
||||
Using Multiple Inventory Sources
|
||||
````````````````````````````````
|
||||
|
||||
If the location given to -i in Ansible is a directory (or as so configured in ansible.cfg), Ansible can use multiple inventory sources
|
||||
at thes same time. When doing so, it is possible to mix both dynamic and statically managed inventory sources in the same ansible run. Instant
|
||||
hybrid cloud!
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`intro_inventory`
|
||||
All about static inventory files
|
||||
`Mailing List <http://groups.google.com/group/ansible-project>`_
|
||||
Questions? Help? Ideas? Stop by the list on Google Groups
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
143
docsite/rst/intro_getting_started.rst
Normal file
143
docsite/rst/intro_getting_started.rst
Normal file
|
@ -0,0 +1,143 @@
|
|||
Getting Started
|
||||
===============
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
.. _gs_about:
|
||||
|
||||
About
|
||||
`````
|
||||
|
||||
Now that you've read :doc:`intro_installation` and installed Ansible, it's time to dig in and get
|
||||
started with some commands.
|
||||
|
||||
What we are showing first are not the powerful configuration/deployment/orchestration of Ansible, called playbooks.
|
||||
Playbooks are covered in a separate section.
|
||||
|
||||
This section is about how to get going initially. Once you have these concepts down, read :doc:`intro_adhoc` for some more
|
||||
detail, and then you'll be ready to dive into playbooks and explore the most interesting parts!
|
||||
|
||||
.. _remote_connection_information:
|
||||
|
||||
Remote Connection Information
|
||||
`````````````````````````````
|
||||
|
||||
Before we get started, it's important to understand how Ansible is communicating with remote
|
||||
machines over SSH.
|
||||
|
||||
By default, Ansible 1.3 and later will try to use native
|
||||
OpenSSH for remote communication when possible. This enables both ControlPersist (a performance feature), Kerberos, and options in ~/.ssh/config such as Jump Host setup. When using Enterprise Linux 6 operating systems as the control machine (Red Hat Enterprise Linux and derivatives such as CentOS), however, the version of OpenSSH may be too old to support Control Persist. On these operating systems, Ansible will fallback into using a high-quality python implementation of
|
||||
OpenSSH called 'paramiko'. If you wish to use features like Kerberized SSH and more, consider using Fedora, OS X, or Ubuntu as your control machine until a newer version of OpenSSH is available for your platform -- or engage 'accelerated mode' in Ansible. See :doc:`playbooks_acceleration`.
|
||||
|
||||
In Ansible 1.2 and before, the default was strictly paramiko and native SSH had to be explicitly selected with -c ssh or set in the configuration file.
|
||||
|
||||
Occasionally you'll encounter a device that doesn't do SFTP. This is rare, but if talking with some remote devices that don't support SFTP, you can switch to SCP mode in :doc:`intro_configuration`.
|
||||
|
||||
When speaking with remote machines, Ansible will by default assume you are using SSH keys -- which we encourage -- but passwords are fine too. To enable password auth, supply the option --ask-pass where needed. If using sudo features and when sudo requires a password, also supply --ask-sudo-pass as appropriate.
|
||||
|
||||
While it may be common sense, it is worth sharing: Any management system benefits from being run near your machines you are being managed. If running in a cloud, consider running Ansible from a machine inside that cloud. It will work better than on the open
|
||||
intranet in most cases.
|
||||
|
||||
As an advanced topic, Ansible doesn't just have to connect remotely over SSH. The transports are pluggable, and there are options for managing things locally, as well as managing chroot, lxc, and jail containers. A mode called 'ansible-pull' can also invert the system and have systems 'phone home' via scheduled git checkouts to pull configuration directives from a central repository.
|
||||
|
||||
.. _your_first_commands:
|
||||
|
||||
Your first commands
|
||||
```````````````````
|
||||
|
||||
Now that you've installed Ansible, it's time to get started with some basics.
|
||||
|
||||
Edit (or create) /etc/ansible/hosts and put one or more remote systems in it, for
|
||||
which you have your SSH key in ``authorized_keys``::
|
||||
|
||||
192.168.1.50
|
||||
aserver.example.org
|
||||
bserver.example.org
|
||||
|
||||
This is an inventory file, which is also explained in greater depth here: :doc:`intro_inventory`.
|
||||
|
||||
We'll assume you are using SSH keys for authentication. To set up SSH agent to avoid retyping passwords, you can
|
||||
do:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ ssh-agent bash
|
||||
$ ssh-add ~/.ssh/id_rsa
|
||||
|
||||
(Depending on your setup, you may wish to ansible's --private-key option to specify a pem file instead)
|
||||
|
||||
Now ping all your nodes:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ ansible all -m ping
|
||||
|
||||
Ansible will attempt to remote connect to the machines using your current
|
||||
user name, just like SSH would. To override the remote user name, just use the '-u' parameter.
|
||||
|
||||
If you would like to access sudo mode, there are also flags to do that:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
# as bruce
|
||||
$ ansible all -m ping -u bruce
|
||||
# as bruce, sudoing to root
|
||||
$ ansible all -m ping -u bruce --sudo
|
||||
# as bruce, sudoing to batman
|
||||
$ ansible all -m ping -u bruce --sudo --sudo-user batman
|
||||
|
||||
(The sudo implementation is changeable in Ansible's configuration file if you happen to want to use a sudo
|
||||
replacement. Flags passed to sudo (like -H) can also be set there.)
|
||||
|
||||
Now run a live command on all of your nodes:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ ansible all -a "/bin/echo hello"
|
||||
|
||||
Congratulations. You've just contacted your nodes with Ansible. It's
|
||||
soon going to be time to read some of the more real-world :doc:`intro_adhoc`, and explore
|
||||
what you can do with different modules, as well as the Ansible
|
||||
:doc:`playbooks` language. Ansible is not just about running commands, it
|
||||
also has powerful configuration management and deployment features. There's more to
|
||||
explore, but you already have a fully working infrastructure!
|
||||
|
||||
.. _a_note_about_host_key_checking:
|
||||
|
||||
Host Key Checking
|
||||
`````````````````
|
||||
|
||||
Ansible 1.2.1 and later have host key checking enabled by default.
|
||||
|
||||
If a host is reinstalled and has a different key in 'known_hosts', this will result in a error message until corrected. If a host is not initially in 'known_hosts' this will result in prompting for confirmation of the key, which results in a interactive experience if using Ansible, from say, cron. You might not want this.
|
||||
|
||||
If you wish to disable this behavior and understand the implications, you can do so by editing /etc/ansible/ansible.cfg or ~/.ansible.cfg::
|
||||
|
||||
[defaults]
|
||||
host_key_checking = False
|
||||
|
||||
Alternatively this can be set by an environment variable:
|
||||
|
||||
$ export ANSIBLE_HOST_KEY_CHECKING=False
|
||||
|
||||
Also note that host key checking in paramiko mode is reasonably slow, therefore switching to 'ssh' is also recommended when using this feature.
|
||||
|
||||
.. _a_note_about_logging:
|
||||
|
||||
Ansible will log some information about module arguments on the remote system in the remote syslog. To enable basic
|
||||
logging on the control machine see `intro_config` document and set the 'log_path' configuration file setting. Enterprise users may also be interested in `AnsibleWorks AWX <http://ansibleworks.com/ansibleworks-awx>`_. AWX provides a very robust database logging feature where it is possible to drill down and see history based on hosts, projects, and particular inventories over time -- explorable both graphically and through a REST API.
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`intro_inventory`
|
||||
More information about inventory
|
||||
:doc:`intro_adhoc`
|
||||
Examples of basic commands
|
||||
:doc:`playbooks`
|
||||
Learning Ansible's configuration management language
|
||||
`Mailing List <http://groups.google.com/group/ansible-project>`_
|
||||
Questions? Help? Ideas? Stop by the list on Google Groups
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
225
docsite/rst/intro_installation.rst
Normal file
225
docsite/rst/intro_installation.rst
Normal file
|
@ -0,0 +1,225 @@
|
|||
Installation
|
||||
============
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
.. _getting_ansible:
|
||||
|
||||
Getting Ansible
|
||||
```````````````
|
||||
|
||||
You may also wish to follow the `Github project <https://github.com/ansible/ansible>`_ if
|
||||
you have a github account. This is also where we keep the issue tracker for sharing
|
||||
bugs and feature ideas.
|
||||
|
||||
.. _what_will_be_installed:
|
||||
|
||||
Basics / What Will Be Installed
|
||||
```````````````````````````````
|
||||
|
||||
Ansible by default manages machines over the SSH protocol.
|
||||
|
||||
Once ansible is installed, it will not add a database, and there will be no daemons to start or keep running. You only need to install it on one machine (which could easily be a laptop) and it can manage an entire fleet of remote machines from that central point. When Ansible manages remote machines, it does not leave software installed or running on them, so there's no real question about how to upgrade Ansible when moving to a new version.
|
||||
|
||||
.. _what_version:
|
||||
|
||||
What Version To Pick?
|
||||
`````````````````````
|
||||
|
||||
Because it runs so easily from source and does not require any installation of software on remote
|
||||
machines, many users will actually track the development version.
|
||||
|
||||
Ansible's release cycles are usually about two months long. Due to this
|
||||
short release cycle, minor bugs will generally be fixed in the next release versus maintaining
|
||||
backports on the stable branch. Major bugs will still have maintenance releases when needed, though
|
||||
these are infrequent.
|
||||
|
||||
If you are wishing to run the latest released version of Ansible and you are running Red Hat Enterprise Linux (TM), CentOS, Fedora, Debian, or Ubuntu, we recommend using the OS package manager.
|
||||
|
||||
For other installation options, we recommend installing via "pip", which is the Python package manager, though other options are also available.
|
||||
|
||||
If you wish to track the development release to use and test the latest features, we will share
|
||||
information about running from source. It's not necessary to install the program to run from source.
|
||||
|
||||
.. _control_machine_requirements:
|
||||
|
||||
Control Machine Requirements
|
||||
````````````````````````````
|
||||
|
||||
Currently Ansible can be from any machine with Python 2.6 installed (Windows isn't supported for the control machine).
|
||||
|
||||
This includes Red Hat, Debian, CentOS, OS X, any of the BSDs, and so on.
|
||||
|
||||
.. _managed_node_requirements:
|
||||
|
||||
Managed Node Requirements
|
||||
`````````````````````````
|
||||
|
||||
On the managed nodes, you only need Python 2.4 or later, but if you are are running less than Python 2.5 on the remotes, you will also need:
|
||||
|
||||
* ``python-simplejson``
|
||||
|
||||
.. note::
|
||||
|
||||
Ansible's "raw" module (for executing commands in a quick and dirty
|
||||
way) and the script module don't even need that. So technically, you can use
|
||||
Ansible to install python-simplejson using the raw module, which
|
||||
then allows you to use everything else. (That's jumping ahead
|
||||
though.)
|
||||
|
||||
.. note::
|
||||
|
||||
If you have SELinux enabled on remote nodes, you will also want to install
|
||||
libselinux-python on them before using any copy/file/template related functions in
|
||||
Ansible. You can of course still use the yum module in Ansible to install this package on
|
||||
remote systems that do not have it.
|
||||
|
||||
.. note::
|
||||
|
||||
Python 3 is a slightly different language than Python 2 and most python programs (including
|
||||
Ansible) are not switching over yet. However, some Linux distributions (Gentoo, Arch) may not have a
|
||||
Python 2.X interpreter installed by default. On those systems, you should install one, and set
|
||||
the 'ansible_python_interpreter' variable in inventory (see :doc:`intro_inventory`) to point at your 2.X python. Distributions
|
||||
like Red Hat Enterprise Linux, CentOS, Fedora, and Ubuntu all have a 2.X interpreter installed
|
||||
by default and this does not apply to those distributions. This is also true of nearly all
|
||||
Unix systems. If you need to bootstrap these remote systems by installing Python 2.X,
|
||||
using the 'raw' module will be able to do it remotely.
|
||||
|
||||
.. _from_source:
|
||||
|
||||
Running From Source
|
||||
+++++++++++++++++++
|
||||
|
||||
Ansible is trivially easy to run from a checkout, root permissions are not required
|
||||
to use it and there is no software to actually install for Ansible itself. No daemons
|
||||
or database setup are required. Because of this, many users in our community use the
|
||||
development version of Ansible all of the time, so they can take advantage of new features
|
||||
when they are implemented, and also easily contribute to the project. Because there is
|
||||
nothing to install, following the development version is significantly easier than most
|
||||
open source projects.
|
||||
|
||||
To install from source.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ git clone git://github.com/ansible/ansible.git
|
||||
$ cd ./ansible
|
||||
$ source ./hacking/env-setup
|
||||
|
||||
If you don't have pip installed in your version of Python, install pip::
|
||||
|
||||
$ sudo easy_install pip
|
||||
|
||||
Ansible also uses the the following Python modules that need to be installed::
|
||||
|
||||
$ sudo pip install paramiko PyYAML jinja2
|
||||
|
||||
Once running the env-setup script you'll be running from checkout and the default inventory file
|
||||
will be /etc/ansible/hosts. You can optionally specify an inventory file (see :doc:`intro_inventory`)
|
||||
other than /etc/ansible/hosts:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ echo "127.0.0.1" > ~/ansible_hosts
|
||||
$ export ANSIBLE_HOSTS=~/ansible_hosts
|
||||
|
||||
You can read more about the inventory file in later parts of the manual.
|
||||
|
||||
Now let's test things with a ping command:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ ansible all -m ping --ask-pass
|
||||
|
||||
You can also use "sudo make install" if you wish.
|
||||
|
||||
.. _from_yum:
|
||||
|
||||
Latest Release Via Yum
|
||||
++++++++++++++++++++++
|
||||
|
||||
RPMs are available from yum for `EPEL
|
||||
<http://fedoraproject.org/wiki/EPEL>`_ 6 and currently supported
|
||||
Fedora distributions.
|
||||
|
||||
Ansible itself can manage earlier operating
|
||||
systems that contain python 2.4 or higher (so also EL5).
|
||||
|
||||
Fedora users can install Ansible directly, though if you are using RHEL or CentOS and have not already done so, `configure EPEL <http://fedoraproject.org/wiki/EPEL>`_
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
# install the epel-release RPM if needed on CentOS, RHEL, or Scientific Linux
|
||||
$ sudo yum install ansible
|
||||
|
||||
You can also build an RPM yourself. From the root of a checkout or tarball, use the ``make rpm`` command to build an RPM you can distribute and install. Make sure you have ``rpm-build``, ``make``, and ``python2-devel`` installed.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ git clone git://github.com/ansible/ansible.git
|
||||
$ cd ./ansible
|
||||
$ make rpm
|
||||
$ sudo rpm -Uvh ~/rpmbuild/ansible-*.noarch.rpm
|
||||
|
||||
.. _from_apt:
|
||||
|
||||
Latest Releases Via Apt (Ubuntu)
|
||||
++++++++++++++++++++++++++++++++
|
||||
|
||||
Ubuntu builds are available `in a PPA here <https://launchpad.net/~rquillo/+archive/ansible>`_.
|
||||
|
||||
Once configured,
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ sudo add-apt-repository ppa:rquillo/ansible
|
||||
$ sudo apt-get update
|
||||
$ sudo apt-get install ansible
|
||||
|
||||
Debian/Ubuntu packages can also be built from the source checkout, run:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ make debian
|
||||
|
||||
You may also wish to run from source to get the latest, which is covered above.
|
||||
|
||||
.. _from_pip:
|
||||
|
||||
Latest Releases Via Pip
|
||||
+++++++++++++++++++++++
|
||||
|
||||
Ansible can be installed via "pip", the Python package manager. If 'pip' isn't already available in
|
||||
your version of Python, you can get pip by::
|
||||
|
||||
$ sudo easy_install pip
|
||||
|
||||
Then install Ansible with::
|
||||
|
||||
$ sudo pip install ansible
|
||||
|
||||
Readers that use virtualenv can also install Ansible under virtualenv, though we'd recommend to not worry about it and just install Ansible globally. Do not use easy_install to install ansible directly.
|
||||
|
||||
.. _tagged_releases:
|
||||
|
||||
Tarballs of Tagged Releases
|
||||
+++++++++++++++++++++++++++
|
||||
|
||||
Packaging Ansible or wanting to build a local package yourself, but don't want to do a git checkout? Tarballs of releases are available on the ansibleworks.com page.
|
||||
|
||||
* `Ansible/downloads <http://ansibleworks.com/releases>`_
|
||||
|
||||
These releases are also tagged in the git repository with the release version.
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`intro_adhoc`
|
||||
Examples of basic commands
|
||||
:doc:`playbooks`
|
||||
Learning ansible's configuration management language
|
||||
`Mailing List <http://groups.google.com/group/ansible-project>`_
|
||||
Questions? Help? Ideas? Stop by the list on Google Groups
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
227
docsite/rst/intro_inventory.rst
Normal file
227
docsite/rst/intro_inventory.rst
Normal file
|
@ -0,0 +1,227 @@
|
|||
.. _inventory:
|
||||
|
||||
Inventory
|
||||
=========
|
||||
|
||||
Ansible works against multiple systems in your infrastructure at the
|
||||
same time. It does this by selecting portions of systems listed in
|
||||
Ansible's inventory file, which defaults to being saved in
|
||||
the location /etc/ansible/hosts.
|
||||
|
||||
Not only is this inventory configurable, but you can also use
|
||||
multiple inventory files at the same time (explained below) and also
|
||||
pull inventory from dynamic or cloud sources, as described in :doc:`intro_dynamic_inventory`.
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
.. _inventoryformat:
|
||||
|
||||
Hosts and Groups
|
||||
++++++++++++++++
|
||||
|
||||
The format for /etc/ansible/hosts is an INI format and looks like this::
|
||||
|
||||
mail.example.com
|
||||
|
||||
[webservers]
|
||||
foo.example.com
|
||||
bar.example.com
|
||||
|
||||
[dbservers]
|
||||
one.example.com
|
||||
two.example.com
|
||||
three.example.com
|
||||
|
||||
The things in brackets are group names, which are used in classifying systems
|
||||
and deciding what systems you are controlling at what times and for what purpose.
|
||||
|
||||
If you have hosts that run on non-standard SSH ports you can put the port number
|
||||
after the hostname with a colon. Ports listed in your SSH config file won't be used,
|
||||
so it is important that you set them if things are not running on the default port::
|
||||
|
||||
badwolf.example.com:5309
|
||||
|
||||
Suppose you have just static IPs and want to set up some aliases that don't live in your host file, or you are connecting through tunnels. You can do things like this::
|
||||
|
||||
jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50
|
||||
|
||||
In the above example, trying to ansible against the host alias "jumper" (which may not even be a real hostname) will contact 192.168.1.50 on port 5555. Note that this is using a feature of the inventory file to define some special variables. Generally speaking this is not the best
|
||||
way to define variables that describe your system policy, but we'll share suggestions on doing this later. We're just getting started.
|
||||
|
||||
Adding a lot of hosts? If you have a lot of hosts following similar patterns you can do this rather than listing each hostname::
|
||||
|
||||
|
||||
[webservers]
|
||||
www[01:50].example.com
|
||||
|
||||
For numeric patterns, leading zeros can be included or removed, as desired. Ranges are inclusive. You can also define alphabetic ranges::
|
||||
|
||||
[databases]
|
||||
db-[a:f].example.com
|
||||
|
||||
You can also select the connection type and user on a per host basis::
|
||||
|
||||
[targets]
|
||||
|
||||
localhost ansible_connection=local
|
||||
other1.example.com ansible_connection=ssh ansible_ssh_user=mpdehaan
|
||||
other2.example.com ansible_connection=ssh ansible_ssh_user=mdehaan
|
||||
|
||||
As mentioned above, setting these in the inventory file is only a shorthand, and we'll discuss how to store them in individual files
|
||||
in the 'host_vars' directory a bit later on.
|
||||
|
||||
.. _host_variables:
|
||||
|
||||
Host Variables
|
||||
++++++++++++++
|
||||
|
||||
As alluded to above, it is easy to assign variables to hosts that will be used later in playbooks::
|
||||
|
||||
[atlanta]
|
||||
host1 http_port=80 maxRequestsPerChild=808
|
||||
host2 http_port=303 maxRequestsPerChild=909
|
||||
|
||||
.. _group_variables:
|
||||
|
||||
Group Variables
|
||||
+++++++++++++++
|
||||
|
||||
Variables can also be applied to an entire group at once::
|
||||
|
||||
[atlanta]
|
||||
host1
|
||||
host2
|
||||
|
||||
[atlanta:vars]
|
||||
ntp_server=ntp.atlanta.example.com
|
||||
proxy=proxy.atlanta.example.com
|
||||
|
||||
.. _subgroups:
|
||||
|
||||
Groups of Groups, and Group Variables
|
||||
+++++++++++++++++++++++++++++++++++++
|
||||
|
||||
It is also possible to make groups of groups and assign
|
||||
variables to groups. These variables can be used by /usr/bin/ansible-playbook, but not
|
||||
/usr/bin/ansible::
|
||||
|
||||
[atlanta]
|
||||
host1
|
||||
host2
|
||||
|
||||
[raleigh]
|
||||
host2
|
||||
host3
|
||||
|
||||
[southeast:children]
|
||||
atlanta
|
||||
raleigh
|
||||
|
||||
[southeast:vars]
|
||||
some_server=foo.southeast.example.com
|
||||
halon_system_timeout=30
|
||||
self_destruct_countdown=60
|
||||
escape_pods=2
|
||||
|
||||
[usa:children]
|
||||
southeast
|
||||
northeast
|
||||
southwest
|
||||
southeast
|
||||
|
||||
If you need to store lists or hash data, or prefer to keep host and group specific variables
|
||||
separate from the inventory file, see the next section.
|
||||
|
||||
.. _splitting_out_vars:
|
||||
|
||||
Splitting Out Host and Group Specific Data
|
||||
++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
The preferred practice in Ansible is actually not to store variables in the main inventory file.
|
||||
|
||||
In addition to the storing variables directly in the INI file, host
|
||||
and group variables can be stored in individual files relative to the
|
||||
inventory file.
|
||||
|
||||
These variable files are in YAML format. See :doc:`YAMLSyntax` if you are new to YAML.
|
||||
|
||||
Assuming the inventory file path is::
|
||||
|
||||
/etc/ansible/hosts
|
||||
|
||||
If the host is named 'foosball', and in groups 'raleigh' and 'webservers', variables
|
||||
in YAML files at the following locations will be made available to the host::
|
||||
|
||||
/etc/ansible/group_vars/raleigh
|
||||
/etc/ansible/group_vars/webservers
|
||||
/etc/ansible/host_vars/foosball
|
||||
|
||||
For instance, suppose you have hosts grouped by datacenter, and each datacenter
|
||||
uses some different servers. The data in the groupfile '/etc/ansible/group_vars/raleigh' for
|
||||
the 'raleigh' group might look like::
|
||||
|
||||
---
|
||||
ntp_server: acme.example.org
|
||||
database_server: storage.example.org
|
||||
|
||||
It is ok if these files do not exist, as this is an optional feature.
|
||||
|
||||
Tip: In Ansible 1.2 or later the group_vars/ and host_vars/ directories can exist in either
|
||||
the playbook directory OR the inventory directory. If both paths exist, variables in the playbook
|
||||
directory will be loaded second.
|
||||
|
||||
Tip: Keeping your inventory file and variables in a git repo (or other version control)
|
||||
is an excellent way to track changes to your inventory and host variables.
|
||||
|
||||
.. _behavioral_parameters:
|
||||
|
||||
List of Behavioral Inventory Parameters
|
||||
+++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
As alluded to above, setting the following variables controls how ansible interacts with remote hosts. Some we have already
|
||||
mentioned::
|
||||
|
||||
ansible_ssh_host
|
||||
The name of the host to connect to, if different from the alias you wish to give to it.
|
||||
ansible_ssh_port
|
||||
The ssh port number, if not 22
|
||||
ansible_ssh_user
|
||||
The default ssh user name to use.
|
||||
ansible_ssh_pass
|
||||
The ssh password to use (this is insecure, we strongly recommend using --ask-pass or SSH keys)
|
||||
ansible_connection
|
||||
Connection type of the host. Candidates are local, ssh or paramiko. The default is paramiko before Ansible 1.2, and 'smart' afterwards which detects whether usage of 'ssh' would be feasible based on whether ControlPersist is supported.
|
||||
ansible_ssh_private_key_file
|
||||
Private key file used by ssh. Useful if using multiple keys and you don't want to use SSH agent.
|
||||
ansible_python_interpreter
|
||||
The target host python path. This is useful for systems with more
|
||||
than one Python or not located at "/usr/bin/python" such as \*BSD, or where /usr/bin/python
|
||||
is not a 2.X series Python. We do not use the "/usr/bin/env" mechanism as that requires the remote user's
|
||||
path to be set right and also assumes the "python" executable is named python, where the executable might
|
||||
be named something like "python26".
|
||||
ansible\_\*\_interpreter
|
||||
Works for anything such as ruby or perl and works just like ansible_python_interpreter.
|
||||
This replaces shebang of modules which will run on that host.
|
||||
|
||||
Examples from a host file::
|
||||
|
||||
some_host ansible_ssh_port=2222 ansible_ssh_user=manager
|
||||
aws_host ansible_ssh_private_key_file=/home/example/.ssh/aws.pem
|
||||
freebsd_host ansible_python_interpreter=/usr/local/bin/python
|
||||
ruby_module_host ansible_ruby_interpreter=/usr/bin/ruby.1.9.3
|
||||
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`intro_dynamic_inventory`
|
||||
Pulling inventory from dynamic sources, such as cloud providers
|
||||
:doc:`intro_adhoc`
|
||||
Examples of basic commands
|
||||
:doc:`playbooks`
|
||||
Learning ansible's configuration management language
|
||||
`Mailing List <http://groups.google.com/group/ansible-project>`_
|
||||
Questions? Help? Ideas? Stop by the list on Google Groups
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
92
docsite/rst/intro_patterns.rst
Normal file
92
docsite/rst/intro_patterns.rst
Normal file
|
@ -0,0 +1,92 @@
|
|||
Patterns
|
||||
++++++++
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
Patterns in Ansible are how we decide which hosts to manage. This can mean what hosts to communicate with, but in terms
|
||||
of :doc:`playbooks` it actually means what hosts to apply a particular configuration or IT process to.
|
||||
|
||||
We'll go over how to use the command line in :doc:`intro_adhoc` section, however, basically it looks like this::
|
||||
|
||||
ansible <pattern_goes_here> -m <module_name> -a <arguments>
|
||||
|
||||
Such as::
|
||||
|
||||
ansible webservers -m service -a "name=httpd state=restarted"
|
||||
|
||||
A pattern usually refers to a set of groups (which are sets of hosts) -- in the above case, machines in the "webservers" group.
|
||||
|
||||
Anyway, to use Ansible, you'll first need to know how to tell Ansible which hosts in your inventory to talk to.
|
||||
This is done by designating particular host names or groups of hosts.
|
||||
|
||||
The following patterns are equivalent and target all hosts in the inventory::
|
||||
|
||||
all
|
||||
*
|
||||
|
||||
It is also possible to address a specific host or set of hosts by name::
|
||||
|
||||
one.example.com
|
||||
one.example.com:two.example.com
|
||||
192.168.1.50
|
||||
192.168.1.*
|
||||
|
||||
The following patterns address one or more groups. Groups separated by a colon indicate an "OR" configuration.
|
||||
This means the host may be in either one group or the other::
|
||||
|
||||
webservers
|
||||
webservers:dbservers
|
||||
|
||||
You can exclude groups as well, for instance, all machines must be in the group webservers but not in the group phoenix::
|
||||
|
||||
webservers:!phoenix
|
||||
|
||||
You can also specify the intersection of two groups. This would mean the hosts must be in the group webservers and
|
||||
the host must also be in the group staging::
|
||||
|
||||
webservers:&staging
|
||||
|
||||
You can do combinations::
|
||||
|
||||
webservers:dbservers:&staging:!phoenix
|
||||
|
||||
The above configuration means "all machines in the groups 'webservers' and 'dbservers' are to be managed if they are in
|
||||
the group 'staging' also, but the machines are not to be managed if they are in the group 'phoenix' ... whew!
|
||||
|
||||
You can also use variables if you want to pass some group specifiers via the "-e" argument to ansible-playbook, but this
|
||||
is uncommonly used::
|
||||
|
||||
webservers:!{{excluded}}:&{{required}}
|
||||
|
||||
You also don't have to manage by strictly defined groups. Individual host names, IPs and groups, can also be referenced using
|
||||
wildcards::
|
||||
|
||||
*.example.com
|
||||
*.com
|
||||
|
||||
It's also ok to mix wildcard patterns and groups at the same time::
|
||||
|
||||
one*.com:dbservers
|
||||
|
||||
Most people don't specify patterns as regular expressions, but you can. Just start the pattern with a '~'::
|
||||
|
||||
~(web|db).*\.example\.com
|
||||
|
||||
While we're jumping a bit ahead, additionally, you can add an exclusion criteria just by supplying the "--limit" flag to /usr/bin/ansible or /usr/bin/ansible-playbook::
|
||||
|
||||
ansible-playbook site.yml --limit datacenter2
|
||||
|
||||
Easy enough. See :doc:`intro_adhoc` and then :doc:`playbooks` for how to apply this knowledge.
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`intro_adhoc`
|
||||
Examples of basic commands
|
||||
:doc:`playbooks`
|
||||
Learning ansible's configuration management language
|
||||
`Mailing List <http://groups.google.com/group/ansible-project>`_
|
||||
Questions? Help? Ideas? Stop by the list on Google Groups
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
100
docsite/rst/modules.rst
Normal file
100
docsite/rst/modules.rst
Normal file
|
@ -0,0 +1,100 @@
|
|||
Ansible Modules
|
||||
===============
|
||||
|
||||
.. contents::
|
||||
:depth: 3
|
||||
|
||||
.. _modules_intro:
|
||||
|
||||
Introduction
|
||||
````````````
|
||||
|
||||
|
||||
Ansible ships with a number of modules (called the 'module library')
|
||||
that can be executed directly on remote hosts or through :doc:`playbooks`.
|
||||
Users can also write their own modules. These modules can control system
|
||||
resources, like services, packages, or files (anything really), or
|
||||
handle executing system commands.
|
||||
|
||||
Let's review how we execute three different modules from the command line::
|
||||
|
||||
ansible webservers -m service -a "name=httpd state=running"
|
||||
ansible webservers -m ping
|
||||
ansible webservers -m command -a "/sbin/reboot -t now"
|
||||
|
||||
Each module supports taking arguments. Nearly all modules take ``key=value``
|
||||
arguments, space delimited. Some modules take no arguments, and the
|
||||
command/shell modules simply take the string of the command you want to run.
|
||||
|
||||
From playbooks, Ansible modules are executed in a very similar way::
|
||||
|
||||
- name: reboot the servers
|
||||
action: command /sbin/reboot -t now
|
||||
|
||||
Version 0.8 and higher support the following shorter syntax::
|
||||
|
||||
- name: reboot the servers
|
||||
command: /sbin/reboot -t now
|
||||
|
||||
All modules technically return JSON format data, though if you are using the
|
||||
command line or playbooks, you don't really need to know much about
|
||||
that. If you're writing your own module, you care, and this means you do
|
||||
not have to write modules in any particular language -- you get to choose.
|
||||
|
||||
Modules are `idempotent`, meaning they will seek to avoid changes to the system unless a change needs to be made. When using Ansible
|
||||
playbooks, these modules can trigger 'change events' in the form of notifying 'handlers'
|
||||
to run additional tasks.
|
||||
|
||||
Documention for each module can be accessed from the command line with the
|
||||
ansible-doc as well as the man command::
|
||||
|
||||
ansible-doc command
|
||||
|
||||
man ansible.template
|
||||
|
||||
Let's see what's available in the Ansible module library, out of the box:
|
||||
|
||||
|
||||
.. include:: modules/_list.rst
|
||||
|
||||
.. _ansible_doc:
|
||||
|
||||
Reading Module Documentation Locally
|
||||
````````````````````````````````````
|
||||
|
||||
ansible-doc is a friendly command line tool that allows you to access module documentation locally.
|
||||
It comes with Ansible.
|
||||
|
||||
To list documentation for a particular module::
|
||||
|
||||
ansible-doc yum | less
|
||||
|
||||
To list all modules available::
|
||||
|
||||
ansible-doc --list | less
|
||||
|
||||
To access modules outside of the stock module path (such as custom modules that live in your playbook directory),
|
||||
use the '--module-path' option to specify the directory where the module lives.
|
||||
|
||||
.. _writing_modules:
|
||||
|
||||
Writing your own modules
|
||||
````````````````````````
|
||||
|
||||
See :doc:`developing_modules`.
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`intro_adhoc`
|
||||
Examples of using modules in /usr/bin/ansible
|
||||
:doc:`playbooks`
|
||||
Examples of using modules with /usr/bin/ansible-playbook
|
||||
:doc:`developing_modules`
|
||||
How to write your own modules
|
||||
:doc:`developing_api`
|
||||
Examples of using modules with the Python API
|
||||
`Mailing List <http://groups.google.com/group/ansible-project>`_
|
||||
Questions? Help? Ideas? Stop by the list on Google Groups
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
0
docsite/rst/modules/.gitdir
Normal file
0
docsite/rst/modules/.gitdir
Normal file
381
docsite/rst/playbooks.rst
Normal file
381
docsite/rst/playbooks.rst
Normal file
|
@ -0,0 +1,381 @@
|
|||
Intro to Playbooks
|
||||
==================
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
.. _about_playbooks:
|
||||
|
||||
About Playbooks
|
||||
```````````````
|
||||
|
||||
Playbooks are a completely different way to use ansible than in adhoc task execution mode, and are
|
||||
particularly powerful.
|
||||
|
||||
Simply put, playbooks are the basis for a really simple configuration management and multi-machine deployment system,
|
||||
unlike any that already exist, and one that is very well suited to deploying complex applications.
|
||||
|
||||
Playbooks can declare configurations, but they can also orchestrate steps of
|
||||
any manual ordered process, even as different steps must bounce back and forth
|
||||
between sets of machines in particular orders. They can launch tasks
|
||||
synchronously or asynchronously.
|
||||
|
||||
While you might run the main /usr/bin/ansible program for ad-hoc
|
||||
tasks, playbooks are more likely to be kept in source control and used
|
||||
to push out your configuration or assure the configurations of your
|
||||
remote systems are in spec.
|
||||
|
||||
There are also some full sets of playbooks illustrating a lot of these techniques in the
|
||||
`ansible-examples repository <https://github.com/ansible/ansible-examples>`_. We'd recommend
|
||||
looking at these in another tab as you go along.
|
||||
|
||||
There are also many jumping off points after you learn playbooks, so hop back to the documentation
|
||||
index after you're done with this section.
|
||||
|
||||
.. _playbook_language_example:
|
||||
|
||||
Playbook Language Example
|
||||
`````````````````````````
|
||||
|
||||
Playbooks are expressed in YAML format (see :doc:`YAMLSyntax`) and have a minimum of syntax, which intentionally
|
||||
tries to not be a programming language or script, but rather a model of a configuration or a process.
|
||||
|
||||
Each playbook is composed of one or more 'plays' in a list.
|
||||
|
||||
The goal of a play is to map a group of hosts to some well defined roles, represented by
|
||||
things ansible calls tasks. At a basic level, a task is nothing more than a call
|
||||
to an ansible module, which you should have learned about in earlier chapters.
|
||||
|
||||
By composing a playbook of multiple 'plays', it is possible to
|
||||
orchestrate multi-machine deployments, running certain steps on all
|
||||
machines in the webservers group, then certain steps on the database
|
||||
server group, then more commands back on the webservers group, etc.
|
||||
|
||||
"plays" are more or less a sports analogy. You can have quite a lot of plays that affect your systems
|
||||
to do different things. It's not as if you were just defining one particular state or model, and you
|
||||
can run different plays at different times.
|
||||
|
||||
For starters, here's a playbook that contains just one play::
|
||||
|
||||
---
|
||||
- hosts: webservers
|
||||
vars:
|
||||
http_port: 80
|
||||
max_clients: 200
|
||||
remote_user: root
|
||||
tasks:
|
||||
- name: ensure apache is at the latest version
|
||||
yum: pkg=httpd state=latest
|
||||
- name: write the apache config file
|
||||
template: src=/srv/httpd.j2 dest=/etc/httpd.conf
|
||||
notify:
|
||||
- restart apache
|
||||
- name: ensure apache is running
|
||||
service: name=httpd state=started
|
||||
handlers:
|
||||
- name: restart apache
|
||||
service: name=httpd state=restarted
|
||||
|
||||
Below, we'll break down what the various features of the playbook language are.
|
||||
|
||||
.. _playbook_basics:
|
||||
|
||||
Basics
|
||||
``````
|
||||
|
||||
.. _playbook_hosts_and_users:
|
||||
|
||||
Hosts and Users
|
||||
+++++++++++++++
|
||||
|
||||
For each play in a playbook, you get to choose which machines in your infrastructure
|
||||
to target and what remote user to complete the steps (called tasks) as.
|
||||
|
||||
The `hosts` line is a list of one or more groups or host patterns,
|
||||
separated by colons, as described in the :doc:`intro_patterns`
|
||||
documentation. The `remote_user` is just the name of the user account::
|
||||
|
||||
---
|
||||
- hosts: webservers
|
||||
remote_user: root
|
||||
|
||||
.. Note::
|
||||
|
||||
The `remote_user` parameter was formerly called just `user`. It was renamed in Ansible 1.4 to make it more distinguishable from the `user` module (used to create users on remote systems).
|
||||
|
||||
Support for running things from sudo is also available::
|
||||
|
||||
---
|
||||
- hosts: webservers
|
||||
remote_user: yourname
|
||||
sudo: yes
|
||||
|
||||
You can also use sudo on a particular task instead of the whole play::
|
||||
|
||||
---
|
||||
- hosts: webservers
|
||||
remote_user: yourname
|
||||
tasks:
|
||||
- service: name=nginx state=started
|
||||
sudo: yes
|
||||
|
||||
|
||||
You can also login as you, and then sudo to different users than root::
|
||||
|
||||
---
|
||||
- hosts: webservers
|
||||
remote_user: yourname
|
||||
sudo: yes
|
||||
sudo_user: postgres
|
||||
|
||||
If you need to specify a password to sudo, run `ansible-playbook` with ``--ask-sudo-pass`` (`-K`).
|
||||
If you run a sudo playbook and the playbook seems to hang, it's probably stuck at the sudo prompt.
|
||||
Just `Control-C` to kill it and run it again with `-K`.
|
||||
|
||||
.. important::
|
||||
|
||||
When using `sudo_user` to a user other than root, the module
|
||||
arguments are briefly written into a random tempfile in /tmp.
|
||||
These are deleted immediately after the command is executed. This
|
||||
only occurs when sudoing from a user like 'bob' to 'timmy', not
|
||||
when going from 'bob' to 'root', or logging in directly as 'bob' or
|
||||
'root'. If this concerns you that this data is briefly readable
|
||||
(not writeable), avoid transferring uncrypted passwords with
|
||||
`sudo_user` set. In other cases, '/tmp' is not used and this does
|
||||
not come into play. Ansible also takes care to not log password
|
||||
parameters.
|
||||
|
||||
.. _tasks_list:
|
||||
|
||||
Tasks list
|
||||
++++++++++
|
||||
|
||||
Each play contains a list of tasks. Tasks are executed in order, one
|
||||
at a time, against all machines matched by the host pattern,
|
||||
before moving on to the next task. It is important to understand that, within a play,
|
||||
all hosts are going to get the same task directives. It is the purpose of a play to map
|
||||
a selection of hosts to tasks.
|
||||
|
||||
When running the playbook, which runs top to bottom, hosts with failed tasks are
|
||||
taken out of the rotation for the entire playbook. If things fail, simply correct the playbook file and rerun.
|
||||
|
||||
The goal of each task is to execute a module, with very specific arguments.
|
||||
Variables, as mentioned above, can be used in arguments to modules.
|
||||
|
||||
Modules are 'idempotent', meaning if you run them
|
||||
again, they will make only the changes they must in order to bring the
|
||||
system to the desired state. This makes it very safe to rerun
|
||||
the same playbook multiple times. They won't change things
|
||||
unless they have to change things.
|
||||
|
||||
The `command` and `shell` modules will typically rerun the same command again,
|
||||
which is totally ok if the command is something like
|
||||
'chmod' or 'setsebool', etc. Though there is a 'creates' flag available which can
|
||||
be used to make these modules also idempotent.
|
||||
|
||||
Every task should have a `name`, which is included in the output from
|
||||
running the playbook. This is output for humans, so it is
|
||||
nice to have reasonably good descriptions of each task step. If the name
|
||||
is not provided though, the string fed to 'action' will be used for
|
||||
output.
|
||||
|
||||
Tasks can be declared using the legacy "action: module options" format, but
|
||||
it is recommended that you use the more conventional "module: options" format.
|
||||
This recommended format is used throughout the documentation, but you may
|
||||
encounter the older format in some playbooks.
|
||||
|
||||
Here is what a basic task looks like, as with most modules,
|
||||
the service module takes key=value arguments::
|
||||
|
||||
tasks:
|
||||
- name: make sure apache is running
|
||||
service: name=httpd state=running
|
||||
|
||||
The `command` and `shell` modules are the one modules that just takes a list
|
||||
of arguments, and don't use the key=value form. This makes
|
||||
them work just like you would expect. Simple::
|
||||
|
||||
tasks:
|
||||
- name: disable selinux
|
||||
command: /sbin/setenforce 0
|
||||
|
||||
The command and shell module care about return codes, so if you have a command
|
||||
whose successful exit code is not zero, you may wish to do this::
|
||||
|
||||
tasks:
|
||||
- name: run this command and ignore the result
|
||||
shell: /usr/bin/somecommand || /bin/true
|
||||
|
||||
Or this::
|
||||
|
||||
tasks:
|
||||
- name: run this command and ignore the result
|
||||
shell: /usr/bin/somecommand
|
||||
ignore_errors: True
|
||||
|
||||
|
||||
If the action line is getting too long for comfort you can break it on
|
||||
a space and indent any continuation lines::
|
||||
|
||||
tasks:
|
||||
- name: Copy ansible inventory file to client
|
||||
copy: src=/etc/ansible/hosts dest=/etc/ansible/hosts
|
||||
owner=root group=root mode=0644
|
||||
|
||||
Variables can be used in action lines. Suppose you defined
|
||||
a variable called 'vhost' in the 'vars' section, you could do this::
|
||||
|
||||
tasks:
|
||||
- name: create a virtual host file for {{ vhost }}
|
||||
template: src=somefile.j2 dest=/etc/httpd/conf.d/{{ vhost }}
|
||||
|
||||
Those same variables are usable in templates, which we'll get to later.
|
||||
|
||||
Now in a very basic playbook all the tasks will be listed directly in that play, though it will usually
|
||||
make more sense to break up tasks using the 'include:' directive. We'll show that a bit later.
|
||||
|
||||
.. _action_shorthand:
|
||||
|
||||
Action Shorthand
|
||||
````````````````
|
||||
|
||||
.. versionadded:: 0.8
|
||||
|
||||
Ansible prefers listing modules like this in 0.8 and later::
|
||||
|
||||
template: src=templates/foo.j2 dest=/etc/foo.conf
|
||||
|
||||
You will notice in earlier versions, this was only available as::
|
||||
|
||||
action: template src=templates/foo.j2 dest=/etc/foo.conf
|
||||
|
||||
The old form continues to work in newer versions without any plan of deprecation.
|
||||
|
||||
.. _handlers:
|
||||
|
||||
Handlers: Running Operations On Change
|
||||
``````````````````````````````````````
|
||||
|
||||
As we've mentioned, modules are written to be 'idempotent' and can relay when
|
||||
they have made a change on the remote system. Playbooks recognize this and
|
||||
have a basic event system that can be used to respond to change.
|
||||
|
||||
These 'notify' actions are triggered at the end of each block of tasks in a playbook, and will only be
|
||||
triggered once even if notified by multiple different tasks.
|
||||
|
||||
For instance, multiple resources may indicate
|
||||
that apache needs to be restarted because they have changed a config file,
|
||||
but apache will only be bounced once to avoid unnecessary restarts.
|
||||
|
||||
Here's an example of restarting two services when the contents of a file
|
||||
change, but only if the file changes::
|
||||
|
||||
- name: template configuration file
|
||||
template: src=template.j2 dest=/etc/foo.conf
|
||||
notify:
|
||||
- restart memcached
|
||||
- restart apache
|
||||
|
||||
The things listed in the 'notify' section of a task are called
|
||||
handlers.
|
||||
|
||||
Handlers are lists of tasks, not really any different from regular
|
||||
tasks, that are referenced by name. Handlers are what notifiers
|
||||
notify. If nothing notifies a handler, it will not run. Regardless
|
||||
of how many things notify a handler, it will run only once, after all
|
||||
of the tasks complete in a particular play.
|
||||
|
||||
Here's an example handlers section::
|
||||
|
||||
handlers:
|
||||
- name: restart memcached
|
||||
service: name=memcached state=restarted
|
||||
- name: restart apache
|
||||
service: name=apache state=restarted
|
||||
|
||||
Handlers are best used to restart services and trigger reboots. You probably
|
||||
won't need them for much else.
|
||||
|
||||
.. note::
|
||||
Notify handlers are always run in the order written.
|
||||
|
||||
Roles are described later on. It's worthwhile to point out that handlers are
|
||||
automatically processed between 'pre_tasks', 'roles', 'tasks', and 'post_tasks'
|
||||
sections. If you ever want to flush all the handler commands immediately though,
|
||||
in 1.2 and later, you can::
|
||||
|
||||
tasks:
|
||||
- shell: some tasks go here
|
||||
- meta: flush_handlers
|
||||
- shell: some other tasks
|
||||
|
||||
In the above example any queued up handlers would be processed early when the 'meta'
|
||||
statement was reached. This is a bit of a niche case but can come in handy from
|
||||
time to time.
|
||||
|
||||
.. _executing_a_playbook:
|
||||
|
||||
Executing A Playbook
|
||||
````````````````````
|
||||
|
||||
Now that you've learned playbook syntax, how do you run a playbook? It's simple.
|
||||
Let's run a playbook using a parallelism level of 10::
|
||||
|
||||
ansible-playbook playbook.yml -f 10
|
||||
|
||||
.. _tips_and_tricks:
|
||||
|
||||
|
||||
Ansible-Pull
|
||||
````````````
|
||||
|
||||
Should you want to invert the architecture of Ansible, so that nodes check in to a central location, instead
|
||||
of pushing configuration out to them, you can.
|
||||
|
||||
Ansible-pull is a small script that will checkout a repo of configuration instructions from git, and then
|
||||
run ansible-playbook against that content.
|
||||
|
||||
Assuming you load balance your checkout location, ansible-pull scales essentially infinitely.
|
||||
|
||||
Run 'ansible-pull --help' for details.
|
||||
|
||||
There's also a `clever playbook <https://github.com/ansible/ansible-examples/blob/master/language_features/ansible_pull.yml>`_ available to using ansible in push mode to configure ansible-pull via a crontab!
|
||||
|
||||
Tips and Tricks
|
||||
```````````````
|
||||
|
||||
Look at the bottom of the playbook execution for a summary of the nodes that were targeted
|
||||
and how they performed. General failures and fatal "unreachable" communication attempts are
|
||||
kept separate in the counts.
|
||||
|
||||
If you ever want to see detailed output from successful modules as well as unsuccessful ones,
|
||||
use the '--verbose' flag. This is available in Ansible 0.5 and later.
|
||||
|
||||
Ansible playbook output is vastly upgraded if the cowsay
|
||||
package is installed. Try it!
|
||||
|
||||
To see what hosts would be affected by a playbook before you run it, you
|
||||
can do this::
|
||||
|
||||
ansible-playbook playbook.yml --list-hosts.
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`YAMLSyntax`
|
||||
Learn about YAML syntax
|
||||
:doc:`playbooks_best_practices`
|
||||
Various tips about managing playbooks in the real world
|
||||
:doc:`index`
|
||||
Hop back to the documentation index for a lot of special topics about playbooks
|
||||
:doc:`modules`
|
||||
Learn about available modules
|
||||
:doc:`developing_modules`
|
||||
Learn how to extend Ansible by writing your own modules
|
||||
:doc:`intro_patterns`
|
||||
Learn about how to select hosts
|
||||
`Github examples directory <https://github.com/ansible/ansible-examples>`_
|
||||
Complete end-to-end playbook examples
|
||||
`Mailing List <http://groups.google.com/group/ansible-project>`_
|
||||
Questions? Help? Ideas? Stop by the list on Google Groups
|
||||
|
||||
|
135
docsite/rst/playbooks_acceleration.rst
Normal file
135
docsite/rst/playbooks_acceleration.rst
Normal file
|
@ -0,0 +1,135 @@
|
|||
Accelerated Mode
|
||||
================
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
.. versionadded:: 1.3
|
||||
|
||||
While OpenSSH using the ControlPersist feature is quite fast and scalable, there is a certain small amount of overhead involved in
|
||||
using SSH connections. While many people will not encounter a need, if you are running on a platform that doesn't have ControlPersist support (such as an EL6 control machine), you'll probably be even more interested in tuning options.
|
||||
|
||||
Accelerate mode is there to help connections work faster, but still uses SSH for initial secure key exchange. There is no
|
||||
additional public key infrastructure to manage, and this does not require things like NTP or even DNS.
|
||||
|
||||
Accelerated mode can be anywhere from 2-6x faster than SSH with ControlPersist enabled, and 10x faster than paramiko.
|
||||
|
||||
Accelerated mode works by launching a temporary daemon over SSH. Once the daemon is running, Ansible will connect directly
|
||||
to it via a socket connection. Ansible secures this communication by using a temporary AES key that is exchanged during
|
||||
the SSH connection (this key is different for every host, and is also regenerated periodically).
|
||||
|
||||
By default, Ansible will use port 5099 for the accelerated connection, though this is configurable. Once running, the daemon will accept connections for 30 minutes, after which time it will terminate itself and need to be restarted over SSH.
|
||||
|
||||
Accelerated mode offers several improvements over the original fireball mode from which it was based:
|
||||
|
||||
* No bootstrapping is required, only a single line needs to be added to each play you wish to run in accelerated mode.
|
||||
* Support for sudo commands (see below for more details and caveats) is available.
|
||||
* There are fewer requirements. ZeroMQ is no longer required, nor are there any special packages beyond python-keyczar.
|
||||
|
||||
In order to use accelerated mode, simply add `accelerate: true` to your play::
|
||||
|
||||
---
|
||||
- hosts: all
|
||||
accelerate: true
|
||||
tasks:
|
||||
- name: some task
|
||||
command: echo {{ item }}
|
||||
with_items:
|
||||
- foo
|
||||
- bar
|
||||
- baz
|
||||
|
||||
If you wish to change the port Ansible will use for the accelerated connection, just add the `accelerated_port` option::
|
||||
|
||||
---
|
||||
- hosts: all
|
||||
accelerate: true
|
||||
# default port is 5099
|
||||
accelerate_port: 10000
|
||||
|
||||
The `accelerate_port` option can also be specified in the environment variable ACCELERATE_PORT, or in your `ansible.cfg` configuration::
|
||||
|
||||
[accelerate]
|
||||
accelerate_port = 5099
|
||||
|
||||
As noted above, accelerated mode also supports running tasks via sudo, however there are two important caveats:
|
||||
|
||||
* You must remove requiretty from your sudoers options.
|
||||
* Prompting for the sudo password is not yet supported, so the NOPASSWD option is required for sudo'ed commands.
|
||||
|
||||
.. _fireball_mode:
|
||||
|
||||
Fireball Mode
|
||||
`````````````
|
||||
|
||||
.. versionadded:: 0.8 (deprecated as of 1.3)
|
||||
|
||||
.. note::
|
||||
|
||||
The following section has been deprecated as of Ansible 1.3 in favor of the accelerated mode described above. This
|
||||
documentation is here for users who may still be using the original fireball connection method only, and should not
|
||||
be used for any new deployments.
|
||||
|
||||
Ansible's core connection types of 'local', 'paramiko', and 'ssh' are augmented in version 0.8 and later by a new extra-fast
|
||||
connection type called 'fireball'. It can only be used with playbooks and does require some additional setup
|
||||
outside the lines of Ansible's normal "no bootstrapping" philosophy. You are not required to use fireball mode
|
||||
to use Ansible, though some users may appreciate it.
|
||||
|
||||
Fireball mode works by launching a temporary 0mq daemon from SSH that by default lives for only 30 minutes before
|
||||
shutting off. Fireball mode, once running, uses temporary AES keys to encrypt a session, and requires direct
|
||||
communication to given nodes on the configured port. The default is 5099. The fireball daemon runs as any user you
|
||||
set it down as. So it can run as you, root, or so on. If multiple users are running Ansible as the same batch of hosts,
|
||||
take care to use unique ports.
|
||||
|
||||
Fireball mode is roughly 10 times faster than paramiko for communicating with nodes and may be a good option
|
||||
if you have a large number of hosts::
|
||||
|
||||
---
|
||||
|
||||
# set up the fireball transport
|
||||
- hosts: all
|
||||
gather_facts: no
|
||||
connection: ssh # or paramiko
|
||||
sudo: yes
|
||||
tasks:
|
||||
- action: fireball
|
||||
|
||||
# these operations will occur over the fireball transport
|
||||
- hosts: all
|
||||
connection: fireball
|
||||
tasks:
|
||||
- shell: echo "Hello {{ item }}"
|
||||
with_items:
|
||||
- one
|
||||
- two
|
||||
|
||||
In order to use fireball mode, certain dependencies must be installed on both ends. You can use this playbook as a basis for initial bootstrapping on
|
||||
any platform. You will also need gcc and zeromq-devel installed from your package manager, which you can of course also get Ansible to install::
|
||||
|
||||
---
|
||||
- hosts: all
|
||||
sudo: yes
|
||||
gather_facts: no
|
||||
connection: ssh
|
||||
tasks:
|
||||
- easy_install: name=pip
|
||||
- pip: name={{ item }} state=present
|
||||
with_items:
|
||||
- pyzmq
|
||||
- pyasn1
|
||||
- PyCrypto
|
||||
- python-keyczar
|
||||
|
||||
Fedora and EPEL also have Ansible RPM subpackages available for fireball-dependencies.
|
||||
|
||||
Also see the module documentation section.
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`playbooks`
|
||||
Introductory playbook information
|
||||
`User Mailing List <http://groups.google.com/group/ansible-devel>`_
|
||||
Have a question? Stop by the google group!
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
65
docsite/rst/playbooks_async.rst
Normal file
65
docsite/rst/playbooks_async.rst
Normal file
|
@ -0,0 +1,65 @@
|
|||
Asynchronous Actions and Polling
|
||||
================================
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
By default tasks in playbooks block, meaning the connections stay open
|
||||
until the task is done on each node. This may not always be desirable, or you may
|
||||
be running operations that take longer than the SSH timeout.
|
||||
|
||||
The easiest way to do this is
|
||||
to kick them off all at once and then poll until they are done.
|
||||
|
||||
You will also want to use asynchronous mode on very long running
|
||||
operations that might be subject to timeout.
|
||||
|
||||
To launch a task asynchronously, specify its maximum runtime
|
||||
and how frequently you would like to poll for status. The default
|
||||
poll value is 10 seconds if you do not specify a value for `poll`::
|
||||
|
||||
---
|
||||
- hosts: all
|
||||
remote_user: root
|
||||
tasks:
|
||||
- name: simulate long running op (15 sec), wait for up to 45, poll every 5
|
||||
command: /bin/sleep 15
|
||||
async: 45
|
||||
poll: 5
|
||||
|
||||
.. note::
|
||||
There is no default for the async time limit. If you leave off the
|
||||
'async' keyword, the task runs synchronously, which is Ansible's
|
||||
default.
|
||||
|
||||
Alternatively, if you do not need to wait on the task to complete, you may
|
||||
"fire and forget" by specifying a poll value of 0::
|
||||
|
||||
---
|
||||
- hosts: all
|
||||
remote_user: root
|
||||
tasks:
|
||||
- name: simulate long running op, allow to run for 45, fire and forget
|
||||
command: /bin/sleep 15
|
||||
async: 45
|
||||
poll: 0
|
||||
|
||||
.. note::
|
||||
You shouldn't "fire and forget" with operations that require
|
||||
exclusive locks, such as yum transactions, if you expect to run other
|
||||
commands later in the playbook against those same resources.
|
||||
|
||||
.. note::
|
||||
Using a higher value for ``--forks`` will result in kicking off asynchronous
|
||||
tasks even faster. This also increases the efficiency of polling.
|
||||
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`playbooks`
|
||||
An introduction to playbooks
|
||||
`User Mailing List <http://groups.google.com/group/ansible-devel>`_
|
||||
Have a question? Stop by the google group!
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
392
docsite/rst/playbooks_best_practices.rst
Normal file
392
docsite/rst/playbooks_best_practices.rst
Normal file
|
@ -0,0 +1,392 @@
|
|||
Best Practices
|
||||
==============
|
||||
|
||||
Here are some tips for making the most of Ansible playbooks.
|
||||
|
||||
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!).
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
.. _content_organization:
|
||||
|
||||
Content Organization
|
||||
++++++++++++++++++++++
|
||||
|
||||
The following section shows one of many possible ways to organize playbook content. Your usage of Ansible should fit your needs, however, not ours, so feel free to modify this approach and organize as you see fit.
|
||||
|
||||
(One thing you will definitely want to do though, is use the "roles" organization feature, which is documented as part
|
||||
of the main playbooks page. See :doc:`playbooks_roles`).
|
||||
|
||||
.. _directory_layout:
|
||||
|
||||
Directory Layout
|
||||
````````````````
|
||||
|
||||
The top level of the directory would contain files and directories like so::
|
||||
|
||||
production # inventory file for production servers
|
||||
stage # inventory file for stage environment
|
||||
|
||||
group_vars/
|
||||
group1 # here we assign variables to particular groups
|
||||
group2 # ""
|
||||
host_vars/
|
||||
hostname1 # if systems need specific variables, put them here
|
||||
hostname2 # ""
|
||||
|
||||
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
|
||||
handlers/ #
|
||||
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
|
||||
foo.sh # <-- script files for use with the script resource
|
||||
vars/ #
|
||||
main.yml # <-- variables associated with this role
|
||||
|
||||
webtier/ # same kind of structure as "common" was above, done for the webtier role
|
||||
monitoring/ # ""
|
||||
fooapp/ # ""
|
||||
|
||||
.. _stage_vs_prod:
|
||||
|
||||
How to Arrange Inventory, Stage vs Production
|
||||
`````````````````````````````````````````````
|
||||
|
||||
In the example below, the *production* file contains the inventory of all of your production hosts. Of course you can pull inventory from an external data source as well, but this is just a basic example.
|
||||
|
||||
It is suggested that you define groups based on purpose of the host (roles) and also geography or datacenter location (if applicable)::
|
||||
|
||||
# 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
|
||||
|
||||
|
||||
.. _groups_and_hosts:
|
||||
|
||||
Group And Host Variables
|
||||
````````````````````````
|
||||
|
||||
Now, 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::
|
||||
|
||||
---
|
||||
# file: group_vars/atlanta
|
||||
ntp: ntp-atlanta.example.com
|
||||
backup: backup-atlanta.example.com
|
||||
|
||||
Variables aren't just for geographic information either! Maybe the webservers have some configuration that doesn't make sense for the database servers::
|
||||
|
||||
---
|
||||
# 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
|
||||
backup: backup-boston.example.com
|
||||
|
||||
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
|
||||
|
||||
.. _split_by_role:
|
||||
|
||||
Top Level Playbooks Are Separated By Role
|
||||
`````````````````````````````````````````
|
||||
|
||||
In site.yml, we include a playbook that defines our entire infrastructure. Note this is SUPER short, because it's just including
|
||||
some other playbooks. Remember, playbooks are nothing more than lists of plays::
|
||||
|
||||
---
|
||||
# file: site.yml
|
||||
- include: webservers.yml
|
||||
- include: dbservers.yml
|
||||
|
||||
In a file like webservers.yml (also at the top level), we simply map the configuration of the webservers group to the roles performed by the webservers group. Also notice this is incredibly short. For example::
|
||||
|
||||
---
|
||||
# file: webservers.yml
|
||||
- hosts: webservers
|
||||
roles:
|
||||
- common
|
||||
- webtier
|
||||
|
||||
.. _role_organization:
|
||||
|
||||
Task And Handler Organization For A Role
|
||||
````````````````````````````````````````
|
||||
|
||||
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::
|
||||
|
||||
---
|
||||
# file: roles/common/tasks/main.yml
|
||||
|
||||
- name: be sure ntp is installed
|
||||
yum: pkg=ntp state=installed
|
||||
tags: ntp
|
||||
|
||||
- name: be sure ntp is configured
|
||||
template: src=ntp.conf.j2 dest=/etc/ntp.conf
|
||||
notify:
|
||||
- restart ntpd
|
||||
tags: ntp
|
||||
|
||||
- name: be sure ntpd is running and enabled
|
||||
service: name=ntpd state=running enabled=yes
|
||||
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::
|
||||
|
||||
---
|
||||
# file: roles/common/handlers/main.yml
|
||||
- name: restart ntpd
|
||||
service: name=ntpd state=restarted
|
||||
|
||||
See :doc:`playbooks_roles` for more information.
|
||||
|
||||
|
||||
.. _organization_examples:
|
||||
|
||||
What This Organization Enables (Examples)
|
||||
`````````````````````````````````````````
|
||||
|
||||
Above we've shared our basic organizational structure.
|
||||
|
||||
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
|
||||
|
||||
What about just reconfiguring NTP on everything? Easy.::
|
||||
|
||||
ansible-playbook -i production site.yml --tags ntp
|
||||
|
||||
What about just reconfiguring my webservers?::
|
||||
|
||||
ansible-playbook -i production webservers.yml
|
||||
|
||||
What about just my webservers in Boston?::
|
||||
|
||||
ansible-playbook -i production webservers.yml --limit boston
|
||||
|
||||
What about just the first 10, and then the next 10?::
|
||||
|
||||
ansible-playbook -i production webservers.yml --limit boston[0-10]
|
||||
ansible-playbook -i production webservers.yml --limit boston[10-20]
|
||||
|
||||
And of course just basic ad-hoc stuff is also possible.::
|
||||
|
||||
ansible -i production -m ping
|
||||
ansible -i production -m command -a '/sbin/reboot' --limit boston
|
||||
|
||||
And there are some useful commands to know (at least in 1.1 and higher)::
|
||||
|
||||
# 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
|
||||
|
||||
.. _dep_vs_config:
|
||||
|
||||
Deployment vs Configuration Organization
|
||||
````````````````````````````````````````
|
||||
|
||||
The above setup models a typical configuration topology. When doing multi-tier deployments, there are going
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
Ansible allows you to deploy and configure using the same tool, so you would likely reuse groups and just
|
||||
keep the OS configuration in separate playbooks from the app deployment.
|
||||
|
||||
.. _stage_vs_production:
|
||||
|
||||
Stage vs Production
|
||||
+++++++++++++++++++
|
||||
|
||||
As also mentioned above, a good way to keep your stage (or testing) and production environments separate is to use a separate inventory file for stage and production. This way you pick with -i what you are targeting. Keeping them all in one file can lead to surprises!
|
||||
|
||||
Testing things in a stage environment before trying in production is always a great idea. Your environments need not be the same
|
||||
size and you can use group variables to control the differences between those environments.
|
||||
|
||||
.. _rolling_update:
|
||||
|
||||
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.
|
||||
|
||||
See :doc:`playbooks_delegation`.
|
||||
|
||||
.. _mention_the_state:
|
||||
|
||||
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.
|
||||
|
||||
.. _group_by_roles:
|
||||
|
||||
Group By Roles
|
||||
++++++++++++++
|
||||
|
||||
A system can be in multiple groups. See :doc:`intro_inventory` and :doc:`intro_patterns`. Having groups named after things like
|
||||
*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.
|
||||
|
||||
See :doc:`playbooks_roles`.
|
||||
|
||||
.. _os_variance:
|
||||
|
||||
Operating System and Distribution Variance
|
||||
++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
When dealing with a parameter that is different between two different operating systems, the best way to handle this is
|
||||
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::
|
||||
|
||||
---
|
||||
|
||||
# talk to all hosts just so we can learn about them
|
||||
|
||||
- hosts: all
|
||||
tasks:
|
||||
- group_by: key={{ ansible_distribution }}
|
||||
|
||||
# now just on the CentOS hosts...
|
||||
|
||||
- hosts: CentOS
|
||||
gather_facts: False
|
||||
tasks:
|
||||
- # tasks that only happen on CentOS go here
|
||||
|
||||
If group-specific settings are needed, this can also be done. For example::
|
||||
|
||||
---
|
||||
# file: group_vars/all
|
||||
asdf: 10
|
||||
|
||||
---
|
||||
# file: group_vars/CentOS
|
||||
asdf: 42
|
||||
|
||||
In the above example, CentOS machines get the value of '42' for asdf, but other machines get '10'.
|
||||
|
||||
.. _ship_modules_with_playbooks:
|
||||
|
||||
Bundling Ansible Modules With Playbooks
|
||||
+++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
.. versionadded:: 0.5
|
||||
|
||||
If a playbook has a "./library" directory relative to its 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.
|
||||
|
||||
.. _whitespace:
|
||||
|
||||
Whitespace and Comments
|
||||
+++++++++++++++++++++++
|
||||
|
||||
Generous use of whitespace to break things up, and use of comments (which start with '#'), is encouraged.
|
||||
|
||||
.. _name_tasks:
|
||||
|
||||
Always Name Tasks
|
||||
+++++++++++++++++
|
||||
|
||||
It is possible to leave off the 'name' for a given task, though it is recommended to provide a description
|
||||
about why something is being done instead. This name is shown when the playbook is run.
|
||||
|
||||
.. _keep_it_simple:
|
||||
|
||||
Keep It Simple
|
||||
++++++++++++++
|
||||
|
||||
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 will probably not need 'vars',
|
||||
'vars_files', 'vars_prompt' and '--extra-vars' all at once,
|
||||
while also using an external inventory file.
|
||||
|
||||
.. _version_control:
|
||||
|
||||
Version Control
|
||||
+++++++++++++++
|
||||
|
||||
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 that are automating your infrastructure.
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`YAMLSyntax`
|
||||
Learn about YAML syntax
|
||||
:doc:`playbooks`
|
||||
Review the basic playbook features
|
||||
:doc:`modules`
|
||||
Learn about available modules
|
||||
:doc:`developing_modules`
|
||||
Learn how to extend Ansible by writing your own modules
|
||||
:doc:`intro_patterns`
|
||||
Learn about how to select hosts
|
||||
`Github examples directory <https://github.com/ansible/ansible/tree/devel/examples/playbooks>`_
|
||||
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
|
56
docsite/rst/playbooks_checkmode.rst
Normal file
56
docsite/rst/playbooks_checkmode.rst
Normal file
|
@ -0,0 +1,56 @@
|
|||
Check Mode ("Dry Run")
|
||||
======================
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
.. versionadded:: 1.1
|
||||
|
||||
When ansible-playbook is executed with --check it will not make any changes on remote systems. Instead, any module
|
||||
instrumented to support 'check mode' (which contains most of the primary core modules, but it is not required that all modules do
|
||||
this) will report what changes they would have made rather than making them. Other modules that do not support check mode will also take no action, but just will not report what changes they might have made.
|
||||
|
||||
Check mode is just a simulation, and if you have steps that use conditionals that depend on the results of prior commands,
|
||||
it may be less useful for you. However it is great for one-node-at-time basic configuration management use cases.
|
||||
|
||||
Example::
|
||||
|
||||
ansible-playbook foo.yml --check
|
||||
|
||||
.. _forcing_to_run_in_check_mode:
|
||||
|
||||
Running a task in check mode
|
||||
````````````````````````````
|
||||
|
||||
.. versionadded:: 1.3
|
||||
|
||||
Sometimes you may want to have a task to be executed even in check
|
||||
mode. To achieve this, use the `always_run` clause on the task. Its
|
||||
value is a Jinja2 expression, just like the `when` clause. In simple
|
||||
cases a boolean YAML value would be sufficient as a value.
|
||||
|
||||
Example::
|
||||
|
||||
tasks:
|
||||
|
||||
- name: this task is run even in check mode
|
||||
command: /something/to/run --even-in-check-mode
|
||||
always_run: yes
|
||||
|
||||
As a reminder, a task with a `when` clause evaluated to false, will
|
||||
still be skipped even if it has a `always_run` clause evaluated to
|
||||
true.
|
||||
|
||||
.. _diff_mode:
|
||||
|
||||
Showing Differences with --diff
|
||||
```````````````````````````````
|
||||
|
||||
.. versionadded:: 1.1
|
||||
|
||||
The --diff option to ansible-playbook works great with --check (detailed above) but can also be used by itself. When this flag is supplied, if any templated files on the remote system are changed, and the ansible-playbook CLI will report back
|
||||
the textual changes made to the file (or, if used with --check, the changes that would have been made). Since the diff
|
||||
feature produces a large amount of output, it is best used when checking a single host at a time, like so::
|
||||
|
||||
ansible-playbook foo.yml --check --diff --limit foo.example.com
|
||||
|
260
docsite/rst/playbooks_conditionals.rst
Normal file
260
docsite/rst/playbooks_conditionals.rst
Normal file
|
@ -0,0 +1,260 @@
|
|||
Conditionals
|
||||
============
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
Often the result of a play may depend on the value of a variable, fact (something learned about the remote system),
|
||||
or previous task result. In some cases, the values of variables may depend on other variables.
|
||||
Further, additional groups can be created to manage hosts based on
|
||||
whether the hosts match other criteria. There are many options to control execution flow in Ansible.
|
||||
|
||||
Let's dig into what they are.
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
The When Statement
|
||||
``````````````````
|
||||
|
||||
Sometimes you will want to skip a particular step on a particular host. This could be something
|
||||
as simple as not installing a certain package if the operating system is a particular version,
|
||||
or it could be something like performing some cleanup steps if a filesystem is getting full.
|
||||
|
||||
This is easy to do in Ansible, with the `when` clause, which contains a Jinja2 expression (see `playbooks_variables`).
|
||||
It's actually pretty simple::
|
||||
|
||||
tasks:
|
||||
- name: "shutdown Debian flavored systems"
|
||||
command: /sbin/shutdown -t now
|
||||
when: ansible_os_family == "Debian"
|
||||
|
||||
A number of Jinja2 "filters" can also be used in when statements, some of which are unique
|
||||
and provided by Ansible. Suppose we want to ignore the error of one statement and then
|
||||
decide to do something conditionally based on success or failure::
|
||||
|
||||
tasks:
|
||||
- command: /bin/false
|
||||
register: result
|
||||
ignore_errors: True
|
||||
- command: /bin/something
|
||||
when: result|failed
|
||||
- command: /bin/something_else
|
||||
when: result|success
|
||||
- command: /bin/still/something_else
|
||||
when: result|skipped
|
||||
|
||||
Note that was a little bit of foreshadowing on the 'register' statement. We'll get to it a bit later in this chapter.
|
||||
|
||||
As a reminder, to see what facts are available on a particular system, you can do::
|
||||
|
||||
ansible hostname.example.com -m setup
|
||||
|
||||
Tip: Sometimes you'll get back a variable that's a string and you'll want to do a math operation comparison on it. You can do this like so::
|
||||
|
||||
tasks:
|
||||
- shell: echo "only on Red Hat 6, derivatives, and later"
|
||||
when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int >= 6
|
||||
|
||||
.. note:: the above example requires the lsb_release package on the target host in order to return the ansible_lsb.major_release fact.
|
||||
|
||||
Variables defined in the playbooks or inventory can also be used. An example may be the execution of a task based on a variable's boolean value::
|
||||
|
||||
vars:
|
||||
epic: true
|
||||
|
||||
Then a conditional execution might look like::
|
||||
|
||||
tasks:
|
||||
- shell: echo "This certainly is epic!"
|
||||
when: epic
|
||||
|
||||
or::
|
||||
|
||||
tasks:
|
||||
- shell: echo "This certainly isn't epic!"
|
||||
when: not epic
|
||||
|
||||
If a required variable has not been set, you can skip or fail using Jinja2's
|
||||
`defined` test. For example::
|
||||
|
||||
tasks:
|
||||
- shell: echo "I've got '{{ foo }}' and am not afraid to use it!"
|
||||
when: foo is defined
|
||||
|
||||
- fail: msg="Bailing out: this play requires 'bar'"
|
||||
when: bar is not defined
|
||||
|
||||
This is especially useful in combination with the conditional import of vars
|
||||
files (see below).
|
||||
|
||||
Note that when combining `when` with `with_items` (see :doc:`playbooks_loops`), be aware that the `when` statement is processed separately for each item. This is by design::
|
||||
|
||||
tasks:
|
||||
- command: echo {{ item }}
|
||||
with_items: [ 0, 2, 4, 6, 8, 10 ]
|
||||
when: item > 5
|
||||
|
||||
Loading in Custom Facts
|
||||
```````````````````````
|
||||
|
||||
It's also easy to provide your own facts if you want, which is covered in :doc:`developing_modules`. To run them, just
|
||||
make a call to your own custom fact gathering module at the top of your list of tasks, and variables returned
|
||||
there will be accessible to future tasks::
|
||||
|
||||
tasks:
|
||||
- name: gather site specific fact data
|
||||
action: site_facts
|
||||
- command: /usr/bin/thingy
|
||||
when: my_custom_fact_just_retrieved_from_the_remote_system == '1234'
|
||||
|
||||
Applying 'when' to roles and includes
|
||||
`````````````````````````````````````
|
||||
|
||||
Note that if you have several tasks that all share the same conditional statement, you can affix the conditional
|
||||
to a task include statement as below. Note this does not work with playbook includes, just task includes. All the tasks
|
||||
get evaluated, but the conditional is applied to each and every task::
|
||||
|
||||
- include: tasks/sometasks.yml
|
||||
when: "'reticulating splines' in output"
|
||||
|
||||
Or with a role::
|
||||
|
||||
- hosts: webservers
|
||||
roles:
|
||||
- { role: debian_stock_config, when: ansible_os_family == 'Debian' }
|
||||
|
||||
You will note a lot of 'skipped' output by default in Ansible when using this approach on systems that don't match the criteria.
|
||||
Read up on the 'group_by' module in the `modules` docs for a more streamlined way to accomplish the same thing.
|
||||
|
||||
Conditional Imports
|
||||
```````````````````
|
||||
|
||||
.. note:: This is an advanced topic that is infrequently used. You can probably skip this section.
|
||||
|
||||
Sometimes you will want to do certain things differently in a playbook based on certain criteria.
|
||||
Having one playbook that works on multiple platforms and OS versions is a good example.
|
||||
|
||||
As an example, the name of the Apache package may be different between CentOS and Debian,
|
||||
but it is easily handled with a minimum of syntax in an Ansible Playbook::
|
||||
|
||||
---
|
||||
- hosts: all
|
||||
remote_user: root
|
||||
vars_files:
|
||||
- "vars/common.yml"
|
||||
- [ "vars/{{ ansible_os_family }}.yml", "vars/os_defaults.yml" ]
|
||||
tasks:
|
||||
- name: make sure apache is running
|
||||
service: name={{ apache }} state=running
|
||||
|
||||
.. note::
|
||||
The variable 'ansible_os_family' is being interpolated into
|
||||
the list of filenames being defined for vars_files.
|
||||
|
||||
As a reminder, the various YAML files contain just keys and values::
|
||||
|
||||
---
|
||||
# for vars/CentOS.yml
|
||||
apache: httpd
|
||||
somethingelse: 42
|
||||
|
||||
How does this work? If the operating system was 'CentOS', the first file Ansible would try to import
|
||||
would be 'vars/CentOS.yml', followed by '/vars/os_defaults.yml' if that file
|
||||
did not exist. If no files in the list were found, an error would be raised.
|
||||
On Debian, it would instead first look towards 'vars/Debian.yml' instead of 'vars/CentOS.yml', before
|
||||
falling back on 'vars/os_defaults.yml'. Pretty simple.
|
||||
|
||||
To use this conditional import feature, you'll need facter or ohai installed prior to running the playbook, but
|
||||
you can of course push this out with Ansible if you like::
|
||||
|
||||
# for facter
|
||||
ansible -m yum -a "pkg=facter ensure=installed"
|
||||
ansible -m yum -a "pkg=ruby-json ensure=installed"
|
||||
|
||||
# for ohai
|
||||
ansible -m yum -a "pkg=ohai ensure=installed"
|
||||
|
||||
Ansible's approach to configuration -- separating variables from tasks, keeps your playbooks
|
||||
from turning into arbitrary code with ugly nested ifs, conditionals, and so on - and results
|
||||
in more streamlined & auditable configuration rules -- especially because there are a
|
||||
minimum of decision points to track.
|
||||
|
||||
Selecting Files And Templates Based On Variables
|
||||
````````````````````````````````````````````````
|
||||
|
||||
.. note:: This is an advanced topic that is infrequently used. You can probably skip this section.
|
||||
|
||||
Sometimes a configuration file you want to copy, or a template you will use may depend on a variable.
|
||||
The following construct selects the first available file appropriate for the variables of a given host, which is often much cleaner than putting a lot of if conditionals in a template.
|
||||
|
||||
The following example shows how to template out a configuration file that was very different between, say, CentOS and Debian::
|
||||
|
||||
- name: template a file
|
||||
template: src={{ item }} dest=/etc/myapp/foo.conf
|
||||
with_first_found:
|
||||
files:
|
||||
- {{ ansible_distribution }}.conf
|
||||
- default.conf
|
||||
paths:
|
||||
- search_location_one/somedir/
|
||||
- /opt/other_location/somedir/
|
||||
|
||||
Register Variables
|
||||
``````````````````
|
||||
|
||||
Often in a playbook it may be useful to store the result of a given command in a variable and access
|
||||
it later. Use of the command module in this way can in many ways eliminate the need to write site specific facts, for
|
||||
instance, you could test for the existence of a particular program.
|
||||
|
||||
The 'register' keyword decides what variable to save a result in. The resulting variables can be used in templates, action lines, or *when* statements. It looks like this (in an obviously trivial example)::
|
||||
|
||||
- name: test play
|
||||
hosts: all
|
||||
|
||||
tasks:
|
||||
|
||||
- shell: cat /etc/motd
|
||||
register: motd_contents
|
||||
|
||||
- shell: echo "motd contains the word hi"
|
||||
when: motd_contents.stdout.find('hi') != -1
|
||||
|
||||
As shown previously, the registered variable's string contents are accessible with the 'stdout' value.
|
||||
The registered result can be used in the "with_items" of a task if it is converted into
|
||||
a list (or already is a list) as shown below. "stdout_lines" is already available on the object as
|
||||
well though you could also call "home_dirs.stdout.split()" if you wanted, and could split by other
|
||||
fields::
|
||||
|
||||
- name: registered variable usage as a with_items list
|
||||
hosts: all
|
||||
|
||||
tasks:
|
||||
|
||||
- name: retrieve the list of home directories
|
||||
command: ls /home
|
||||
register: home_dirs
|
||||
|
||||
- name: add home dirs to the backup spooler
|
||||
file: path=/mnt/bkspool/{{ item }} src=/home/{{ item }} state=link
|
||||
with_items: home_dirs.stdout_lines
|
||||
# same as with_items: home_dirs.stdout.split()
|
||||
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`playbooks`
|
||||
An introduction to playbooks
|
||||
:doc:`playbooks_roles`
|
||||
Playbook organization by roles
|
||||
:doc:`playbooks_best_practices`
|
||||
Best practices in playbooks
|
||||
:doc:`playbooks_conditionals`
|
||||
Conditional statements in playbooks
|
||||
:doc:`playbooks_variables`
|
||||
All about variables
|
||||
`User Mailing List <http://groups.google.com/group/ansible-devel>`_
|
||||
Have a question? Stop by the google group!
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
148
docsite/rst/playbooks_delegation.rst
Normal file
148
docsite/rst/playbooks_delegation.rst
Normal file
|
@ -0,0 +1,148 @@
|
|||
Delegation, Rolling Updates, and Local Actions
|
||||
==============================================
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
Being designed for multi-tier deployments since the beginning, Ansible is great at doing things on one host on behalf of another, or doing local steps with reference to some remote hosts.
|
||||
|
||||
This in particular this is very applicable when setting up continuous deployment infrastructure or zero downtime rolling updates, where you might be talking with load balancers or monitoring systems.
|
||||
|
||||
Additional features allow for tuning the orders in which things complete, and assigning a batch window size for how many machines to process at once during a rolling update.
|
||||
|
||||
This section covers all of these features. For examples of these items in use, `please see the ansible-examples repository <http://github.com/ansible/ansible-examples/>`_. There are quite a few examples of zero-downtime update procedures for different kinds of applications.
|
||||
|
||||
You should also consult the :doc:`modules` section, various modules like 'ec2_elb', 'nagios', and 'bigip_pool', and 'netscaler' dovetail neatly with the concepts mentioned here.
|
||||
|
||||
You'll also want to read up on :doc:`playbooks_roles`, as the 'pre_task' and 'post_task' concepts are the places where you would typically call these modules.
|
||||
|
||||
.. _rolling_update_batch_size:
|
||||
|
||||
Rolling Update Batch Size
|
||||
`````````````````````````
|
||||
|
||||
.. versionadded:: 0.7
|
||||
|
||||
By default, Ansible will try to manage all of the machines referenced in a play in parallel. For a rolling updates
|
||||
use case, you can define how many hosts Ansible should manage at a single time by using the ''serial'' keyword::
|
||||
|
||||
|
||||
- name: test play
|
||||
hosts: webservers
|
||||
serial: 3
|
||||
|
||||
In the above example, if we had 100 hosts, 3 hosts in the group 'webservers'
|
||||
would complete the play completely before moving on to the next 3 hosts.
|
||||
|
||||
.. _maximum_failure_percentage:
|
||||
|
||||
Maximum Failure Percentage
|
||||
``````````````````````````
|
||||
|
||||
.. versionadded:: 1.3
|
||||
|
||||
By default, Ansible will continue executing actions as long as there are hosts in the group that have not yet failed.
|
||||
In some situations, such as with the rolling updates described above, it may be desirable to abort the play when a
|
||||
certain threshold of failures have been reached. To achieve this, as of version 1.3 you can set a maximum failure
|
||||
percentage on a play as follows::
|
||||
|
||||
- hosts: webservers
|
||||
max_fail_percentage: 30
|
||||
serial: 10
|
||||
|
||||
In the above example, if more than 3 of the 10 servers in the group were to fail, the rest of the play would be aborted.
|
||||
|
||||
.. note::
|
||||
|
||||
The percentage set must be exceeded, not equaled. For example, if serial were set to 4 and you wanted the task to abort
|
||||
when 2 of the systems failed, the percentage should be set at 49 rather than 50.
|
||||
|
||||
.. _delegation:
|
||||
|
||||
Delegation
|
||||
``````````
|
||||
|
||||
.. versionadded:: 0.7
|
||||
|
||||
This isn't actually rolling update specific but comes up frequently in those cases.
|
||||
|
||||
If you want to perform a task on one host with reference to other hosts, use the 'delegate_to' keyword on a task.
|
||||
This is ideal for placing nodes in a load balanced pool, or removing them. It is also very useful for controlling
|
||||
outage windows. Using this with the 'serial' keyword to control the number of hosts executing at one time is also
|
||||
a good idea::
|
||||
|
||||
---
|
||||
- hosts: webservers
|
||||
serial: 5
|
||||
|
||||
tasks:
|
||||
- name: take out of load balancer pool
|
||||
command: /usr/bin/take_out_of_pool {{ inventory_hostname }}
|
||||
delegate_to: 127.0.0.1
|
||||
|
||||
- name: actual steps would go here
|
||||
yum: name=acme-web-stack state=latest
|
||||
|
||||
- name: add back to load balancer pool
|
||||
command: /usr/bin/add_back_to_pool {{ inventory_hostname }}
|
||||
delegate_to: 127.0.0.1
|
||||
|
||||
|
||||
These commands will run on 127.0.0.1, which is the machine running Ansible. There is also a shorthand syntax that
|
||||
you can use on a per-task basis: 'local_action'. Here is the same playbook as above, but using the shorthand
|
||||
syntax for delegating to 127.0.0.1::
|
||||
|
||||
---
|
||||
# ...
|
||||
tasks:
|
||||
- name: take out of load balancer pool
|
||||
local_action: command /usr/bin/take_out_of_pool {{ inventory_hostname }}
|
||||
|
||||
# ...
|
||||
|
||||
- name: add back to load balancer pool
|
||||
local_action: command /usr/bin/add_back_to_pool {{ inventory_hostname }}
|
||||
|
||||
A common pattern is to use a local action to call 'rsync' to recursively copy files to the managed servers.
|
||||
Here is an example::
|
||||
|
||||
---
|
||||
# ...
|
||||
tasks:
|
||||
- name: recursively copy files from management server to target
|
||||
local_action: command rsync -a /path/to/files {{ inventory_hostname }}:/path/to/target/
|
||||
|
||||
Note that you must have passphrase-less SSH keys or an ssh-agent configured for this to work, otherwise rsync
|
||||
will need to ask for a passphrase.
|
||||
|
||||
.. _local_playbooks:
|
||||
|
||||
Local Playbooks
|
||||
```````````````
|
||||
|
||||
It may be useful to use a playbook locally, rather than by connecting over SSH. This can be useful
|
||||
for assuring the configuration of a system by putting a playbook on a crontab. This may also be used
|
||||
to run a playbook inside a OS installer, such as an Anaconda kickstart.
|
||||
|
||||
To run an entire playbook locally, just set the "hosts:" line to "hosts:127.0.0.1" and then run the playbook like so::
|
||||
|
||||
ansible-playbook playbook.yml --connection=local
|
||||
|
||||
Alternatively, a local connection can be used in a single playbook play, even if other plays in the playbook
|
||||
use the default remote connection type::
|
||||
|
||||
- hosts: 127.0.0.1
|
||||
connection: local
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`playbooks`
|
||||
An introduction to playbooks
|
||||
`Ansible Examples on GitHub <http://github.com/ansible/ansible-examples>`_
|
||||
Many examples of full-stack deployments
|
||||
`User Mailing List <http://groups.google.com/group/ansible-devel>`_
|
||||
Have a question? Stop by the google group!
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
||||
|
60
docsite/rst/playbooks_environment.rst
Normal file
60
docsite/rst/playbooks_environment.rst
Normal file
|
@ -0,0 +1,60 @@
|
|||
Setting the Environment (and Working With Proxies)
|
||||
==================================================
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
.. versionadded:: 1.1
|
||||
|
||||
It is quite possible that you may need to get package updates through a proxy, or even get some package
|
||||
updates through a proxy and access other packages not through a proxy. Or maybe a script you might wish to
|
||||
call may also need certain environment variables set to run properly.
|
||||
|
||||
Ansible makes it easy for you to configure your environment by using the 'environment' keyword. Here is an example::
|
||||
|
||||
- hosts: all
|
||||
remote_user: root
|
||||
|
||||
tasks:
|
||||
|
||||
- apt: name=cobbler state=installed
|
||||
environment:
|
||||
http_proxy: http://proxy.example.com:8080
|
||||
|
||||
The environment can also be stored in a variable, and accessed like so::
|
||||
|
||||
- hosts: all
|
||||
remote_user: root
|
||||
|
||||
# here we make a variable named "env" that is a dictionary
|
||||
vars:
|
||||
proxy_env:
|
||||
http_proxy: http://proxy.example.com:8080
|
||||
|
||||
tasks:
|
||||
|
||||
- apt: name=cobbler state=installed
|
||||
environment: proxy_env
|
||||
|
||||
While just proxy settings were shown above, any number of settings can be supplied. The most logical place
|
||||
to define an environment hash might be a group_vars file, like so::
|
||||
|
||||
---
|
||||
# file: group_vars/boston
|
||||
|
||||
ntp_server: ntp.bos.example.com
|
||||
backup: bak.bos.example.com
|
||||
proxy_env:
|
||||
http_proxy: http://proxy.bos.example.com:8080
|
||||
https_proxy: http://proxy.bos.example.com:8080
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`playbooks`
|
||||
An introduction to playbooks
|
||||
`User Mailing List <http://groups.google.com/group/ansible-devel>`_
|
||||
Have a question? Stop by the google group!
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
||||
|
99
docsite/rst/playbooks_error_handling.rst
Normal file
99
docsite/rst/playbooks_error_handling.rst
Normal file
|
@ -0,0 +1,99 @@
|
|||
Error Handling In Playbooks
|
||||
===========================
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
Ansible normally has defaults that make sure to check the return codes of commands and modules and
|
||||
it fails fast -- forcing an error to be dealt with unless you decide otherwise.
|
||||
y
|
||||
Sometimes a command that returns 0 isn't an error. Sometimes a command might not always
|
||||
need to report that it 'changed' the remote system. This section describes how to change
|
||||
the default behavior of Ansible for certain tasks so output and error handling behavior is
|
||||
as desired.
|
||||
|
||||
.. _ignoring_failed_commands:
|
||||
|
||||
Ignoring Failed Commands
|
||||
````````````````````````
|
||||
|
||||
.. versionadded:: 0.6
|
||||
|
||||
Generally playbooks will stop executing any more steps on a host that
|
||||
has a failure. Sometimes, though, you want to continue on. To do so,
|
||||
write a task that looks like this::
|
||||
|
||||
- name: this will not be counted as a failure
|
||||
command: /bin/false
|
||||
ignore_errors: yes
|
||||
|
||||
.. _controlling_what_defines_failure:
|
||||
|
||||
Controlling What Defines Failure
|
||||
````````````````````````````````
|
||||
|
||||
.. versionadded:: 1.4
|
||||
|
||||
Suppose the erorr code of a command is meaningless and to tell if there
|
||||
is a failure what really matters is the output of the command, for instance
|
||||
if the string "FAILED" is in the output.
|
||||
|
||||
Ansible in 1.4 and later provides a way to specify this behavior as follows::
|
||||
|
||||
- name: this command prints FAILED when it fails
|
||||
command: /usr/bin/example-command -x -y -z
|
||||
register: command_result
|
||||
failed_when: "'FAILED' in command_result.stderr"
|
||||
|
||||
In previous version of Ansible, this can be still be accomplished as follows::
|
||||
|
||||
- name: this command prints FAILED when it fails
|
||||
command: /usr/bin/example-command -x -y -z
|
||||
register: command_result
|
||||
ignore_errors: True
|
||||
|
||||
- name: fail the play if the previous command did not succeed
|
||||
fail: msg="the command failed"
|
||||
when: "'FAILED' in command_result.stderr"
|
||||
|
||||
.. _override_the_changed_result:
|
||||
|
||||
Overriding The Changed Result
|
||||
`````````````````````````````
|
||||
|
||||
.. versionadded:: 1.3
|
||||
|
||||
When a shell/command or other module runs it will typically report
|
||||
"changed" status based on whether it thinks it affected machine state.
|
||||
|
||||
Sometimes you will know, based on the return code
|
||||
or output that it did not make any changes, and wish to override
|
||||
the "changed" result such that it does not appear in report output or
|
||||
does not cause handlers to fire::
|
||||
|
||||
tasks:
|
||||
|
||||
- shell: /usr/bin/billybass --mode="take me to the river"
|
||||
register: bass_result
|
||||
changed_when: "bass_result.rc != 2"
|
||||
|
||||
# this will never report 'changed' status
|
||||
- shell: wall 'beep'
|
||||
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`playbooks`
|
||||
An introduction to playbooks
|
||||
:doc:`playbooks_best_practices`
|
||||
Best practices in playbooks
|
||||
:doc:`playbooks_conditionals`
|
||||
Conditional statements in playbooks
|
||||
:doc:`playbooks_variables`
|
||||
All about variables
|
||||
`User Mailing List <http://groups.google.com/group/ansible-devel>`_
|
||||
Have a question? Stop by the google group!
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
||||
|
115
docsite/rst/playbooks_lookups.rst
Normal file
115
docsite/rst/playbooks_lookups.rst
Normal file
|
@ -0,0 +1,115 @@
|
|||
Using Lookups
|
||||
=============
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
Lookup plugins allow access of data in Ansible from outside sources. This can include the filesystem
|
||||
but also external datastores. These values are then made available using the standard templating system
|
||||
in Ansible, and are typically used to load variables or templates with information from those systems.
|
||||
|
||||
.. note:: This is considered an advanced feature, and many users will probably not rely on these features.
|
||||
|
||||
.. _getting_file_contents:
|
||||
|
||||
Intro to Lookups: Getting File Contents
|
||||
```````````````````````````````````````
|
||||
|
||||
The file lookup is the most basic lookup type.
|
||||
|
||||
Contents can be read off the filesystem as follows::
|
||||
|
||||
- hosts: all
|
||||
vars:
|
||||
contents: "{{ lookup('file', '/etc/foo.txt') }}"
|
||||
tasks:
|
||||
- debug: msg="the value of foo.txt is {{ contents }}"
|
||||
|
||||
.. _password_lookup:
|
||||
|
||||
The Password Lookup
|
||||
```````````````````
|
||||
|
||||
``password`` generates a random plaintext password and store it in
|
||||
a file at a given filepath. Support for crypted save modes (as with vars_prompt) is pending. If the
|
||||
file exists previously, it will retrieve its contents, behaving just like with_file. Usage of variables like "{{ inventory_hostname }}" in the filepath can be used to set
|
||||
up random passwords per host (what simplifies password management in 'host_vars' variables).
|
||||
|
||||
Generated passwords contain a random mix of upper and lowercase ASCII letters, the
|
||||
numbers 0-9 and punctuation (". , : - _"). The default length of a generated password is 30 characters.
|
||||
This length can be changed by passing an extra parameter::
|
||||
|
||||
---
|
||||
- hosts: all
|
||||
|
||||
tasks:
|
||||
|
||||
# create a mysql user with a random password:
|
||||
- mysql_user: name={{ client }}
|
||||
password="{{ lookup('password', 'credentials/' + client + '/' + tier + '/' + role + '/mysqlpassword length=15') }}"
|
||||
priv={{ client }}_{{ tier }}_{{ role }}.*:ALL
|
||||
|
||||
(...)
|
||||
|
||||
.. _more_lookups:
|
||||
|
||||
More Lookups
|
||||
````````````
|
||||
|
||||
.. note:: This feature is very infrequently used in Ansible. You may wish to skip this section.
|
||||
|
||||
.. versionadded:: 0.8
|
||||
|
||||
Various *lookup plugins* allow additional ways to iterate over data. In `playbooks_loops` you will learn
|
||||
how to use them to walk over collections of numerous types. However, they can also be used to pull in data
|
||||
from remote sources, such as shell commands or even key value stores. This section will cover lookup
|
||||
plugins in this capacity.
|
||||
|
||||
Here are some examples::
|
||||
|
||||
---
|
||||
- hosts: all
|
||||
|
||||
tasks:
|
||||
|
||||
- debug: msg="{{ lookup('env','HOME') }} is an environment variable"
|
||||
|
||||
- debug: msg="{{ item }} is a line from the result of this command"
|
||||
with_lines:
|
||||
- cat /etc/motd
|
||||
|
||||
- debug: msg="{{ lookup('pipe','date') }} is the raw result of running this command"
|
||||
|
||||
- debug: msg="{{ lookup('redis_kv', 'redis://localhost:6379,somekey') }} is value in Redis for somekey"
|
||||
|
||||
- debug: msg="{{ lookup('dnstxt', 'example.com') }} is a DNS TXT record for example.com"
|
||||
|
||||
- debug: msg="{{ lookup('template', './some_template.j2') }} is a value from evaluation of this template"
|
||||
|
||||
As an alternative you can also assign lookup plugins to variables or use them
|
||||
elsewhere. This macros are evaluated each time they are used in a task (or
|
||||
template)::
|
||||
|
||||
vars:
|
||||
motd_value: "{{ lookup('file', '/etc/motd') }}"
|
||||
|
||||
tasks:
|
||||
- debug: msg="motd value is {{ motd_value }}"
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`playbooks`
|
||||
An introduction to playbooks
|
||||
:doc:`playbooks_conditionals`
|
||||
Conditional statements in playbooks
|
||||
:doc:`playbooks_variables`
|
||||
All about variables
|
||||
:doc:`playbooks_loops`
|
||||
Looping in playbooks
|
||||
`User Mailing List <http://groups.google.com/group/ansible-devel>`_
|
||||
Have a question? Stop by the google group!
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
||||
|
||||
|
344
docsite/rst/playbooks_loops.rst
Normal file
344
docsite/rst/playbooks_loops.rst
Normal file
|
@ -0,0 +1,344 @@
|
|||
Loops
|
||||
=====
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
Often you'll want to do many things in one task, such as create a lot of users, install a lot of packages, or
|
||||
repeat a polling step until a certain result is reached.
|
||||
|
||||
This chapter is all about how to use loops in playbooks.
|
||||
|
||||
.. _standard_loops:
|
||||
|
||||
Standard Loops
|
||||
``````````````
|
||||
|
||||
To save some typing, repeated tasks can be written in short-hand like so::
|
||||
|
||||
- name: add several users
|
||||
user: name={{ item }} state=present groups=wheel
|
||||
with_items:
|
||||
- testuser1
|
||||
- testuser2
|
||||
|
||||
If you have defined a YAML list in a variables file, or the 'vars' section, you can also do::
|
||||
|
||||
with_items: somelist
|
||||
|
||||
The above would be the equivalent of::
|
||||
|
||||
- name: add user testuser1
|
||||
user: name=testuser1 state=present groups=wheel
|
||||
- name: add user testuser2
|
||||
user: name=testuser2 state=present groups=wheel
|
||||
|
||||
The yum and apt modules use with_items to execute fewer package manager transactions.
|
||||
|
||||
Note that the types of items you iterate over with 'with_items' do not have to be simple lists of strings.
|
||||
If you have a list of hashes, you can reference subkeys using things like::
|
||||
|
||||
- name: add several users
|
||||
user: name={{ item.name }} state=present groups={{ item.groups }}
|
||||
with_items:
|
||||
- { name: 'testuser1', groups: 'wheel' }
|
||||
- { name: 'testuser2', groups: 'root' }
|
||||
|
||||
.. _nested_loops:
|
||||
|
||||
Nested Loops
|
||||
````````````
|
||||
|
||||
Loops can be nested as well::
|
||||
|
||||
- name: give users access to multiple databases
|
||||
mysql_user: name={{ item[0] }} priv={{ item[1] }}.*:ALL append_privs=yes password=foo
|
||||
with_nested:
|
||||
- [ 'alice', 'bob', 'eve' ]
|
||||
- [ 'clientdb', 'employeedb', 'providerdb' ]
|
||||
|
||||
As with the case of 'with_items' above, you can use previously defined variables. Just specify the variable's name without templating it with '{{ }}'::
|
||||
|
||||
- name: here, 'users' contains the above list of employees
|
||||
mysql_user: name={{ item[0] }} priv={{ item[1] }}.*:ALL append_privs=yes password=foo
|
||||
with_nested:
|
||||
- users
|
||||
- [ 'clientdb', 'employeedb', 'providerdb' ]
|
||||
|
||||
.. _looping_over_fileglobs:
|
||||
|
||||
Looping over Fileglobs
|
||||
``````````````````````
|
||||
|
||||
``with_fileglob`` matches all files in a single directory, non-recursively, that match a pattern. It can
|
||||
be used like this::
|
||||
|
||||
---
|
||||
- hosts: all
|
||||
|
||||
tasks:
|
||||
|
||||
# first ensure our target directory exists
|
||||
- file: dest=/etc/fooapp state=directory
|
||||
|
||||
# copy each file over that matches the given pattern
|
||||
- copy: src={{ item }} dest=/etc/fooapp/ owner=root mode=600
|
||||
with_fileglob:
|
||||
- /playbooks/files/fooapp/*
|
||||
|
||||
Looping over Parallel Sets of Data
|
||||
``````````````````````````````````
|
||||
|
||||
.. note:: This is an uncommon thing to want to do, but we're documenting it for completeness. You probably won't be reaching for this one often.
|
||||
|
||||
Suppose you have the following variable data was loaded in via somewhere::
|
||||
|
||||
---
|
||||
alpha: [ 'a', 'b', 'c', 'd' ]
|
||||
numbers: [ 1, 2, 3, 4 ]
|
||||
|
||||
And you want the set of '(a, 1)' and '(b, 2)' and so on. Use 'with_together' to get this::
|
||||
|
||||
tasks:
|
||||
- debug: msg="{{ item.0 }} and {{ item.1 }}"
|
||||
with_together:
|
||||
- alpha
|
||||
- numbers
|
||||
|
||||
Looping over Subelements
|
||||
````````````````````````
|
||||
|
||||
Suppose you want to do something like loop over a list of users, creating them, and allowing them to login by a certain set of
|
||||
SSH keys.
|
||||
|
||||
How might that be accomplished? Let's assume you had the following defined and loaded in via "vars_files" or maybe a "group_vars/all" file::
|
||||
|
||||
---
|
||||
users:
|
||||
- name: alice
|
||||
authorized:
|
||||
- /tmp/alice/onekey.pub
|
||||
- /tmp/alice/twokey.pub
|
||||
- name: bob
|
||||
authorized:
|
||||
- /tmp/bob/id_rsa.pub
|
||||
|
||||
It might happen like so::
|
||||
|
||||
- user: name={{ item.name }} state=present generate_ssh_key=yes
|
||||
with_items: users
|
||||
|
||||
- authorized_key: user={{ item.0.name }} key={{ lookup('file', item.1) }}
|
||||
with_subelements:
|
||||
- users
|
||||
- authorized
|
||||
|
||||
Subelements walks a list of hashes (aka dictionaries) and then traverses a list with a given key inside of those
|
||||
records.
|
||||
|
||||
The authorized_key pattern is exactly where it comes up most.
|
||||
|
||||
.. _looping_over_integer_sequences:
|
||||
|
||||
Looping over Integer Sequences
|
||||
``````````````````````````````
|
||||
|
||||
``with_sequence`` generates a sequence of items in ascending numerical order. You
|
||||
can specify a start, end, and an optional step value.
|
||||
|
||||
Arguments should be specified in key=value pairs. If supplied, the 'format' is a printf style string.
|
||||
|
||||
Numerical values can be specified in decimal, hexadecimal (0x3f8) or octal (0600).
|
||||
Negative numbers are not supported. This works as follows::
|
||||
|
||||
---
|
||||
- hosts: all
|
||||
|
||||
tasks:
|
||||
|
||||
# create groups
|
||||
- group: name=evens state=present
|
||||
- group: name=odds state=present
|
||||
|
||||
# create some test users
|
||||
- user: name={{ item }} state=present groups=evens
|
||||
with_sequence: start=0 end=32 format=testuser%02x
|
||||
|
||||
# create a series of directories with even numbers for some reason
|
||||
- file: dest=/var/stuff/{{ item }} state=directory
|
||||
with_sequence: start=4 end=16 stride=2
|
||||
|
||||
# a simpler way to use the sequence plugin
|
||||
# create 4 groups
|
||||
- group: name=group{{ item }} state=present
|
||||
with_sequence: count=4
|
||||
|
||||
.. _random_choice:
|
||||
|
||||
Random Choices
|
||||
``````````````
|
||||
|
||||
The 'random_choice' feature can be used to pick something at random. While it's not a load balancer (there are modules
|
||||
for those), it can somewhat be used as a poor man's loadbalancer in a MacGyver like situation::
|
||||
|
||||
- debug: msg={{ item }}
|
||||
with_random_choice:
|
||||
- "go through the door"
|
||||
- "drink from the goblet"
|
||||
- "press the red button"
|
||||
- "do nothing"
|
||||
|
||||
One of the provided strings will be selected at random.
|
||||
|
||||
At a more basic level, they can be used to add chaos and excitement to otherwise predictable automation environments.
|
||||
|
||||
.. _do_until_loops:
|
||||
|
||||
Do-Until Loops
|
||||
``````````````
|
||||
|
||||
.. versionadded: 1.4
|
||||
|
||||
Sometimes you would want to retry a task until a certain condition is met. Here's an example::
|
||||
|
||||
- action: shell /usr/bin/foo
|
||||
register: result
|
||||
until: result.stdout.find("all systems go") != -1
|
||||
retries: 5
|
||||
delay: 10
|
||||
|
||||
The above example run the shell module recursively till the module's result has "all systems go" in it's stdout or the task has
|
||||
been retried for 5 times with a delay of 10 seconds. The default value for "retries" is 3 and "delay" is 5.
|
||||
|
||||
The task returns the results returned by the last task run. The results of individual retries can be viewed by -vv option.
|
||||
The registered variable will also have a new key "attempts" which will have the number of the retries for the task.
|
||||
|
||||
.. _with_first_found:
|
||||
|
||||
Finding First Matched Files
|
||||
```````````````````````````
|
||||
|
||||
.. note:: This is an uncommon thing to want to do, but we're documenting it for completeness. You probably won't be reaching for this one often.
|
||||
|
||||
This isn't exactly a loop, but it's close. What if you want to use a reference to a file based on the first file found
|
||||
that matches a given criteria, and some of the filenames are determined by variable names? Yes, you can do that as follows::
|
||||
|
||||
- name: INTERFACES | Create Ansible header for /etc/network/interfaces
|
||||
template: src={{ item }} dest=/etc/foo.conf
|
||||
with_first_found:
|
||||
- "{{ansible_virtualization_type}_foo.conf"
|
||||
- "default_foo.conf"
|
||||
|
||||
This tool also has a long form version that allows for configurable search paths. Here's an example::
|
||||
|
||||
- name: some configuration template
|
||||
template: src={{ item }} dest=/etc/file.cfg mode=0444 owner=root group=root
|
||||
with_first_found:
|
||||
- files:
|
||||
- "{{inventory_hostname}}/etc/file.cfg"
|
||||
paths:
|
||||
- ../../../templates.overwrites
|
||||
- ../../../templates
|
||||
- files:
|
||||
- etc/file.cfg
|
||||
paths:
|
||||
- templates
|
||||
|
||||
.. _looping_over_the_results_of_a_program_execution:
|
||||
|
||||
Iterating Over The Results of a Program Execution
|
||||
`````````````````````````````````````````````````
|
||||
|
||||
.. note:: This is an uncommon thing to want to do, but we're documenting it for completeness. You probably won't be reaching for this one often.
|
||||
|
||||
Sometimes you might want to execute a program, and based on the output of that program, loop over the results of that line by line.
|
||||
Ansible provides a neat way to do that, though you should remember, this is always executed on the control machine, not the local
|
||||
machine::
|
||||
|
||||
- name: Example of looping over a command result
|
||||
shell: /usr/bin/frobnicate {{ item }}
|
||||
with_lines: /usr/bin/frobnications_per_host --param {{ inventory_hostname }}
|
||||
|
||||
Ok, that was a bit arbitrary. In fact, if you're doing something that is inventory related you might just want to write a dynamic
|
||||
inventory source instead (see :doc:`intro_dynamic_inventory`), but this can be occasionally useful in quick-and-dirty implementations.
|
||||
|
||||
Should you ever need to execute a command remotely, you would not use the above method. Instead do this::
|
||||
|
||||
- name: Example of looping over a REMOTE command result
|
||||
shell: /usr/bin/something
|
||||
register: command_result
|
||||
|
||||
- name: Do something with each result
|
||||
shell: /usr/bin/something_else --param {{ item }}
|
||||
with_items: command_result.stdout_lines
|
||||
|
||||
.. _indexed_lists:
|
||||
|
||||
Looping Over A List With An Index
|
||||
`````````````````````````````````
|
||||
|
||||
.. note:: This is an uncommon thing to want to do, but we're documenting it for completeness. You probably won't be reaching for this one often.
|
||||
|
||||
.. versionadded: 1.3
|
||||
|
||||
If you want to loop over an array and also get the numeric index of where you are in the array as you go, you can also do that.
|
||||
It's uncommonly used::
|
||||
|
||||
- name: indexed loop demo
|
||||
debug: msg="at array position {{ item.0 }} there is a value {{ item.1 }}"
|
||||
with_indexed_items: some_list
|
||||
|
||||
.. _flattening_a_list:
|
||||
|
||||
Flattening A List
|
||||
`````````````````
|
||||
|
||||
.. note:: This is an uncommon thing to want to do, but we're documenting it for completeness. You probably won't be reaching for this one often.
|
||||
|
||||
In rare instances you might have several lists of lists, and you just want to iterate over every item in all of those lists. Assume
|
||||
a really crazy hypothetical datastructure::
|
||||
|
||||
----
|
||||
# file: roles/foo/vars/main.yml
|
||||
packages_base:
|
||||
- [ 'foo-package', 'bar-package' ]
|
||||
packages_apps:
|
||||
- [ ['one-package', 'two-package' ]]
|
||||
- [ ['red-package'], ['blue-package']]
|
||||
|
||||
As you can see the formatting of packages in these lists is all over the place. How can we install all of the packages in both lists?::
|
||||
|
||||
- name: flattened loop demo
|
||||
yum: name={{ item }} state=installed
|
||||
with_flattened:
|
||||
- packages_base
|
||||
- packages_apps
|
||||
|
||||
That's how!
|
||||
|
||||
.. _writing_your_own_iterators:
|
||||
|
||||
Writing Your Own Iterators
|
||||
``````````````````````````
|
||||
|
||||
While you ordinarily shouldn't have to, should you wish to write your own ways to loop over arbitrary datastructures, you can read `developing_plugins` for some starter
|
||||
information. Each of the above features are implemented as plugins in ansible, so there are many implementations to reference.
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`playbooks`
|
||||
An introduction to playbooks
|
||||
:doc:`playbooks_roles`
|
||||
Playbook organization by roles
|
||||
:doc:`playbooks_best_practices`
|
||||
Best practices in playbooks
|
||||
:doc:`playbooks_conditionals`
|
||||
Conditional statements in playbooks
|
||||
:doc:`playbooks_variables`
|
||||
All about variables
|
||||
`User Mailing List <http://groups.google.com/group/ansible-devel>`_
|
||||
Have a question? Stop by the google group!
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
||||
|
97
docsite/rst/playbooks_prompts.rst
Normal file
97
docsite/rst/playbooks_prompts.rst
Normal file
|
@ -0,0 +1,97 @@
|
|||
Prompts
|
||||
=======
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
When running a playbook, you may wish to prompt the user for certain input, and can
|
||||
do so with the 'vars_prompt' section.
|
||||
|
||||
A common use for this might be for asking for sensitive data that you do not want to record.
|
||||
|
||||
This has uses beyond security, for instance, you may use the same playbook for all
|
||||
software releases and would prompt for a particular release version
|
||||
in a push-script.
|
||||
|
||||
Here is a most basic example::
|
||||
|
||||
---
|
||||
- hosts: all
|
||||
remote_user: root
|
||||
vars:
|
||||
from: "camelot"
|
||||
vars_prompt:
|
||||
name: "what is your name?"
|
||||
quest: "what is your quest?"
|
||||
favcolor: "what is your favorite color?"
|
||||
|
||||
If you have a variable that changes infrequently, it might make sense to
|
||||
provide a default value that can be overridden. This can be accomplished using
|
||||
the default argument::
|
||||
|
||||
vars_prompt:
|
||||
- name: "release_version"
|
||||
prompt: "Product release version"
|
||||
default: "1.0"
|
||||
|
||||
An alternative form of vars_prompt allows for hiding input from the user, and may later support
|
||||
some other options, but otherwise works equivalently::
|
||||
|
||||
vars_prompt:
|
||||
- name: "some_password"
|
||||
prompt: "Enter password"
|
||||
private: yes
|
||||
- name: "release_version"
|
||||
prompt: "Product release version"
|
||||
private: no
|
||||
|
||||
If `Passlib <http://pythonhosted.org/passlib/>`_ is installed, vars_prompt can also crypt the
|
||||
entered value so you can use it, for instance, with the user module to define a password::
|
||||
|
||||
vars_prompt:
|
||||
- name: "my_password2"
|
||||
prompt: "Enter password2"
|
||||
private: yes
|
||||
encrypt: "md5_crypt"
|
||||
confirm: yes
|
||||
salt_size: 7
|
||||
|
||||
You can use any crypt scheme supported by 'Passlib':
|
||||
|
||||
- *des_crypt* - DES Crypt
|
||||
- *bsdi_crypt* - BSDi Crypt
|
||||
- *bigcrypt* - BigCrypt
|
||||
- *crypt16* - Crypt16
|
||||
- *md5_crypt* - MD5 Crypt
|
||||
- *bcrypt* - BCrypt
|
||||
- *sha1_crypt* - SHA-1 Crypt
|
||||
- *sun_md5_crypt* - Sun MD5 Crypt
|
||||
- *sha256_crypt* - SHA-256 Crypt
|
||||
- *sha512_crypt* - SHA-512 Crypt
|
||||
- *apr_md5_crypt* - Apache’s MD5-Crypt variant
|
||||
- *phpass* - PHPass’ Portable Hash
|
||||
- *pbkdf2_digest* - Generic PBKDF2 Hashes
|
||||
- *cta_pbkdf2_sha1* - Cryptacular’s PBKDF2 hash
|
||||
- *dlitz_pbkdf2_sha1* - Dwayne Litzenberger’s PBKDF2 hash
|
||||
- *scram* - SCRAM Hash
|
||||
- *bsd_nthash* - FreeBSD’s MCF-compatible nthash encoding
|
||||
|
||||
However, the only parameters accepted are 'salt' or 'salt_size'. You can use you own salt using
|
||||
'salt', or have one generated automatically using 'salt_size'. If nothing is specified, a salt
|
||||
of size 8 will be generated.
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`playbooks`
|
||||
An introduction to playbooks
|
||||
:doc:`playbooks_conditionals`
|
||||
Conditional statements in playbooks
|
||||
:doc:`playbooks_variables`
|
||||
All about variables
|
||||
`User Mailing List <http://groups.google.com/group/ansible-devel>`_
|
||||
Have a question? Stop by the google group!
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
||||
|
||||
|
345
docsite/rst/playbooks_roles.rst
Normal file
345
docsite/rst/playbooks_roles.rst
Normal file
|
@ -0,0 +1,345 @@
|
|||
Playbook Roles and Include Statements
|
||||
=====================================
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
Introduction
|
||||
````````````
|
||||
|
||||
While it is possible to write a playbook in one very large file (and you might start out learning playbooks this way),
|
||||
eventually you'll want to reuse files and start to organize things.
|
||||
|
||||
At a basic level, including task files allows you to break up bits of configuration policy into smaller files. Task includes
|
||||
pull in tasks from other files. Since handlers are tasks too, you can also include handler files from the 'handlers:' section.
|
||||
|
||||
See :doc:`playbooks` if you need a review of these concepts.
|
||||
|
||||
Playbooks can also include plays from other playbook files. When that is done, the plays will be inserted into the playbook to form
|
||||
a longer list of plays.
|
||||
|
||||
When you start to think about it -- tasks, handlers, variables, and so -- begin to form larger concepts. You start to think about modeling
|
||||
what something is, rather than how to make something look like something. It's no longer "apply this handful of THINGS" to these hosts, you say "these hosts are a dbservers" or "these hosts are webservers". In programming, we might call that 'encapsulating' how things work. For instance,
|
||||
you can drive a car without knowing how the engine works.
|
||||
|
||||
Roles in Ansible build on the idea of include files and combine them to form clean, reusable abstractions -- they allow you to focus
|
||||
more on the big picture and only dive down into the details when needed.
|
||||
|
||||
We'll start with understanding includes so roles make more sense, but our ultimate goal should be understanding roles -- roles
|
||||
are great and you should use them every time you write playbooks.
|
||||
|
||||
See the 'ansible-examples' repository on github for lots of examples of all of this
|
||||
put together. You may wish to have this open in a separate tab as you dive in.
|
||||
|
||||
Task Include Files And Encouraging Reuse
|
||||
````````````````````````````````````````
|
||||
|
||||
Suppose you want to reuse lists of tasks between plays or playbooks. You can use
|
||||
include files to do this. Use of included task lists is a great way to define a role
|
||||
that system is going to fulfill. Remember, the goal of a play in a playbook is to map
|
||||
a group of systems into multiple roles. Let's see what this looks like...
|
||||
|
||||
A task include file simply contains a flat list of tasks, like so::
|
||||
|
||||
---
|
||||
# possibly saved as tasks/foo.yml
|
||||
- name: placeholder foo
|
||||
command: /bin/foo
|
||||
- name: placeholder bar
|
||||
command: /bin/bar
|
||||
|
||||
Include directives look like this, and can be mixed in with regular tasks in a playbook::
|
||||
|
||||
tasks:
|
||||
- include: tasks/foo.yml
|
||||
|
||||
You can also pass variables into includes. We call this a 'parameterized include'.
|
||||
|
||||
For instance, if deploying multiple wordpress instances, I could
|
||||
contain all of my wordpress tasks in a single wordpress.yml file, and use it like so::
|
||||
|
||||
tasks:
|
||||
- include: wordpress.yml user=timmy
|
||||
- include: wordpress.yml user=alice
|
||||
- include: wordpress.yml user=bob
|
||||
|
||||
If you are running Ansible 1.4 and later, include syntax is streamlined to match roles, and also allows passing list and dictionary parameters::
|
||||
|
||||
tasks:
|
||||
- { include: wordpress.yml, user: timmy, ssh_keys: [ 'keys/one.txt', 'keys/two.txt' ] }
|
||||
|
||||
Using either syntax, variables passed in can then be used in the included files. We've already covered them a bit in :doc:`playbooks_variables`.
|
||||
You can reference them like this::
|
||||
|
||||
{{ user }}
|
||||
|
||||
(In addition to the explicitly passed-in parameters, all variables from
|
||||
the vars section are also available for use here as well.)
|
||||
|
||||
Starting in 1.0, variables can also be passed to include files using an alternative syntax,
|
||||
which also supports structured variables::
|
||||
|
||||
tasks:
|
||||
|
||||
- include: wordpress.yml
|
||||
vars:
|
||||
remote_user: timmy
|
||||
some_list_variable:
|
||||
- alpha
|
||||
- beta
|
||||
- gamma
|
||||
|
||||
Playbooks can include other playbooks too, but that's mentioned in a later section.
|
||||
|
||||
.. note::
|
||||
As of 1.0, task include statements can be used at arbitrary depth.
|
||||
They were previously limited to a single level, so task includes
|
||||
could not include other files containing task includes.
|
||||
|
||||
Includes can also be used in the 'handlers' section, for instance, if you
|
||||
want to define how to restart apache, you only have to do that once for all
|
||||
of your playbooks. You might make a handlers.yml that looks like::
|
||||
|
||||
---
|
||||
# this might be in a file like handlers/handlers.yml
|
||||
- name: restart apache
|
||||
service: name=apache state=restarted
|
||||
|
||||
And in your main playbook file, just include it like so, at the bottom
|
||||
of a play::
|
||||
|
||||
handlers:
|
||||
- include: handlers/handlers.yml
|
||||
|
||||
You can mix in includes along with your regular non-included tasks and handlers.
|
||||
|
||||
Includes can also be used to import one playbook file into another. This allows
|
||||
you to define a top-level playbook that is composed of other playbooks.
|
||||
|
||||
For example::
|
||||
|
||||
- name: this is a play at the top level of a file
|
||||
hosts: all
|
||||
remote_user: root
|
||||
tasks:
|
||||
- name: say hi
|
||||
tags: foo
|
||||
shell: echo "hi..."
|
||||
|
||||
- include: load_balancers.yml
|
||||
- include: webservers.yml
|
||||
- include: dbservers.yml
|
||||
|
||||
Note that you cannot do variable substitution when including one playbook
|
||||
inside another.
|
||||
|
||||
.. note::
|
||||
You can not conditionally path the location to an include file,
|
||||
like you can with 'vars_files'. If you find yourself needing to do
|
||||
this, consider how you can restructure your playbook to be more
|
||||
class/role oriented. This is to say you cannot use a 'fact' to
|
||||
decide what include file to use. All hosts contained within the
|
||||
play are going to get the same tasks. ('*when*' provides some
|
||||
ability for hosts to conditionally skip tasks).
|
||||
|
||||
.. _roles:
|
||||
|
||||
Roles
|
||||
`````
|
||||
|
||||
.. versionadded:: 1.2
|
||||
|
||||
Now that you have learned about vars_files, tasks, and handlers, what is the best way to organize your playbooks?
|
||||
The short answer is to use roles! Roles are ways of automatically loading certain vars_files, tasks, and
|
||||
handlers based on a known file structure. Grouping content by roles also allows easy sharing of roles with other users.
|
||||
|
||||
Roles are just automation around 'include' directives as described above, and really don't contain much
|
||||
additional magic beyond some improvements to search path handling for referenced files. However, that can be a big thing!
|
||||
|
||||
Example project structure::
|
||||
|
||||
site.yml
|
||||
webservers.yml
|
||||
fooservers.yml
|
||||
roles/
|
||||
common/
|
||||
files/
|
||||
templates/
|
||||
tasks/
|
||||
handlers/
|
||||
vars/
|
||||
meta/
|
||||
webservers/
|
||||
files/
|
||||
templates/
|
||||
tasks/
|
||||
handlers/
|
||||
vars/
|
||||
meta/
|
||||
|
||||
In a playbook, it would look like this::
|
||||
|
||||
---
|
||||
- hosts: webservers
|
||||
roles:
|
||||
- common
|
||||
- webservers
|
||||
|
||||
This designates the following behaviors, for each role 'x':
|
||||
|
||||
- If roles/x/tasks/main.yml exists, tasks listed therein will be added to the play
|
||||
- If roles/x/handlers/main.yml exists, handlers listed therein will be added to the play
|
||||
- If roles/x/vars/main.yml exists, variables listed therein will be added to the play
|
||||
- If roles/x/meta/main.yml exists, any role dependencies listed therein will be added to the list of roles (1.3 and later)
|
||||
- Any copy tasks can reference files in roles/x/files/ without having to path them relatively or absolutely
|
||||
- Any script tasks can reference scripts in roles/x/files/ without having to path them relatively or absolutely
|
||||
- Any template tasks can reference files in roles/x/templates/ without having to path them relatively or absolutely
|
||||
|
||||
In Ansible 1.4 and later you can configure a roles_path to search for roles. Use this to check all of your common roles out to one location, and share
|
||||
them easily between multiple playbook projects. See :doc:`intro_configuration` for details about how to set this up in ansible.cfg.
|
||||
|
||||
.. note::
|
||||
Role dependencies are discussed below.
|
||||
|
||||
If any files are not present, they are just ignored. So it's ok to not have a 'vars/' subdirectory for the role,
|
||||
for instance.
|
||||
|
||||
Note, you are still allowed to list tasks, vars_files, and handlers "loose" in playbooks without using roles,
|
||||
but roles are a good organizational feature and are highly recommended. if there are loose things in the playbook,
|
||||
the roles are evaluated first.
|
||||
|
||||
Also, should you wish to parameterize roles, by adding variables, you can do so, like this::
|
||||
|
||||
---
|
||||
- hosts: webservers
|
||||
roles:
|
||||
- common
|
||||
- { role: foo_app_instance, dir: '/opt/a', port: 5000 }
|
||||
- { role: foo_app_instance, dir: '/opt/b', port: 5001 }
|
||||
|
||||
While it's probably not something you should do often, you can also conditionally apply roles like so::
|
||||
|
||||
---
|
||||
- hosts: webservers
|
||||
roles:
|
||||
- { role: some_role, when: "ansible_os_family == 'RedHat'" }
|
||||
|
||||
This works by applying the conditional to every task in the role. Conditionals are covered later on in
|
||||
the documentation.
|
||||
|
||||
Finally, you may wish to assign tags to the roles you specify. You can do so inline:::
|
||||
|
||||
---
|
||||
- hosts: webservers
|
||||
roles:
|
||||
- { role: foo, tags: ["bar", "baz"] }
|
||||
|
||||
|
||||
If the play still has a 'tasks' section, those tasks are executed after roles are applied.
|
||||
|
||||
If you want to define certain tasks to happen before AND after roles are applied, you can do this::
|
||||
|
||||
---
|
||||
- hosts: webservers
|
||||
pre_tasks:
|
||||
- shell: echo 'hello'
|
||||
roles:
|
||||
- { role: some_role }
|
||||
tasks:
|
||||
- shell: echo 'still busy'
|
||||
post_tasks:
|
||||
- shell: echo 'goodbye'
|
||||
|
||||
.. note::
|
||||
If using tags with tasks (described later as a means of only running part of a playbook),
|
||||
be sure to also tag your pre_tasks and post_tasks and pass those along as well, especially if the pre
|
||||
and post tasks are used for monitoring outage window control or load balancing.
|
||||
|
||||
Role Default Variables
|
||||
``````````````````````
|
||||
|
||||
.. versionadded:: 1.3
|
||||
|
||||
Role default variables allow you to set default variables for included or dependent roles (see below). To create
|
||||
defaults, simply add a `defaults/main.yml` file in your role directory. These variables will have the lowest priority
|
||||
of any variables available, and can be easily overridden by any other variable, including inventory variables.
|
||||
|
||||
Role Dependencies
|
||||
`````````````````
|
||||
|
||||
.. versionadded:: 1.3
|
||||
|
||||
Role dependencies allow you to automatically pull in other roles when using a role. Role dependencies are stored in the
|
||||
`meta/main.yml` file contained within the role directory. This file should contain
|
||||
a list of roles and parameters to insert before the specified role, such as the following in an example
|
||||
`roles/myapp/meta/main.yml`::
|
||||
|
||||
---
|
||||
dependencies:
|
||||
- { role: common, some_parameter: 3 }
|
||||
- { role: apache, port: 80 }
|
||||
- { role: postgres, dbname: blarg, other_parameter: 12 }
|
||||
|
||||
Role dependencies can also be specified as a full path, just like top level roles::
|
||||
|
||||
---
|
||||
dependencies:
|
||||
- { role: '/path/to/common/roles/foo', x: 1 }
|
||||
|
||||
Roles dependencies are always executed before the role that includes them, and are recursive. By default,
|
||||
roles can also only be added as a dependency once - if another role also lists it as a dependency it will
|
||||
not be run again. This behavior can be overridden by adding `allow_duplicates: yes` to the `meta/main.yml` file.
|
||||
For example, a role named 'car' could add a role named 'wheel' to its dependencies as follows::
|
||||
|
||||
---
|
||||
dependencies:
|
||||
- { role: wheel, n: 1 }
|
||||
- { role: wheel, n: 2 }
|
||||
- { role: wheel, n: 3 }
|
||||
- { role: wheel, n: 4 }
|
||||
|
||||
And the `meta/main.yml` for wheel contained the following::
|
||||
|
||||
---
|
||||
allow_duplicates: yes
|
||||
dependencies:
|
||||
- { role: tire }
|
||||
- { role: brake }
|
||||
|
||||
The resulting order of execution would be as follows::
|
||||
|
||||
tire(n=1)
|
||||
brake(n=1)
|
||||
wheel(n=1)
|
||||
tire(n=2)
|
||||
brake(n=2)
|
||||
wheel(n=2)
|
||||
...
|
||||
car
|
||||
|
||||
.. note::
|
||||
Variable inheritance and scope are detailed in the :doc:`playbooks_variables`.
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`YAMLSyntax`
|
||||
Learn about YAML syntax
|
||||
:doc:`playbooks`
|
||||
Review the basic Playbook language features
|
||||
:doc:`playbooks_best_practices`
|
||||
Various tips about managing playbooks in the real world
|
||||
:doc:`playbooks_variables`
|
||||
All about variables in playbooks
|
||||
:doc:`playbooks_conditionals`
|
||||
Conditionals in playbooks
|
||||
:doc:`playbooks_loops`
|
||||
Loops in playbooks
|
||||
:doc:`modules`
|
||||
Learn about available modules
|
||||
:doc:`developing_modules`
|
||||
Learn how to extend Ansible by writing your own modules
|
||||
`Github examples directory <https://github.com/ansible/ansible/tree/devel/examples/playbooks>`_
|
||||
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
|
||||
|
55
docsite/rst/playbooks_tags.rst
Normal file
55
docsite/rst/playbooks_tags.rst
Normal file
|
@ -0,0 +1,55 @@
|
|||
Tags
|
||||
====
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
If you have a large playbook it may become useful to be able to run a
|
||||
specific part of the configuration without running the whole playbook.
|
||||
|
||||
Both plays and tasks support a "tags:" attribute for this reason.
|
||||
|
||||
Example::
|
||||
|
||||
tasks:
|
||||
|
||||
- yum: name={{ item }} state=installed
|
||||
with_items:
|
||||
- httpd
|
||||
- memcached
|
||||
tags:
|
||||
- packages
|
||||
|
||||
- template: src=templates/src.j2 dest=/etc/foo.conf
|
||||
tags:
|
||||
- configuration
|
||||
|
||||
If you wanted to just run the "configuration" and "packages" part of a very long playbook, you could do this::
|
||||
|
||||
ansible-playbook example.yml --tags "configuration,packages"
|
||||
|
||||
You may also apply takes to roles::
|
||||
|
||||
roles:
|
||||
- { role: webserver, port: 5000, tags: [ 'web', 'foo' ] }
|
||||
|
||||
And you may also tag basic include statements::
|
||||
|
||||
- include: foo.yml tags=web,foo
|
||||
|
||||
Both of these have the function of tagging every single task inside the include statement.
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`playbooks`
|
||||
An introduction to playbooks
|
||||
:doc:`playbooks_roles`
|
||||
Playbook organization by roles
|
||||
`User Mailing List <http://groups.google.com/group/ansible-devel>`_
|
||||
Have a question? Stop by the google group!
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
||||
|
||||
|
||||
|
908
docsite/rst/playbooks_variables.rst
Normal file
908
docsite/rst/playbooks_variables.rst
Normal file
|
@ -0,0 +1,908 @@
|
|||
Variables
|
||||
=========
|
||||
|
||||
While automation exists to make it easier to make things repeatable, all of your systems are likely not exactly alike.
|
||||
|
||||
All of your systems are likely not the same. On some systems you may want to set some behavior
|
||||
or configuration that is slightly different from others.
|
||||
|
||||
Also, some of the observed behavior or state
|
||||
of remote systems might need to influence how you configure those systems. (Such as you might need to find out the IP
|
||||
address of a system and even use it as a configuration value on another system).
|
||||
|
||||
You might have some templates for configuration files that are mostly the same, but slightly different
|
||||
based on those variables.
|
||||
|
||||
Variables in Ansible are how we deal with differences between systems.
|
||||
|
||||
Once understanding variables you'll also want to dig into `playbooks_conditionals` and `playbooks_loops`.
|
||||
Useful things like the "group_by" module
|
||||
and the "when" conditional can also be used with variables, and to help manage differences between systems.
|
||||
|
||||
It's highly recommended that you consult the ansible-examples github repository to see a lot of examples of variables put to use.
|
||||
|
||||
.. contents::
|
||||
:depth: 2
|
||||
|
||||
.. _valid_variable_names:
|
||||
|
||||
What Makes A Valid Variable Name
|
||||
````````````````````````````````
|
||||
|
||||
Before we start using variables it's important to know what are valid variable names.
|
||||
|
||||
Variable names should be letters, numbers, and underscores. Variables should always start with a letter.
|
||||
|
||||
"foo_port" is a great variable. "foo5" is fine too.
|
||||
|
||||
"foo-port", "foo port", "foo.port" and "12" are not valid variable names.
|
||||
|
||||
Easy enough, let's move on.
|
||||
|
||||
.. _variables_in_inventory:
|
||||
|
||||
Variables Defined in Inventory
|
||||
``````````````````````````````
|
||||
|
||||
We've actually already covered a lot about variables in another section, so so far this shouldn't be terribly new, but
|
||||
a bit of a refresher.
|
||||
|
||||
Often you'll want to set variables based on what groups a machine is in. For instance, maybe machines in Boston
|
||||
want to use 'boston.ntp.example.com' as an NTP server.
|
||||
|
||||
See the `intro_inventory` document for multiple ways on how to define variables in inventory.
|
||||
|
||||
.. _playbook_variables:
|
||||
|
||||
Variables Defined in a Playbook
|
||||
```````````````````````````````
|
||||
|
||||
In a playbook, it's possible to define variables directly inline like so::
|
||||
|
||||
- hosts: webservers
|
||||
vars:
|
||||
http_port: 80
|
||||
|
||||
This can be nice as it's right there when you are reading the playbook.
|
||||
|
||||
.. _included_variables:
|
||||
|
||||
Variables defined from included files and roles
|
||||
-----------------------------------------------
|
||||
|
||||
It turns out we've already talked about variables in another place too.
|
||||
|
||||
As described in `intro_roles`, variables can also be included in the playbook via include files, which may or may
|
||||
not be part of an "Ansible Role". Usage of roles is preferred as it provides a nice organizational system.
|
||||
|
||||
.. _about_jinja2:
|
||||
|
||||
Using Variables: About Jinja2
|
||||
`````````````````````````````
|
||||
|
||||
It's nice enough to know about how to define variables, but how do you use them?
|
||||
|
||||
Ansible allows you to
|
||||
reference variables in your playbooks using the Jinja2 templating system. While you can do a lot of complex
|
||||
things in Jinja, only the basics are things you really need to learn at first.
|
||||
|
||||
For instance, in a simple template, you can do something like
|
||||
|
||||
My amp goes to {{ max_amp_value }}
|
||||
|
||||
And that will provide the most basic form of variable substitution.
|
||||
|
||||
This is also valid directly in playbooks, and you'll occasionally want to do things like:
|
||||
|
||||
template: src=foo.cfg.j2 dest={{ remote_install_path}}/foo.cfg
|
||||
|
||||
In the above example, we used a variable to help decide where to place a file.
|
||||
|
||||
Inside a template you automatically have access to all of the variables that are in scope for a host. Actually
|
||||
it's more than that -- you can also read variables about other hosts. We'll show how to do that in a bit.
|
||||
|
||||
.. note:: ansible allows Jinja2 loops and conditionals in templates, but in playbooks, we do not use them. Ansible
|
||||
templates are pure machine-parseable YAML. This is an rather important feature as it means it is possible to code-generate
|
||||
pieces of files, or to have other ecosystem tools read Ansible files. Not everyone will need this but it can unlock
|
||||
possibilities.
|
||||
|
||||
.. _jinja2_filters:
|
||||
|
||||
Jinja2 Filters
|
||||
``````````````
|
||||
|
||||
.. note: These are infrequently utilized features. Use them if they fit a use case you have, but this is optional knowledge.
|
||||
|
||||
Filters in Jinja2 are a way of transforming template expressions from one kind of data into another. Jinja2
|
||||
ships with many of these as documented on the official Jinja2 template documentation.
|
||||
|
||||
In addition to these, Ansible supplies many more.
|
||||
|
||||
.. _filters_for_formatting_data:
|
||||
|
||||
Filters For Formatting Data
|
||||
---------------------------
|
||||
|
||||
The following filters will take a data structure in a template and render it in a slightly different format. These
|
||||
are occasionally useful for debugging::
|
||||
|
||||
{{ some_variable | to_nice_json }}
|
||||
{{ some_variable | to_nice_yaml }}
|
||||
|
||||
.. _filters_used_with_conditionals:
|
||||
|
||||
Filters Often Used With Conditionals
|
||||
------------------------------------
|
||||
|
||||
The following tasks are illustrative of how filters can be used with conditionals::
|
||||
|
||||
tasks:
|
||||
- shell: /usr/bin/foo
|
||||
register: result
|
||||
ignore_errors: True
|
||||
|
||||
- debug: msg="it failed"
|
||||
when: result|failed
|
||||
|
||||
# in most cases you'll want a handler, but if you want to do something right now, this is nice
|
||||
- debug: msg="it changed"
|
||||
when: result|changed
|
||||
|
||||
- debug: msg="it succeeded"
|
||||
when: result|success
|
||||
|
||||
- debug: msg="it was skipped"
|
||||
when: result|skipped
|
||||
|
||||
.. _forcing_variables_to_be_defined:
|
||||
|
||||
Forcing Variables To Be Defined
|
||||
-------------------------------
|
||||
|
||||
The default behavior from ansible and ansible.cfg is to fail if variables are undefined, but you can turn this off.
|
||||
|
||||
This allows an explicit check with this feature off::
|
||||
|
||||
{{ variable | mandatory }}
|
||||
|
||||
The variable value will be used as is, but the template evaluation will raise an error if it is undefined.
|
||||
|
||||
.. _other_useful_filters:
|
||||
|
||||
Other Useful Filters
|
||||
--------------------
|
||||
|
||||
To get the last name of a file path, like 'foo.txt' out of '/etc/asdf/foo.txt'::
|
||||
|
||||
{{ path | basename }}
|
||||
|
||||
To get the directory from a path::
|
||||
|
||||
{{ path | dirname }}
|
||||
|
||||
To work with Base64 encoded strings::
|
||||
|
||||
{{ encoded | b64decode }}
|
||||
{{ decoded | b64encode }}
|
||||
|
||||
To take an md5sum of a filename::
|
||||
|
||||
{{ filename | md5 }}
|
||||
|
||||
To cast values as certain types, such as when you input a string as "True" from a vars_prompt and the system
|
||||
doesn't know it is a boolean value::
|
||||
|
||||
- debug: msg=test
|
||||
when: some_string_value | bool
|
||||
|
||||
A few useful filters are typically added with each new Ansible release. The development documentation shows
|
||||
how to extend Ansible filters by writing your own as plugins, though in general, we encourage new ones
|
||||
to be added to core so everyone can make use of them.
|
||||
|
||||
.. _yaml_gotchas:
|
||||
|
||||
Hey Wait, A YAML Gotcha
|
||||
```````````````````````
|
||||
|
||||
YAML syntax requires that if you start a value with {{ foo }} you quote the whole line, since it wants to be
|
||||
sure you aren't trying to start a YAML dictionary. This is covered on the `YAMLSyntax` page.
|
||||
|
||||
This won't work::
|
||||
|
||||
- hosts: app_servers
|
||||
vars:
|
||||
app_path: {{ base_path }}/22
|
||||
|
||||
Do it like this and you'll be fine::
|
||||
|
||||
- hosts: app_servers
|
||||
vars:
|
||||
app_path: "{{ base_path }}/22"
|
||||
|
||||
.. _vars_and_facts:
|
||||
|
||||
Information discovered from systems: Facts
|
||||
``````````````````````````````````````````
|
||||
|
||||
There are other places where variables can come from, but these are a type of variable that are discovered, not set by the user.
|
||||
|
||||
Facts are information derived from speaking with your remote systems.
|
||||
|
||||
An example of this might be the ip address of the remote host, or what the operating system is.
|
||||
|
||||
To see what information is available, try the following::
|
||||
|
||||
ansible hostname -m setup
|
||||
|
||||
This will return a ginormous amount of variable data, which may look like this, as taken from Ansible 1.4 on a Ubuntu 12.04 system::
|
||||
|
||||
"ansible_all_ipv4_addresses": [
|
||||
"REDACTED IP ADDRESS"
|
||||
],
|
||||
"ansible_all_ipv6_addresses": [
|
||||
"REDACTED IPV6 ADDRESS"
|
||||
],
|
||||
"ansible_architecture": "x86_64",
|
||||
"ansible_bios_date": "09/20/2012",
|
||||
"ansible_bios_version": "6.00",
|
||||
"ansible_cmdline": {
|
||||
"BOOT_IMAGE": "/boot/vmlinuz-3.5.0-23-generic",
|
||||
"quiet": true,
|
||||
"ro": true,
|
||||
"root": "UUID=4195bff4-e157-4e41-8701-e93f0aec9e22",
|
||||
"splash": true
|
||||
},
|
||||
"ansible_date_time": {
|
||||
"date": "2013-10-02",
|
||||
"day": "02",
|
||||
"epoch": "1380756810",
|
||||
"hour": "19",
|
||||
"iso8601": "2013-10-02T23:33:30Z",
|
||||
"iso8601_micro": "2013-10-02T23:33:30.036070Z",
|
||||
"minute": "33",
|
||||
"month": "10",
|
||||
"second": "30",
|
||||
"time": "19:33:30",
|
||||
"tz": "EDT",
|
||||
"year": "2013"
|
||||
},
|
||||
"ansible_default_ipv4": {
|
||||
"address": "REDACTED",
|
||||
"alias": "eth0",
|
||||
"gateway": "REDACTED",
|
||||
"interface": "eth0",
|
||||
"macaddress": "REDACTED",
|
||||
"mtu": 1500,
|
||||
"netmask": "255.255.255.0",
|
||||
"network": "REDACTED",
|
||||
"type": "ether"
|
||||
},
|
||||
"ansible_default_ipv6": {},
|
||||
"ansible_devices": {
|
||||
"fd0": {
|
||||
"holders": [],
|
||||
"host": "",
|
||||
"model": null,
|
||||
"partitions": {},
|
||||
"removable": "1",
|
||||
"rotational": "1",
|
||||
"scheduler_mode": "deadline",
|
||||
"sectors": "0",
|
||||
"sectorsize": "512",
|
||||
"size": "0.00 Bytes",
|
||||
"support_discard": "0",
|
||||
"vendor": null
|
||||
},
|
||||
"sda": {
|
||||
"holders": [],
|
||||
"host": "SCSI storage controller: LSI Logic / Symbios Logic 53c1030 PCI-X Fusion-MPT Dual Ultra320 SCSI (rev 01)",
|
||||
"model": "VMware Virtual S",
|
||||
"partitions": {
|
||||
"sda1": {
|
||||
"sectors": "39843840",
|
||||
"sectorsize": 512,
|
||||
"size": "19.00 GB",
|
||||
"start": "2048"
|
||||
},
|
||||
"sda2": {
|
||||
"sectors": "2",
|
||||
"sectorsize": 512,
|
||||
"size": "1.00 KB",
|
||||
"start": "39847934"
|
||||
},
|
||||
"sda5": {
|
||||
"sectors": "2093056",
|
||||
"sectorsize": 512,
|
||||
"size": "1022.00 MB",
|
||||
"start": "39847936"
|
||||
}
|
||||
},
|
||||
"removable": "0",
|
||||
"rotational": "1",
|
||||
"scheduler_mode": "deadline",
|
||||
"sectors": "41943040",
|
||||
"sectorsize": "512",
|
||||
"size": "20.00 GB",
|
||||
"support_discard": "0",
|
||||
"vendor": "VMware,"
|
||||
},
|
||||
"sr0": {
|
||||
"holders": [],
|
||||
"host": "IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)",
|
||||
"model": "VMware IDE CDR10",
|
||||
"partitions": {},
|
||||
"removable": "1",
|
||||
"rotational": "1",
|
||||
"scheduler_mode": "deadline",
|
||||
"sectors": "2097151",
|
||||
"sectorsize": "512",
|
||||
"size": "1024.00 MB",
|
||||
"support_discard": "0",
|
||||
"vendor": "NECVMWar"
|
||||
}
|
||||
},
|
||||
"ansible_distribution": "Ubuntu",
|
||||
"ansible_distribution_release": "precise",
|
||||
"ansible_distribution_version": "12.04",
|
||||
"ansible_domain": "",
|
||||
"ansible_env": {
|
||||
"COLORTERM": "gnome-terminal",
|
||||
"DISPLAY": ":0",
|
||||
"HOME": "/home/mdehaan",
|
||||
"LANG": "C",
|
||||
"LESSCLOSE": "/usr/bin/lesspipe %s %s",
|
||||
"LESSOPEN": "| /usr/bin/lesspipe %s",
|
||||
"LOGNAME": "root",
|
||||
"LS_COLORS": "rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:",
|
||||
"MAIL": "/var/mail/root",
|
||||
"OLDPWD": "/root/ansible/docsite",
|
||||
"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
|
||||
"PWD": "/root/ansible",
|
||||
"SHELL": "/bin/bash",
|
||||
"SHLVL": "1",
|
||||
"SUDO_COMMAND": "/bin/bash",
|
||||
"SUDO_GID": "1000",
|
||||
"SUDO_UID": "1000",
|
||||
"SUDO_USER": "mdehaan",
|
||||
"TERM": "xterm",
|
||||
"USER": "root",
|
||||
"USERNAME": "root",
|
||||
"XAUTHORITY": "/home/mdehaan/.Xauthority",
|
||||
"_": "/usr/local/bin/ansible"
|
||||
},
|
||||
"ansible_eth0": {
|
||||
"active": true,
|
||||
"device": "eth0",
|
||||
"ipv4": {
|
||||
"address": "REDACTED",
|
||||
"netmask": "255.255.255.0",
|
||||
"network": "REDACTED"
|
||||
},
|
||||
"ipv6": [
|
||||
{
|
||||
"address": "REDACTED",
|
||||
"prefix": "64",
|
||||
"scope": "link"
|
||||
}
|
||||
],
|
||||
"macaddress": "REDACTED",
|
||||
"module": "e1000",
|
||||
"mtu": 1500,
|
||||
"type": "ether"
|
||||
},
|
||||
"ansible_form_factor": "Other",
|
||||
"ansible_fqdn": "ubuntu2",
|
||||
"ansible_hostname": "ubuntu2",
|
||||
"ansible_interfaces": [
|
||||
"lo",
|
||||
"eth0"
|
||||
],
|
||||
"ansible_kernel": "3.5.0-23-generic",
|
||||
"ansible_lo": {
|
||||
"active": true,
|
||||
"device": "lo",
|
||||
"ipv4": {
|
||||
"address": "127.0.0.1",
|
||||
"netmask": "255.0.0.0",
|
||||
"network": "127.0.0.0"
|
||||
},
|
||||
"ipv6": [
|
||||
{
|
||||
"address": "::1",
|
||||
"prefix": "128",
|
||||
"scope": "host"
|
||||
}
|
||||
],
|
||||
"mtu": 16436,
|
||||
"type": "loopback"
|
||||
},
|
||||
"ansible_lsb": {
|
||||
"codename": "precise",
|
||||
"description": "Ubuntu 12.04.2 LTS",
|
||||
"id": "Ubuntu",
|
||||
"major_release": "12",
|
||||
"release": "12.04"
|
||||
},
|
||||
"ansible_machine": "x86_64",
|
||||
"ansible_memfree_mb": 74,
|
||||
"ansible_memtotal_mb": 991,
|
||||
"ansible_mounts": [
|
||||
{
|
||||
"device": "/dev/sda1",
|
||||
"fstype": "ext4",
|
||||
"mount": "/",
|
||||
"options": "rw,errors=remount-ro",
|
||||
"size_available": 15032406016,
|
||||
"size_total": 20079898624
|
||||
}
|
||||
],
|
||||
"ansible_os_family": "Debian",
|
||||
"ansible_pkg_mgr": "apt",
|
||||
"ansible_processor": [
|
||||
"Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz"
|
||||
],
|
||||
"ansible_processor_cores": 1,
|
||||
"ansible_processor_count": 1,
|
||||
"ansible_processor_threads_per_core": 1,
|
||||
"ansible_processor_vcpus": 1,
|
||||
"ansible_product_name": "VMware Virtual Platform",
|
||||
"ansible_product_serial": "REDACTED",
|
||||
"ansible_product_uuid": "REDACTED",
|
||||
"ansible_product_version": "None",
|
||||
"ansible_python_version": "2.7.3",
|
||||
"ansible_selinux": false,
|
||||
"ansible_ssh_host_key_dsa_public": "REDACTED KEY VALUE"
|
||||
"ansible_ssh_host_key_ecdsa_public": "REDACTED KEY VALUE"
|
||||
"ansible_ssh_host_key_rsa_public": "REDACTED KEY VALUE"
|
||||
"ansible_swapfree_mb": 665,
|
||||
"ansible_swaptotal_mb": 1021,
|
||||
"ansible_system": "Linux",
|
||||
"ansible_system_vendor": "VMware, Inc.",
|
||||
"ansible_user_id": "root",
|
||||
"ansible_userspace_architecture": "x86_64",
|
||||
"ansible_userspace_bits": "64",
|
||||
"ansible_virtualization_role": "guest",
|
||||
"ansible_virtualization_type": "VMware"
|
||||
|
||||
In the above the model of the first harddrive may be referenced in a template or playbook as::
|
||||
|
||||
{{ ansible_devices.sda.model }}
|
||||
|
||||
Similarly, the hostname as the system reports it is::
|
||||
|
||||
{{ ansible_hostname }}
|
||||
|
||||
|
||||
Facts are frequently used in conditionals (see `playbook_conditionals`) and also in templates.
|
||||
|
||||
Facts can be also used to create dynamic groups of hosts that match particular criteria, see the :doc:`modules` documentation on 'group_by' for details, as well as in generalized conditional statements as discussed in the `playbook_conditionals` chapter.
|
||||
|
||||
.. _disabling_facts:
|
||||
|
||||
Turning Off Facts
|
||||
`````````````````
|
||||
|
||||
If you know you don't need any fact data about your hosts, and know everything about your systems centrally, you
|
||||
can turn off fact gathering. This has advantages in scaling Ansible in push mode with very large numbers of
|
||||
systems, mainly, or if you are using Ansible on experimental platforms. In any play, just do this::
|
||||
|
||||
- hosts: whatever
|
||||
gather_facts: no
|
||||
|
||||
.. _local_facts:
|
||||
|
||||
Local Facts (Facts.d)
|
||||
`````````````````````
|
||||
|
||||
.. versionadded:: 1.3
|
||||
|
||||
As discussed in the playbooks chapter, Ansible facts are a way of getting data about remote systems for use in playbook variables.
|
||||
Usually these are discovered automatically by the 'setup' module in Ansible. Users can also write custom facts modules, as described
|
||||
in the API guide. However, what if you want to have a simple way to provide system or user
|
||||
provided data for use in Ansible variables, without writing a fact module?
|
||||
|
||||
For instance, what if you want users to be able to control some aspect about how their systems are managed? "Facts.d" is one such mechanism.
|
||||
|
||||
.. note:: Perhaps "local facts" is a bit of a misnomer, it means "locally supplied user values" as opposed to "centrally supplied user values", or what facts are -- "locally dynamically determined values".
|
||||
|
||||
If a remotely managed system has an "/etc/ansible/facts.d" directory, any files in this directory
|
||||
ending in ".fact", can be JSON, INI, or executable files returning JSON, and these can supply local facts in Ansible.
|
||||
|
||||
For instance assume a /etc/ansible/facts.d/preferences.fact::
|
||||
|
||||
[general]
|
||||
asdf=1
|
||||
bar=2
|
||||
|
||||
This will produce a hash variable fact named "general" with 'asdf' and 'bar' as members.
|
||||
To validate this, run the following::
|
||||
|
||||
ansible <hostname> -m setup -a "filter=ansible_local"
|
||||
|
||||
And you will see the following fact added::
|
||||
|
||||
"ansible_local": {
|
||||
"preferences": {
|
||||
"general": {
|
||||
"asdf" : "1",
|
||||
"bar" : "2"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
And this data can be accessed in a template/playbook as::
|
||||
|
||||
{{ ansible_local.preferences.general.asdf }}
|
||||
|
||||
The local namespace prevents any user supplied fact from overriding system facts
|
||||
or variables defined elsewhere in the playbook.
|
||||
|
||||
.. _registered_variables:
|
||||
|
||||
Registered Variables
|
||||
````````````````````
|
||||
|
||||
Another major use of variables is running a command and using the result of that command to save the result into a variable.
|
||||
|
||||
The value of a task being executed in ansible can be saved in a variable and used later. See some examples of this in the
|
||||
`playbooks_conditionals` chapter.
|
||||
|
||||
While it's mentioned elsewhere in that document too, here's a quick syntax example::
|
||||
|
||||
- hosts: web_servers
|
||||
|
||||
tasks:
|
||||
|
||||
- shell: /usr/bin/foo
|
||||
register: foo_result
|
||||
ignore_errors: True
|
||||
|
||||
- shell: /usr/bin/bar
|
||||
when: foo_result.rc == 5
|
||||
|
||||
Registered variables are valid on the host the remainder of the playbook run, which is the same as the lifetime of "facts"
|
||||
in Ansible. Effectively registered variables are just like facts.
|
||||
|
||||
.. _accessing_complex_variable_data:
|
||||
|
||||
Accessing Complex Variable Data
|
||||
```````````````````````````````
|
||||
|
||||
We already talked about facts a little higher up in the documentation.
|
||||
|
||||
Some provided facts, like networking information, are made available as nested data structures. To access
|
||||
them a simple {{ foo }} is not sufficient, but it is still easy to do. Here's how we get an IP address::
|
||||
|
||||
{{ ansible_eth0["ipv4"]["address"] }}
|
||||
|
||||
OR alternatively::
|
||||
|
||||
{{ ansible_eth0.ipv4.address }}
|
||||
|
||||
Similarly, this is how we access the first element of an array::
|
||||
|
||||
{{ foo[0] }}
|
||||
|
||||
.. _magic_variables_and_hostvars:
|
||||
|
||||
Magic Variables, and How To Access Information About Other Hosts
|
||||
````````````````````````````````````````````````````````````````
|
||||
|
||||
Even if you didn't define them yourself, Ansible provides a few variables for you automatically.
|
||||
The most important of these are 'hostvars', 'group_names', and 'groups'. Users should not use
|
||||
these names themselves as they are reserved. 'environment' is also reserved.
|
||||
|
||||
Hostvars lets you ask about the variables of another host, including facts that have been gathered
|
||||
about that host. If, at this point, you haven't talked to that host yet in any play in the playbook
|
||||
or set of playbooks, you can get at the variables, but you will not be able to see the facts.
|
||||
|
||||
If your database server wants to use the value of a 'fact' from another node, or an inventory variable
|
||||
assigned to another node, it's easy to do so within a template or even an action line::
|
||||
|
||||
{{ hostvars['test.example.com']['ansible_distribution'] }}
|
||||
|
||||
Additionally, *group_names* is a list (array) of all the groups the current host is in. This can be used in templates using Jinja2 syntax to make template source files that vary based on the group membership (or role) of the host::
|
||||
|
||||
{% if 'webserver' in group_names %}
|
||||
# some part of a configuration file that only applies to webservers
|
||||
{% endif %}
|
||||
|
||||
*groups* is a list of all the groups (and hosts) in the inventory. This can be used to enumerate all hosts within a group.
|
||||
For example::
|
||||
|
||||
{% for host in groups['app_servers'] %}
|
||||
# something that applies to all app servers.
|
||||
{% endfor %}
|
||||
|
||||
A frequently used idiom is walking a group to find all IP addresses in that group::
|
||||
|
||||
{% for host in groups['app_servers'] %}
|
||||
{{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}
|
||||
{% endfor %}
|
||||
|
||||
An example of this could include pointing a frontend proxy server to all of the app servers, setting up the correct firewall rules between servers, etc.
|
||||
|
||||
Just a few other 'magic' variables are available... There aren't many.
|
||||
|
||||
Additionally, *inventory_hostname* is the name of the hostname as configured in Ansible's inventory host file. This can
|
||||
be useful for when you don't want to rely on the discovered hostname `ansible_hostname` or for other mysterious
|
||||
reasons. If you have a long FQDN, *inventory_hostname_short* also contains the part up to the first
|
||||
period, without the rest of the domain.
|
||||
|
||||
Don't worry about any of this unless you think you need it. You'll know when you do.
|
||||
|
||||
Also available, *inventory_dir* is the pathname of the directory holding Ansible's inventory host file, *inventory_file* is the pathname and the filename pointing to the Ansible's inventory host file.
|
||||
|
||||
.. _variable_file_seperation_details:
|
||||
|
||||
Variable File Separation
|
||||
````````````````````````
|
||||
|
||||
It's a great idea to keep your playbooks under source control, but
|
||||
you may wish to make the playbook source public while keeping certain
|
||||
important variables private. Similarly, sometimes you may just
|
||||
want to keep certain information in different files, away from
|
||||
the main playbook.
|
||||
|
||||
You can do this by using an external variables file, or files, just like this::
|
||||
|
||||
---
|
||||
- hosts: all
|
||||
remote_user: root
|
||||
vars:
|
||||
favcolor: blue
|
||||
vars_files:
|
||||
- /vars/external_vars.yml
|
||||
tasks:
|
||||
- name: this is just a placeholder
|
||||
command: /bin/echo foo
|
||||
|
||||
This removes the risk of sharing sensitive data with others when
|
||||
sharing your playbook source with them.
|
||||
|
||||
The contents of each variables file is a simple YAML dictionary, like this::
|
||||
|
||||
---
|
||||
# in the above example, this would be vars/external_vars.yml
|
||||
somevar: somevalue
|
||||
password: magic
|
||||
|
||||
.. note::
|
||||
It's also possible to keep per-host and per-group variables in very
|
||||
similar files, this is covered in :doc:`intro_patterns`.
|
||||
|
||||
.. _passing_variables_on_the_command_line:
|
||||
|
||||
Passing Variables On The Command Line
|
||||
`````````````````````````````````````
|
||||
|
||||
In addition to `vars_prompt` and `vars_files`, it is possible to send variables over
|
||||
the Ansible command line. This is particularly useful when writing a generic release playbook
|
||||
where you may want to pass in the version of the application to deploy::
|
||||
|
||||
ansible-playbook release.yml --extra-vars "version=1.23.45 other_variable=foo"
|
||||
|
||||
This is useful, for, among other things, setting the hosts group or the user for the playbook.
|
||||
|
||||
Example::
|
||||
|
||||
---
|
||||
- remote_user: '{{ user }}'
|
||||
hosts: '{{ hosts }}'
|
||||
tasks:
|
||||
- ...
|
||||
|
||||
ansible-playbook release.yml --extra-vars "hosts=vipers user=starbuck"
|
||||
|
||||
As of Ansible 1.2, you can also pass in extra vars as quoted JSON, like so::
|
||||
|
||||
--extra-vars '{"pacman":"mrs","ghosts":["inky","pinky","clyde","sue"]}'
|
||||
|
||||
The key=value form is obviously simpler, but it's there if you need it!
|
||||
|
||||
As of Ansible 1.3, extra vars can be loaded from a JSON file with the "@" syntax::
|
||||
|
||||
--extra-vars "@some_file.json"
|
||||
|
||||
Also as of Ansible 1.3, extra vars can be formatted as YAML, either on the command line
|
||||
or in a file as above.
|
||||
|
||||
.. _conditional_imports:
|
||||
|
||||
Conditional Imports
|
||||
```````````````````
|
||||
|
||||
.. note: this behavior is infrequently used in Ansible. You may wish to skip this section. The 'group_by' module as described in the module documentation is a better way to achieve this behavior in most cases.
|
||||
|
||||
Sometimes you will want to do certain things differently in a playbook based on certain criteria.
|
||||
Having one playbook that works on multiple platforms and OS versions is a good example.
|
||||
|
||||
As an example, the name of the Apache package may be different between CentOS and Debian,
|
||||
but it is easily handled with a minimum of syntax in an Ansible Playbook::
|
||||
|
||||
---
|
||||
- hosts: all
|
||||
remote_user: root
|
||||
vars_files:
|
||||
- "vars/common.yml"
|
||||
- [ "vars/{{ ansible_os_family }}.yml", "vars/os_defaults.yml" ]
|
||||
tasks:
|
||||
- name: make sure apache is running
|
||||
service: name={{ apache }} state=running
|
||||
|
||||
.. note::
|
||||
The variable 'ansible_os_family' is being interpolated into
|
||||
the list of filenames being defined for vars_files.
|
||||
|
||||
As a reminder, the various YAML files contain just keys and values::
|
||||
|
||||
---
|
||||
# for vars/CentOS.yml
|
||||
apache: httpd
|
||||
somethingelse: 42
|
||||
|
||||
How does this work? If the operating system was 'CentOS', the first file Ansible would try to import
|
||||
would be 'vars/CentOS.yml', followed by '/vars/os_defaults.yml' if that file
|
||||
did not exist. If no files in the list were found, an error would be raised.
|
||||
On Debian, it would instead first look towards 'vars/Debian.yml' instead of 'vars/CentOS.yml', before
|
||||
falling back on 'vars/os_defaults.yml'. Pretty simple.
|
||||
|
||||
To use this conditional import feature, you'll need facter or ohai installed prior to running the playbook, but
|
||||
you can of course push this out with Ansible if you like::
|
||||
|
||||
# for facter
|
||||
ansible -m yum -a "pkg=facter ensure=installed"
|
||||
ansible -m yum -a "pkg=ruby-json ensure=installed"
|
||||
|
||||
# for ohai
|
||||
ansible -m yum -a "pkg=ohai ensure=installed"
|
||||
|
||||
Ansible's approach to configuration -- separating variables from tasks, keeps your playbooks
|
||||
from turning into arbitrary code with ugly nested ifs, conditionals, and so on - and results
|
||||
in more streamlined & auditable configuration rules -- especially because there are a
|
||||
minimum of decision points to track.
|
||||
|
||||
.. _variable_precedence:
|
||||
|
||||
Variable Precedence: Where Should I Put A Variable?
|
||||
```````````````````````````````````````````````````
|
||||
|
||||
A lot of folks may ask about how variables override another. Ultimately it's Ansible's philosophy that it's better
|
||||
you know where to put a variable, and then you have to think about it a lot less.
|
||||
|
||||
Avoid defining the variable "x" in 47 places and then ask the question "which x gets used".
|
||||
Why? Because that's not Ansible's Zen philosophy of doing things.
|
||||
|
||||
There is only one Empire State Building. One Mona Lisa, etc. Figure out where to define a variable, and don't make
|
||||
it complicated.
|
||||
|
||||
However, let's go ahead and get precedence out of the way! It exists. It's a real thing, and you might have
|
||||
a use for it.
|
||||
|
||||
If multiple variables of the same name are defined in different places, they win in a certain order, which is::
|
||||
|
||||
* -e variables always win
|
||||
* then comes "most everything else"
|
||||
* then comes variables defined in inventory
|
||||
* then "role defaults", which are the most "defaulty" and lose in priority to everything.
|
||||
|
||||
That seems a little theoretical. Let's show some examples and where you would choose to put what based on the kind of
|
||||
control you might want over values.
|
||||
|
||||
First off, group variables are super powerful.
|
||||
|
||||
Site wide defaults should be defined as a 'group_vars/all' setting. Group variables are generally placed alongside
|
||||
your inventory file. They can also be returned by a dynamic inventory script (see `intro_dynamic_inventory`) or defined
|
||||
in things like AnsibleWorks AWX from the UI or API::
|
||||
|
||||
---
|
||||
# file: /etc/ansible/group_vars/all
|
||||
# this is the site wide default
|
||||
ntp_server: default-time.example.com
|
||||
|
||||
Regional information might be defined in a 'group_vars/region' variable. If this group is a child of the 'all' group (which it is, because all groups are), it will override the group that is higher up and more general::
|
||||
|
||||
---
|
||||
# file: /etc/ansible/group_vars/boston
|
||||
ntp_server: boston-time.example.com
|
||||
|
||||
If for some crazy reason we wanted to tell just a specific host to use a specific NTP server, it would then override the group variable!::
|
||||
|
||||
---
|
||||
# file: /etc/ansible/host_vars/xyz.boston.example.com
|
||||
ntp-server: override.example.com
|
||||
|
||||
So that covers inventory and what you would normally set there. It's a great place for things that deal with geography or behavior. Since groups are frequently the entity that maps roles onto hosts, it is sometimes a shortcut to set variables on the group instead of defining them on a role. You could go either way.
|
||||
|
||||
Remember: Child groups override parent groups, and hosts always override their groups.
|
||||
|
||||
Next up: learning about role variable precedence.
|
||||
|
||||
We'll pretty much assume you are using roles at this point. You should be using roles for sure. Roles are great. You are using
|
||||
roles aren't you? Hint hint.
|
||||
|
||||
Ok, so if you are writing a redistributable role with reasonable defaults, put those in the 'roles/x/defaults/main.yml' file. This means
|
||||
the role will bring along a default value but ANYTHING in Ansible will override it. It's just a default. That's why it says "defaults" :)
|
||||
See `intro_roles` for more info about this::
|
||||
|
||||
----
|
||||
# file: roles/x/defaults/main.yml
|
||||
# if not overriden in inventory or as a parameter, this is the value that will be used
|
||||
http_port: 80
|
||||
|
||||
if you are writing a role and want to ensure the value in the role is absolutely used in that role, and is not going to be overridden
|
||||
by inventory, you should but it in roles/x/vars/main.yml like so, and inventory values cannot override it. -e however, still will::
|
||||
|
||||
----
|
||||
# file: roles/x/vars/main.yml
|
||||
# this will absolutely be used in this role
|
||||
http_port: 80
|
||||
|
||||
So the above is a great way to plug in constants about the role that are always true. If you are not sharing your role with others,
|
||||
app specific behaviors like ports is fine to put in here. But if you are sharing roles with others, putting variables in here might
|
||||
be bad. Nobody will be able to override them with inventory, but they still can by passing a parameter to the role.
|
||||
|
||||
Parameterized roles are useful.
|
||||
|
||||
If you are using a role and want to override a default, pass it as a parameter to the role like so::
|
||||
|
||||
roles:
|
||||
- { name: apache, http_port: 8080 }
|
||||
|
||||
This makes it clear to the playbook reader that you've made a conscious choice to override some default in the role, or pass in some
|
||||
configuration that the role can't assume by itself. It also allows you to pass something site-specific that isn't really part of the
|
||||
role you are sharing with others.
|
||||
|
||||
This can often be used for things that might apply to some hosts multiple times,
|
||||
like so::
|
||||
|
||||
roles:
|
||||
- { role: app_user, name: Ian }
|
||||
- { role: app_user, name: Terry }
|
||||
- { role: app_user, name: Graham }
|
||||
- { role: app_user, name: John }
|
||||
|
||||
That's a bit arbitrary, but you can see how the same role was invoked multiple Times. In that example it's quite likely there was
|
||||
no default for 'name' supplied at all. Ansible can yell at you when variables aren't defined -- it's the default behavior in fact.
|
||||
|
||||
So that's a bit about roles.
|
||||
|
||||
There are a few bonus things that go on with roles.
|
||||
|
||||
Generally speaking, variables set in one role are available to others. This means if you have a "roles/common/vars/main.yml" you
|
||||
can set variables in there and make use of them in other roles and elsewhere in your playbook::
|
||||
|
||||
roles:
|
||||
- { role: common_settings }
|
||||
- { role: something, foo: 12 }
|
||||
- { role: something_else }
|
||||
|
||||
.. note:: There are some protections in place to avoid the need to namespace variables.
|
||||
In the above, variables defined in common_settings are most definitely available to 'app_user' and 'something_else' tasks, but if
|
||||
"something's" guaranteed to have foo set at 12, even if somewhere deep in common settings it set foo to 20.
|
||||
|
||||
So, that's precedence, explained in a more direct way. Don't worry about precedence, just think about if your role is defining a
|
||||
variable that is a default, or a "live" variable you definitely want to use. Inventory lies in precedence right in the middle, and
|
||||
if you want to forcibly override something, use -e.
|
||||
|
||||
If you found that a little hard to understand, take a look at the "ansible-examples" repo on our github for a bit more about
|
||||
how all of these things can work together.
|
||||
|
||||
.. seealso::
|
||||
|
||||
:doc:`playbooks`
|
||||
An introduction to playbooks
|
||||
:doc:`playbooks_conditionals`
|
||||
Conditional statements in playbooks
|
||||
:doc:`playbooks_loops`
|
||||
Looping in playbooks
|
||||
:doc:`playbooks_roles`
|
||||
Playbook organization by roles
|
||||
:doc:`playbooks_best_practices`
|
||||
Best practices in playbooks
|
||||
`User Mailing List <http://groups.google.com/group/ansible-devel>`_
|
||||
Have a question? Stop by the google group!
|
||||
`irc.freenode.net <http://irc.freenode.net>`_
|
||||
#ansible IRC chat channel
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue