From charlesreid1

List of user-contributed Ansible scripts to interface with other machine-managing services (like AWS): https://github.com/ansible/ansible/tree/devel/contrib/inventory

Basics

How to use Ansible Hosts file with AWS

The hosts inventory file is treated as static when managing our own infrastructure. With AWS or large numbers of Vagrant machines, we do not want to manage machines by hand.

To use Ansible with AWS, we need to use a dynamic hosts file, and use the AWS API to get information about machines. We can do that by using an executable hosts file that runs a program to return the information we need. (Also see Ansible/Hosts/Dynamic Inventory page.)

The dynamic inventory script must accept to command line flags, for the two ways Ansible will call this dynamic inventory script:

--host=<hostname>   show host details
--list              list groups

For example, Ansible will call the inventory script like so:

./dynamic.py --host=vagrant2

We can call the dynamic inventory script ourselves to test, but usually what we're doing is calling Ansible and passing the -i flag, along with the path to the inventory file.

For example, if we want to use a dynamic inventory file to get information about the vagrant hosts running on a system, and we wanted to ping a vagrant box named myvagrantbox, we could run the command:

ansible -i dynamic.py myvagrantbox -m ping

(where dynamic.py is the dynamic inventory file that interacts with vagrant.)

Example dynamic inventory scripts

Vagrant dynamic inventory script: https://charlesreid1.com/wiki/Ansible/Hosts/Dynamic_Inventory#Putting_it_all_together

Huge list of user-contributed dynamic inventory scripts

See https://github.com/ansible/ansible/tree/devel/contrib/inventory for a huge list of user-contributed dynamic inventory scripts

How to manage static and dynamic inventory

To have a regular static inventory file and a dynamic inventory script, or any combination of the above, put them all in a directory, and tell Ansible to use this directory for inventory in the Ansible configuration file or on the command line.

If our directory structure is:

playbooks/inventory/hosts
playbooks/inventory/vagrant.py

we would have an ansible.cfg with the contents:

[defaults]
inventory = inventory

How to add and configure hosts in a playbook

It is important to note that the dynamic inventory script is executed at the beginning of a playbook.

If a playbook creates new hosts, the dynamic inventory script will not pick up the new hosts.

Therefore, need to add tasks using the add_host module.

add_host name=hostname groups=web,staging myvar=myval


Example playbook adding a host

Here is an example playbook that uses the add_host command:

- name: Provision a vagrant machine
  hosts: localhost
  vars:
    box: xenial64
  tasks:
    - name: create a Vagrantfile
      command: vagrant init {{ box }} creates=Vagrantfile

    - name: Bring up a vagrant machine
      command: vagrant up

    - name: add the vagrant machine to the inventory
      add_host: >
            name=vagrant
            ansible_host=127.0.0.1
            ansible_port=2222
            ansible_user=vagrant
            ansible_private_key_file=/home/username/.vagrant.d/
            insecure_private_key

- name: Do something to the vagrant machine
  hosts: vagrant
  become: yes
  tasks:
    # The list of tasks would go here
    - ...

NOTE: This is a good pattern to use.

Play number 1 runs against localhost - it configures and provisions the hosts.

Play number 2 configures the hosts.

Because we use the creates=Vagrantfile, it tells Ansible to only run the first play once (or, if our Vagrant boxes have been destroyed).


How to group hosts in a playbook

Another useful task is to be able to group hosts in a playbook.

Because on AWS we may be dealing with various types of machines, we can use built-in Ansible variables to determine the Linux distribution or architecture of our target machines, and use those to determine which group to add the machines to.

Example: {{ ansible_distribution }} refers to the Linux distribution

To group by distribution type:

- name: create groups based on Linux distribution
  group_by: key={{ ansible_distribution }}

Example playbook grouping by host distribution

- name: group hosts by distribution
  hosts: myhosts
  gather_facts: True
  tasks:
    - name: create groups based on distro
      group_by: key={{ ansible_distribution }}

- name: do something to Ubuntu hosts
  hosts: Ubuntu
  tasks:
    - name: install htop
      apt: name=htop
    # ...

- name: do something else to CentOS hosts
  hosts: CentOS
  tasks:
    - name: install htop
      yum: name=htop
    # ...

Advanced

Dynamic Inventory Plugin

Tags

EC2 VPC

Ansible Config for EC2

New EC2 Instances

Keys and Security Groups

Bringing Up Servers

Idempotency

Specifying a VPC

Flags