From charlesreid1

Revision as of 03:49, 9 November 2018 by Admin (talk | contribs)

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, but with AWS this information will become out of date quickly. Ansible can get information via the AWS API, but has to know to do that.

Ansible can be put in dynamic inventory mode by........ using an executable dynamic inventory file (not sure how you specify an inventory file, but ok):

chmod +x dynamic.py

The script must accept two command line flags:

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

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

$ ./dynamic.py --host=vagrant2

Example executable dynamic inventory script

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