From b71cf6653fe6ce1a233718c5b39860b0f276ec87 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Sun, 13 May 2012 12:03:24 -0400 Subject: [PATCH] Add missing files --- bestpractices.html | 297 ++++++++++++++++++++++++++ playbooks2.html | 509 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 806 insertions(+) create mode 100644 bestpractices.html create mode 100644 playbooks2.html diff --git a/bestpractices.html b/bestpractices.html new file mode 100644 index 0000000000..e1977fda50 --- /dev/null +++ b/bestpractices.html @@ -0,0 +1,297 @@ + + + + + + + + Best Practices — Ansible - SSH-Based Configuration Management & Deployment + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+Fork me on GitHub + + +
+ Ansible
+
+ +
+

Best Practices

+

Here are some tips for making the most of Ansible.

+
+

Group By Roles

+

A system can be in multiple groups. See ref: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.

+
+
+

Directory Organization

+

Playbooks should be organized like this:

+
(root of source control repository)
+
+    acme/           # each playbook has a directory
+
+        setup.yml   # playbook to manage the service
+        stop.yml    # playbook to halt the service (optional)
+
+        files/
+           some_file_path_foo.conf
+
+        templates/
+           etc_acme_conf_acme.conf
+           etc_other_conf_other.conf
+
+        vars/
+           main.yml
+
+        handlers/
+           main.yml
+
+        tasks/
+           setup.yml
+           stop.yml
+
+

Any directories or files not needed can be omitted. Not all modules may require vars or files sections, though most +will require handlers, tasks, and templates. To review what each of these sections do, see ref:playbooks and ref:playbooks2.

+

The acme/setup.yml playbook would be as simple as:

+
----
+
+- hosts: webservers
+  user: root
+
+  vars_files
+    - include: vars/main.yml
+  tasks:
+    - include: tasks/setup.yml
+  handlers:
+    - include: handlers/main.yml
+
+

The tasks are individually broken out in ‘acme/tasks/setup.yml’, and handlers, which are common to all task files, +are contained in ‘acme/handlers/main.yml’. As a reminder, handlers are mostly just used to notify services to restart +when things change, and these are described in ref:playbooks.

+

Including more than one setup file or more than one handlers file is of course legal.

+

Having playbooks be able to include other playbooks is coming in release 0.5.

+

Until then, to manage your entire site, simply execute all of your playbooks together, in the order desired. +You don’t have to do this though, it’s fine to select sections of your infrastructure to manage at a single time. +You may wish to construct simple shell scripts to wrap calls to ansible-playbook.

+
+
+

Miscellaneous Tips

+

When you can do something simply, do something simply. Do not reach to use every feature of Ansible together, all +at once. Use what works for you. For example, you should probably not need ‘vars’, ‘vars_files’, ‘vars_prompt’ and ‘–extra-vars’ all at once, while also using an external inventory file.

+

Optimize for readability. Whitespace between sections of YAML documents and in between tasks is strongly encouraged, +as is usage of YAML comments, which start with “#”. It is also useful to comment at the top of each file the purpose of the individual file and the author, including email address.

+

It is possible to leave off the “name” for a given task, though it is recommended to provide +a descriptive comment about why something is being done instead.

+

Use version control. Keep your playbooks and inventory file in git (or another version control system), and commit when you make changes to them. +This way you have an audit trail describing when and why you changed the rules automating your infrastructure.

+

Resist the urge to write the same playbooks and configuration files for heterogeneous distributions. While lots of software packages claim to make this easy on you, the configuration files are often quite different, to the point where it would be easier to treat them as different playbooks. This is why, for example, Ansible has a seperate ‘yum’ and ‘apt’ module. Yum and apt have different capabilities, and we don’t want to code for the least common denominator.

+

Use variables for user tunable settings versus having constants in the tasks file or templates, so that it is easy to reconfigure a playbook. Think about this as exposing the knobs to things you would like to tweak.

+

Since a system can be in more than one group, if you have multiple datacenters or sites, consider putting systems into groups by role, but also different groups by geography. This allows you to assign different variables to different geographies.

+
+

See also

+
+
YAML Syntax
+
Learn about YAML syntax
+
Playbooks
+
Review the basic playbook features
+
Ansible Modules
+
Learn about available modules
+
Module Development
+
Learn how to extend Ansible by writing your own modules
+
Inventory & Patterns
+
Learn about how to select hosts
+
Github examples directory
+
Complete playbook files from the github project source
+
Mailing List
+
Questions? Help? Ideas? Stop by the list on Google Groups
+
+
+
+
+ + +
+
+ + + \ No newline at end of file diff --git a/playbooks2.html b/playbooks2.html new file mode 100644 index 0000000000..682a458a6b --- /dev/null +++ b/playbooks2.html @@ -0,0 +1,509 @@ + + + + + + + + Advanced Playbooks — Ansible - SSH-Based Configuration Management & Deployment + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+Fork me on GitHub + + +
+ Ansible
+
+ +
+

Advanced Playbooks

+

Here are some advanced features of the playbooks language. Using all of these features +are not neccessary, but many of them will prove useful. If a feature doesn’t seem immediately +relevant, feel free to skip it. For many people, the features documented in playbooks will +be 90% or more of what they use in Ansible.

+
+

Accessing Complex Variable Data

+

Some provided facts, like networking information, are made available as nested datastructures. To access +them a simple ‘$foo’ is not sufficient, but it is still easy to do. Here’s how we get an IP address using +Ansible 0.4 and later:

+
${ansible_eth0.ipv4.address}
+
+

It is also possible to access variables whose elements are arrays:

+
${somelist[0]}
+
+

And the array and hash reference syntaxes can be mixed.

+

In templates, the simple access form still holds, but they can also be accessed from Jinja2 in more Python-native ways if +that is preferred:

+
{{ ansible_eth0["ipv4"]["address"] }}
+
+
+
+

Accessing Information About Other Hosts

+

If your database server wants to check 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 (note: this uses syntax available in 0.4 and later):

+
${hostvars.hostname.factname}
+
+

NOTE: No database or other complex system is required to exchange data between hosts. The hosts that you +want to reference data from must be included in either the current play or any previous play.

+
+
+

Magic Variables

+

Some variables made available to hosts don’t come from definitions in a playbook, the inventory file, or discovery from the system. There are only two of these, and are used in special cases that many users won’t need.

+

groups 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 groups %}
+   # some part of a configuration file that only applies to webservers
+{% endif %}
+
+

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. Don’t worry about it unless you think you need it.

+
+
+

Variable File Seperation

+

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
+  user: root
+  vars:
+    favcolor: blue
+  vars_files:
+    - /vars/external_vars.yml
+  tasks:
+  - name: this is just a placeholder
+    action: 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
+
+
+
+

Prompting For Sensitive Data

+

You may wish to prompt the user for certain input, and can +do so with the similarly named ‘vars_prompt’ section. 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:

+
---
+- hosts: all
+  user: root
+  vars:
+    from: "camelot"
+  vars_prompt:
+    name: "what is your name?"
+    quest: "what is your quest?"
+    favcolor: "what is your favorite color?"
+
+

There are full examples of both of these items in the github examples/playbooks directory.

+
+
+

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"
+
+
+
+

Conditional Execution

+

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 only_if clause, which actually is a Python expression. +Don’t panic – it’s actually pretty simple:

+
vars:
+  favcolor: blue
+  is_favcolor_blue: "'$favcolor' == 'blue'"
+  is_centos: "'$facter_operatingsystem' == 'CentOS'"
+tasks:
+  - name: "shutdown if my favorite color is blue"
+    action: command /sbin/shutdown -t now
+    only_if: '$is_favcolor_blue'
+
+

Variables from tools like facter and ohai can be used here, if installed, or you can +use variables that bubble up from ansible (0.3 and later). As a reminder, +these variables are prefixed, so it’s $facter_operatingsystem, not $operatingsystem. Ansible’s +built in variables are prefixed with ansible_. The only_if +expression is actually a tiny small bit of Python, so be sure to quote variables and make something +that evaluates to True or False. It is a good idea to use ‘vars_files’ instead of ‘vars’ to define +all of your conditional expressions in a way that makes them very easy to reuse between plays +and playbooks.

+
+
+

Conditional Imports

+

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
+  user: root
+  vars_files:
+    - "vars/common.yml"
+    - [ "vars/$facter_operatingsystem.yml", "vars/os_defaults.yml" ]
+  tasks:
+  - name: make sure apache is running
+    action: service name=$apache state=running
+
+

Note that a variable ($facter_operatingsystem) 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 up 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 – seperating 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.

+
+
+

Loop Shorthand

+

To save some typing, repeated tasks can be written in short-hand like so:

+
- name: add user $item
+  action: user name=$item state=present groups=wheel
+  with_items:
+     - testuser1
+     - testuser2
+
+

The above would be the equivalent of:

+
- name: add user testuser1
+  action: user name=testuser1 state=present groups=wheel
+- name: add user testuser2
+  action: user name=testuser2 state=present groups=wheel
+
+

In a future release, the yum and apt modules will use with_items to execute fewer package +manager transactions.

+
+
+

Selecting Files And Templates Based On Variables

+

Sometimes a configuration file you want to copy, or a template you will use may depend on a variable. +The following construct (new in 0.4) 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
+  action: template src=$item dest=/etc/myapp/foo.conf
+  first_available_file:
+    - /srv/templates/myapp/${ansible_distribution}.conf
+    - /srv/templates/myapp/default.conf
+
+
+
+

Asynchronous Actions and Polling

+

By default tasks in playbooks block, meaning the connections stay open +until the task is done on each node. If executing playbooks with +a small parallelism value (aka --forks), you may wish that long +running operations can go faster. 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
+  user: root
+  tasks:
+  - name: simulate long running op (15 sec), wait for up to 45, poll every 5
+    action: 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
+  user: root
+  tasks:
+  - name: simulate long running op, allow to run for 45, fire and forget
+    action: 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.

+
+
+
+

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
+
+
+
+

Pull-Mode Playbooks

+

The use of playbooks in local mode (above) is made extremely powerful with the addition of ansible-pull in the +0.4 release. A script for setting up ansible-pull is provided in the examples/playbooks directory of the source +checkout.

+

The basic idea is to use Ansible to set up a remote copy of ansible on each managed node, each set to run via +cron and update playbook source via git. This interverts the default push architecture of ansible into a pull +architecture, which has near-limitless scaling potential. The setup playbook can be tuned to change +the cron frequency, logging locations, and parameters to ansible-pull.

+

This is useful both for extreme scale-out as well as periodic remediation. Usage of the ‘fetch’ module to retrieve +logs from ansible-pull runs would be an excellent way to gather and analyze remote logs from ansible-pull.

+
+

See also

+
+
YAML Syntax
+
Learn about YAML syntax
+
Playbooks
+
Review the basic playbook features
+
Best Practices
+
Various tips about playbooks in the real world
+
Ansible Modules
+
Learn about available modules
+
Module Development
+
Learn how to extend Ansible by writing your own modules
+
Inventory & Patterns
+
Learn about how to select hosts
+
Github examples directory
+
Complete playbook files from the github project source
+
Mailing List
+
Questions? Help? Ideas? Stop by the list on Google Groups
+
+
+
+
+ + +
+
+ + + \ No newline at end of file