using ansible dynamic inventory with amazon ec2

29
Using Ansible Dynamic Inventory with Amazon EC2 Brian Schott, CTO Nimbis Services [email protected] Using Ansible Dynamic Inventory for Amazon EC2 1

Upload: brian-schott

Post on 14-Jul-2015

2.911 views

Category:

Internet


2 download

TRANSCRIPT

Using Ansible Dynamic Inventory with Amazon EC2

Brian Schott, CTO

Nimbis Services

[email protected]

Using Ansible Dynamic Inventory for Amazon EC2 1

Something Borrowed…

Using Ansible Dynamic Inventory for Amazon EC2 2

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 EC2 3

Ansible big idea: very simple syntax, SSH for communication

• Open Sourcehttps://github.com/ansible/ansible

• Agentless

• Python

• YAML

• Jinja2

• SSH

Using Ansible Dynamic Inventory for Amazon EC2 4

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 EC2 5

Specify hosts in an inventory file

[controller]

192.168.206.130

[compute]

192.168.206.131

192.168.206.132

192.168.206.133

192.168.206.134

Using Ansible Dynamic Inventory for Amazon EC2 6

Run the playbook$ ansible-playbook ntp.yaml

PLAY [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 EC2 7

What did Ansible just do?

1. Made SSH connections to remote host(s)

2. Copied over Python modules and arguments parsed from playbook file

3. Executed modules on remote machine

Using Ansible Dynamic Inventory for Amazon EC2 8

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 EC2 9

Ansible scripts are idempotent: can run multiple times safely

$ ansible-playbook ntp.yaml

PLAY [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=1unreachable=0 failed=0

Using Ansible Dynamic Inventory for Amazon EC2 10

What does Ansible have to do with cloud?

Using Ansible Dynamic Inventory for Amazon EC2 11http://docs.ansible.com/list_of_cloud_modules.html

Ansible Dynamic Inventory Feature

Using Ansible Dynamic Inventory for Amazon EC2 12

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

A Practical Example

Using Ansible Dynamic Inventory for Amazon EC2 13

https://github.com/electroniceagle/ansible-dc-ec2-tutorial

AWS Free Tier

Using Ansible Dynamic Inventory for Amazon EC2 14

Ansible Galaxy (https://galaxy.ansible.com)

Using Ansible Dynamic Inventory for Amazon EC2 15

Ansible Directory Structure

Using Ansible Dynamic Inventory for Amazon EC2 16

ansible.cfg

Using Ansible Dynamic Inventory for Amazon EC2 17

provision_instances.yaml

Using Ansible Dynamic Inventory for Amazon EC2 18

keypair.yaml

Using Ansible Dynamic Inventory for Amazon EC2 19

securitygroup.yaml

Using Ansible Dynamic Inventory for Amazon EC2 20

instance.yaml

Using Ansible Dynamic Inventory for Amazon EC2 21

Provision Playbook

Using Ansible Dynamic Inventory for Amazon EC2 22

setup_instances.yaml, part 1

Using Ansible Dynamic Inventory for Amazon EC2 23

setup_instances.yaml, part 2

Using Ansible Dynamic Inventory for Amazon EC2 24

setup_instances.yaml, part 3

Using Ansible Dynamic Inventory for Amazon EC2 25

Setup Playbook

Using Ansible Dynamic Inventory for Amazon EC2 26

terminate_instances.yaml

Using Ansible Dynamic Inventory for Amazon EC2 27

Terminate Playbook

Using Ansible Dynamic Inventory for Amazon EC2 28

Thank You

• Brian Schott, CTONimbis [email protected]

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

Using Ansible Dynamic Inventory for Amazon EC2 29