Our First Ansible Playbook
Ansible is a configuration management and has the same breath as Chef, Puppet and Salt. When we talk configuration management, we are talking writing some configurations to ensure the state of our servers (packages installed, file permissions, services are running, etc). In this tutorial, we will install nginx
package to our server.
Let’s create our first playbook and we will use Vagrant as our test server. We also will use VirtualBox as our provider. Ensure we have Virtual Box and Vagrant installed in our machine.
# Virtual Box sudo apt-get install virtualbox-5.0 # Download vagrant
$ wget https://releases.hashicorp.com/vagrant/x.x.x/vagrant_x.x.x_x86_64.deb
$ sudo gdebi vagrant_x.x.x_x86_64.deb
Now, ensure we also have Ansible installed in our machine.
$ sudo apt-get install software-properties-common
$ sudo apt-add-repository ppa:ansible/ansible
$ sudo apt-get update
$ sudo apt-get install ansible
This is the final state of our workspace after this session.
playbooks-01
├── ansible.cfg
├── files
│ └── nginx.conf
├── hosts
├── templates
│ └── index.html.j2
├── Vagrantfile
└── web-notls.yml
Let’s begin!
$ mkdir playbooks-01
$ cd playbooks-01
$ vagrant init ubuntu/trusty64
$ vagrant up
Note: The first time we do vagrant up
, it will download the machine image.
Then, let’s create our hosts
file. But, check the ssh configuration first.
$ vagrant ssh-config# The output of ssh-config
Host default
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /home/teddyaryono/workspace/sandbox-ansible/playbooks-01/.vagrant/machines/default/virtualbox/private_key
IdentitiesOnly yes
LogLevel FATAL
Create our hosts
file.
$ nano hosts# Content of hosts file
[webservers]
testserver ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222
Ensure you can reach your vagrant server.
$ ansible testserver -m pingtestserver | SUCCESS => {
"changed": false,
"ping": "pong"
}
Then, create the rest of the files.
$ mkdir templates files
$ touch files/nginx.conf templates/index.html.j2
$ touch web-notls.yml# Content of nginx.conf
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;server_name localhost;location / {
try_files $uri $uri/ =404;
}
}# Content of index.html.j2
<html>
<head>
<title>Welcome to Ansible</title>
</head>
<body>
<h1>nginx, configured by Ansible</h1>
<p>If you can see this, Ansible successfully installed nginx.</p><p></p>
</body>
</html># Content of web-notls.yml
- name: Configure webserver with nginx
hosts: webservers
become: true
tasks:
- name: install nginx
apt: name=nginx update_cache=yes- name: copy nginx config file
copy: src=files/nginx.conf dest=/etc/nginx/sites-available/default- name: enable configuration
file: >
dest=/etc/nginx/sites-enabled/default
src=/etc/nginx/sites-available/default
state=link- name: copy index.html
template: src=templates/index.html.j2 dest=/usr/share/nginx/html/index.html mode=0644- name: restart nginx
service: name=nginx state=restarted
Finally, run the ansible-playbook
to begin the provisioning.
ansible-playbook web-notls.yml
Below is the sample output of our provisioning result.
Then, we can access the webserver in the guest machine from our host machine.
Cheers!