Ansible allows you to manage the configuration of remote linux server instances — whether config files, installed software.
Additionally Ansible can create hosts in EC2 (and other clouds or VPS providers such as digitalocean), in Vagrant, through cobbler, etc.
Ansible is open source
Key concepts of Ansible include:
Playbooks specify a list of tasks* that are run in sequence across one or more hosts. Each task can also run multiple times with a variable taking a different value
- hosts: localhost
connection: local
tasks:
— name: create ex1, ex2 directories in current directory
action: file state=directory dest={{item}}
with_items: [ex1, ex2]
* Also: roles, handlers, variables. But mostly tasks!
Inventory is the representation of information about hosts — what groups a host belongs to, the properties those groups and hosts have. A hierarchy of groups often results
-i
flag or set it in
your Ansible config file, and it can point to a static file (typically
called hosts) or a dynamic inventory script (e.g. ec2.py).~/inventory/hosts
,
then the information
about the exampleapp group would live in
~/inventory/group_vars/exampleapp.yml
Templates allow you to generate configuration files from values set in various inventory properties. This means that you can store one template in source control that applies to many different environments.
An example might be a file specifying database connection information that would have the same structure but different values for dev, test and prod environments
db.settings={{dbhost}}:{{dbport}}/{{dbuser}}:{{dbpass}}@{{dbschema}}
Roles are a way to encapsulate common tasks and properties for reuse. One example is to install java, a very common task!
- role: ../../../common/roles/java7/0.3.0
minor_version: 45
dest: /opt/java
If you find yourself writing the same tasks in multiple playbooks, turn them into roles.
You will need:
Ansible modules are designed to be safely repeatable
Use the file module rather than command with rm, mkdir, rmdir etc.
Other modules that replace shell commands include synchronize, unarchive, git, hg, svn
You'll need to merge in patch #5123 for this to work in your environment
As with any software development, make the most of the tools of your environment. Agree coding standards with your teams, and enforce them with pre- or post-commit scripts.
You can use e.g. ansible-playbook --syntax-check
to ensure that the playbook is syntactically valid, for example
This is not specifically Ansible related — there are some things you just have to do to manage AWS. But this is how the two go together
Ansible relies on boto. With a suitable ~/.boto config file, the playbook that creates an instance looks a bit like:
- name: create instance
ec2:
user_data: "{{ lookup('template', '../templates/userdata.tmpl') }}"
region: "{{region}}"
image: "{{rhel6ami}}"
instance_type: "{{instance_type}}"
vpc_subnet_id: "{{vpc_subnet}}"
group_id: "{{security_group}}"
register: ec2