The document discusses using Ansible and Vagrant together to easily test and deploy OpenStack. Ansible allows writing idempotent infrastructure scripts, while Vagrant allows testing them by booting reproducible virtual machines. The document provides an example of using Ansible plays to install NTP and using Vagrant to define VMs for an OpenStack controller and compute node.
4. 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.sh
5. You want an easy way to write & debug
deployment scripts
6. Use Ansible to write OpenStack
deployment scripts, Vagrant to test
them inside of VMs
8. Example Ansible play: 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
9. 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
10. 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
11. What did Ansible just do?
1. Made SSH connections to remote host
2. Copied over Python modules and arguments
parsed from playbook file
3. Executed modules on remote machine
12. Can run a single action using
ansible command
$ ansible controller –m apt –a "pkg=ntp"
192.168.206.130 | success >> {
"changed": false,
"item": "",
"module": "apt"
}
13. 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=1
unreachable=0 failed=0
14. Use handlers if action should only
occur on a state change
---
- hosts: controller
tasks:
- name: ensure glance database is present
action: mysql_db name=glance
notify:
- version glance database
handlers:
- name: version glance database
action: command glance-manage version_control 0
20. Boot it and connect to it
$ vagrant up
[default] Importing base box 'precise64'...
[default] Matching MAC address for NAT networking...
[default] Clearing any previously set forwarded ports...
[default] Fixed port collision for 22 => 2222. Now on port 2200.
[default] Forwarding ports...
[default] -- 22 => 2200 (adapter 1)
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Mounting shared folders...
[default] -- v-root: /vagrant
$ vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64)
* Documentation: https://help.ubuntu.com/
Welcome to your Vagrant-built virtual machine.
Last login: Thu Jun 7 00:49:30 2012 from 10.0.2.2
vagrant@precise64:~$
21. Boot multi-VMs: configure IPs,
memory, hostname
Vagrant::Config.run do |config|
config.vm.box = "precise64”
config.vm.define :controller do |controller_config|
controller_config.vm.network :hostonly, "192.168.206.130"
controller_config.vm.host_name = "controller"
end
config.vm.define :compute1 do |compute1_config|
compute1_config.vm.network :hostonly, "192.168.206.131"
compute1_config.vm.host_name = "compute1"
compute1_config.vm.customize ["modifyvm", :id,
"--memory", 1024]
end
end
Ansible scripts are called playbooks, that are organized into individual plays.Ansible plays are collection of tasks. You also need to specify which hosts you’re running on.This play has three tasks: - Install the NTP package - Copy over a local ntp.conf file - Restart the ntp service
By default, ansible will look in /etc/ansible/hosts for the inventory file, you can override this to specify a different location.
The items that appear in green did not change state. With a real ansible run, yellow ones would change state.
Ansible scripts are called playbooks, that are organized into individual plays.Ansible plays are collection of tasks. You also need to specify which hosts you’re running on.This play has three tasks: - Install the NTP package - Copy over a local ntp.conf file - Restart the ntp service
Arbitrary shell commands are not idempotent, of course