A simple configuration management and orchestration tool.
brew install python
easy_install pip
pip install -Iv ansible==1.7.2
Create a folder for your development environment
mkdir testansible
cd testansible
Initialize Vagrant with the box you just imported
vagrant init ubuntu-12.04
Uncomment the private network line in Vagrantfile and change the ip
config.vm.network "private_network", ip: "192.168.33.13"
Start Vagrant Instance
vagrant up
Ansible static inventory uses INI-like format.
It looks something like this
mail.example.coma ;<~host
[webservers]
foo.example.com
bar.example.com
[dbservers] ;<~group
one.example.com id=10 ;<~host_vars
two.example.com id=11 ;<~host_vars
foo.example.com
[ranged_hosts]
www[01:50].example.com
db-[a:f].example.com
[webservers:vars] ; group_vars
http_port=80
Add this in hosts file inside the testansible folder
[servers]
server ansible_ssh_host=192.168.33.13 ansible_ssh_user=vagrant ansible_ssh_private_key_file=.vagrant/machines/default/virtualbox/private_key
[localhost]
127.0.0.1 ansible_connection=local ec2_tag_Name=localhost
Now you have inventory, you can execute your own ad-hoc commands
Run this on terminal:
ansible -i hosts server -m shell -a "uptime"
Lets target a group this time
ansible -i hosts servers -m ping
Let's fetch and use a hostvar this time
ansible -i hosts localhost -m shell -a "echo {{ec2_tag_Name}}"
Ansible modules is a logical unit in ansible, which when specified with required parameters, can perform certain action.
Ansible playbooks is the language of ansible. It allows you to design plans for orchestration, config management, deployment.
A simple playbook file looks like this. This one has a single play. Let's create a playbook.yml file in our testansible dir:
---
- name: playbook to setup apache
hosts: server
sudo: yes
tasks:
- name: ensure apache is at the latest version
apt: name=apache2 state=latest update_cache=yes
- name: ensure apache is running
service: name=apache2 state=started
We can execute a playbook on a host like this:
ansible-playbook -i hosts playbook.yml
---
- name: playbook to setup apache
hosts: server
sudo: yes
vars:
http_port: 80
vars_files:
- vars_files/apache.yml
tasks:
- name: ensure apache is at the latest version
apt: name=apache2 state=latest update_cache=yes
tags: install_apache
- name: copy apache conf
template: src=site.conf.j2 dest=/etc/apache2/sites-enabled/site.conf mode=0640 owner=apache group=apache
notify: restart apache
- name: ensure apache is running
service: name=apache2 state=started
handlers:
- name: restart apache
service: name=apache2 state=restarted
Roles are abstracted logical grouping of tasks, handlers and vars.
---
- name: playbook to setup apache
hosts: server
sudo: yes
#pre_tasks:
roles:
- apache
#post_tasks:
Roles directory structure looks something like this
$ tree roles/apache
roles/apache
├── README.md
├── defaults
│ └── main.yml
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── tasks
│ └── main.yml
├── files
│ └── apache2.conf
├── templates
│ └── site.conf.j2
└── vars
└── main.yml
create roles directory inside your testansible
mkdir -p roles/apache/{defaults,tasks,handlers,templates}
add this to roles/apache/defaults/main.yml
http_name: john lennon
add this to roles/apache/tasks/main.yml
- name: ensure apache is at the latest version
apt: name=apache2 state=latest update_cache=yes
- name: copy test html page
template: src=test.html.j2 dest=/var/www/test.html mode=0644 owner=root group=root
notify: restart apache
- name: ensure apache is running
service: name=apache2 state=started
add this to roles/apache/handlers/main.yml
- name: restart apache
service: name=apache2 state=restarted
add this to roles/apache/templates/test.html.j2
<html>
<head>
<title>{{http_name}}'s Page</title>
</head>
<body>
<p>This is {{http_name}}'s webpage</p>
</body>
</html>
Once you have setup all your create a apache.yml playbook in your testansible directory
- name: playbook to setup apache
hosts: server
sudo: yes
roles:
- apache
Execute your playbook using:
ansible-playbook -i hosts apache.yml -e "http_name=ayush"
You can lookup documentation for modules using ansible-doc utility
list all modules
ansible-doc -l
check documentation of a module using:
ansible-doc apt
Although not hard to do and explain, loops and conditionals can be setup in ansible playbooks using with_items and when keywords Checkout reference for ansible documentation link for it.
You can have encrypted vars files. There is a utility ansible-vault to encrypt a vars files
Vagrant has an ansible provisioner facility, which can generate the ansible inventory file and run playbook with just vagrant up
Looks something like this:
config.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
ansible.tags = "logrotate"
ansible.groups = {
"group1" => ["machine1"],
"group2" => ["machine2", "machine3"],
"all_groups:children" => ["group1", "group2", "group3"],
"group1:vars" => { "variable1" => 9, "variable2" => "example" }
}
end
World Peace. Maybe?