using ansible dynamic inventory with amazon ec2

29
Using Ansible Dynamic Inventory with Amazon EC2 Brian Schott, CTO Nimbis Services brian.schott@nimbisservic es.com Using Ansible Dynamic Inventory for Amazon EC2 1

Upload: brian-schott

Post on 21-Apr-2017

10.888 views

Category:

Internet


1 download

TRANSCRIPT

Vagrant, Ansible and OpenStack on your laptop

Using Ansible Dynamic Inventory with Amazon EC2Brian Schott, CTONimbis [email protected] Ansible Dynamic Inventory for Amazon EC21

Something BorrowedUsing Ansible Dynamic Inventory for Amazon EC22

http://www.slideshare.net/lorinh/vagrant-ansible-and-openstack-on-your-laptop

http://www.ansible.com/ansible-book

Shell scripts are painful, Puppet & Chef have steep learning curves

if [[ $EUID -eq 0 ]]; then ROOTSLEEP=${ROOTSLEEP:-10} echo "You are running this script as root." echo "In $ROOTSLEEP seconds, we will create a user 'stack' and run as that user" sleep $ROOTSLEEP

# since this script runs as a normal user, we need to give that user # ability to run sudo if [[ "$os_PACKAGE" = "deb" ]]; then dpkg -l sudo || apt_get update && install_package sudo else rpm -qa | grep sudo || install_package sudo fi if ! getent passwd stack >/dev/null; then echo "Creating a user called stack" useradd -U -s /bin/bash -d $DEST -m stack fi

Source: devstack/stack.shUsing Ansible Dynamic Inventory for Amazon EC23

Ansible big idea: very simple syntax, SSH for communicationOpen Sourcehttps://github.com/ansible/ansibleAgentlessPythonYAMLJinja2SSHUsing Ansible Dynamic Inventory for Amazon EC24

http://ansible.com/ansible-resources

Example Ansible playbook: install ntp---- hosts: controller tasks: - name: ensure ntp packages is installed action: apt pkg=ntp

- name: ensure ntp.conf file is present action: copy src=files/ntp.conf dest=/etc/ntp.conf owner=root group=root mode=0644

- name: ensure ntp service is restarted action: service name=ntp state=restarted

Using Ansible Dynamic Inventory for Amazon EC25

Ansible scripts are called playbooks, that are organized into individual plays.

Ansible plays are collection of tasks. You also need to specify which hosts youre running on.

This play has three tasks: - Install the NTP package - Copy over a local ntp.conf file - Restart the ntp service5

Specify hosts in an inventory file[controller]192.168.206.130

[compute]192.168.206.131192.168.206.132192.168.206.133192.168.206.134

Using Ansible Dynamic Inventory for Amazon EC26

By default, ansible will look in /etc/ansible/hosts for the inventory file, you can override this to specify a different location.6

Run the playbook$ ansible-playbook ntp.yamlPLAY [controller] *********************

GATHERING FACTS ********************* ok: [192.168.206.130]

TASK: [ensure ntp packages is installed] ********************* ok: [192.168.206.130]

TASK: [ensure ntp.conf file is present] ********************* ok: [192.168.206.130]

TASK: [ensure ntp service is restarted] ********************* ok: [192.168.206.130]

PLAY RECAP ********************* 192.168.206.130 : ok=4 changed=3 unreachable=0 failed=0 Using Ansible Dynamic Inventory for Amazon EC27

What did Ansible just do?Made SSH connections to remote host(s)Copied over Python modules and arguments parsed from playbook fileExecuted modules on remote machineUsing Ansible Dynamic Inventory for Amazon EC28

Can run a single action usingansible command

$ ansible controller m apt a "pkg=ntp"

192.168.206.130 | success >> { "changed": false, "item": "", "module": "apt"}Using Ansible Dynamic Inventory for Amazon EC29

Ansible scripts are idempotent: can run multiple times safely$ ansible-playbook ntp.yamlPLAY [controller] *********************

GATHERING FACTS ********************* ok: [192.168.206.130]

TASK: [ensure ntp packages is installed] ********************* ok: [192.168.206.130]

TASK: [ensure ntp.conf file is present] ********************* ok: [192.168.206.130]

TASK: [ensure ntp service is restarted] ********************* ok: [192.168.206.130]

PLAY RECAP ********************* 192.168.206.130 : ok=4 changed=1 unreachable=0 failed=0 Using Ansible Dynamic Inventory for Amazon EC210

The items that appear in green did not change state. With a real ansible run, yellow ones would change state.10

What does Ansible have to do with cloud?Using Ansible Dynamic Inventory for Amazon EC211

http://docs.ansible.com/list_of_cloud_modules.html

Ansible Dynamic Inventory Feature

Using Ansible Dynamic Inventory for Amazon EC212http://docs.ansible.com/intro_dynamic_inventory.html

A Practical ExampleUsing Ansible Dynamic Inventory for Amazon EC213https://github.com/electroniceagle/ansible-dc-ec2-tutorial

AWS Free Tier

Using Ansible Dynamic Inventory for Amazon EC214

Ansible Galaxy (https://galaxy.ansible.com) Using Ansible Dynamic Inventory for Amazon EC215

Ansible Directory Structure

Using Ansible Dynamic Inventory for Amazon EC216

ansible.cfg

Using Ansible Dynamic Inventory for Amazon EC217

provision_instances.yamlUsing Ansible Dynamic Inventory for Amazon EC218

keypair.yaml

Using Ansible Dynamic Inventory for Amazon EC219

securitygroup.yaml

Using Ansible Dynamic Inventory for Amazon EC220

instance.yaml

Using Ansible Dynamic Inventory for Amazon EC221

Provision PlaybookUsing Ansible Dynamic Inventory for Amazon EC222

setup_instances.yaml, part 1Using Ansible Dynamic Inventory for Amazon EC223

setup_instances.yaml, part 2

Using Ansible Dynamic Inventory for Amazon EC224

setup_instances.yaml, part 3

Using Ansible Dynamic Inventory for Amazon EC225

Setup Playbook

Using Ansible Dynamic Inventory for Amazon EC226

terminate_instances.yaml

Using Ansible Dynamic Inventory for Amazon EC227

Terminate Playbook

Using Ansible Dynamic Inventory for Amazon EC228

Thank YouBrian Schott, CTONimbis [email protected]

https://github.com/electroniceagle/ansible-dc-ec2-tutorialhttps://slideshare.net/bfschott

Using Ansible Dynamic Inventory for Amazon EC229