Ansible Demo, Do it your self
Ubuntu Based system
1. Setup Vagrant Machine (Optional):
Install Vagrant and VirtualBox
## VirtualBox
sudo apt-get update
sudo apt-get install virtualbox
wget https://releases.hashicorp.com/vagrant/1.8.1/vagrant_1.8.1_x86_64.deb
sudo dpkg -i vagrant_1.8.1_x86_64.deb
Add Ubuntu Vagrant box and init Vagrant File
mkdir work && cd work
vagrant init ubuntu/trusty64; vagrant up --provider virtualbox
SSH into Vagrant
2. SSH Keys and passwordless access
Generate your ssh keys
Copy public key for passwordless access
cd ~/.ssh
cat id_rsa.pub >> authorized_keys
Install OpenSSH packages
sudo apt-get update
sudo apt-get install openssh-server
Test your SSH connection
sudo apt-add-repository -y ppa:ansible/ansible
sudo apt-get update
sudo apt-get install -y ansible
First backup your inventory file
sudo mv /etc/ansible/hosts /etc/ansible/hosts.orig
Create a new inverntory file
sudo vim /etc/ansible/hosts
And add following content init
$ansible all -m ping
$ansible all -m setup
$ansible all -m command -a "ls"
$ansible all -m command -a "df -h"
$ansible all -m shell -a "ls"
$ansible all -m shell -a "ls | grep txt"
$ansible all -b --ask-become-pass -m apt -a "name=htop update_cache=yes"
6. Try running Nginx Playbook:
Create web.yml file
Add following content in it
---
- hosts: local
vars:
- docroot: /var/www/serversforhackers.com/public
tasks:
- name: Add Nginx Repository
apt_repository: repo='ppa:nginx/stable' state=present
register: ppastable
- name: Install Nginx
apt: pkg=nginx state=installed update_cache=true
when: ppastable|success
register: nginxinstalled
notify:
- Start Nginx
- name: Create Web Root
when: nginxinstalled|success
file: dest={{ '{{' }} docroot {{ '}}' }} mode=775 state=directory owner=www-data group=www-data
notify:
- Reload Nginx
handlers:
- name: Start Nginx
service: name=nginx state=started
- name: Reload Nginx
service: name=nginx state=reloaded
Run the playbook
ansible-playbook -b --ask-become-pass web.yml