Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Introduction to Ansible Setup in Ubuntu 14.04

DZone's Guide to

Introduction to Ansible Setup in Ubuntu 14.04

How to use Docker with Ansible on Ubuntu 14.04.

· DevOps Zone
Free Resource

Download “The DevOps Journey - From Waterfall to Continuous Delivery” to learn about the importance of integrating automated testing into the DevOps workflow, brought to you in partnership with Sauce Labs.

Ansible is a configuration management and provisioning tool, similar to Chef, Puppet, or Salt. It uses SSH to connect to servers and run the configured Tasks.

Installing Ansible in Ubuntu 14.o4

sudo apt-add-repository -y ppa:ansible/ansible
sudo apt-get update
sudo apt-get install -y ansible

Managing Servers

Ansible has a default inventory file used to define which servers it will be managing. After installation, there's an example one you can reference at /etc/ansible/hosts.

First, copy and move the default one so I can reference it later:

sudo mv /etc/ansible/hosts /etc/ansible/hosts.orig

Then we create our own inventory file from scratch. After moving the example inventory file, create a new /etc/ansible/hosts file, and define some servers to manage. Here we'll define two servers under the “web” label:

[web]
200.168.65.204
200.168.63.215

Edit ansible.cfg

Edit ansible.cfg and uncomment ask_sudo_pass=yes  for performing sudo operations in server. 

Basic: Running Commands

Once we have an inventory configured, we can start running Tasks against the defined servers.
Ansible will assume you have SSH access available to your servers, usually based on SSH-Key.

ansible 200.168.65.204 -m ping -s -k -u nandaraj 

In the case of one server

ansible all -m ping -s -k -u nandaraj 

Output

200.168.65.204 | success >> {
    "changed": false,
    "ping": "pong"
}
  • all - Use all defined servers from the inventory file.

  • -m ping - Use the “ping” module, which simply runs the ping command and returns the results.

  • -s - Use “sudo” to run the commands.

  • -k - Ask for a password rather than use key-based authentication.

  • -u nandaraj - Log into servers using user nandaraj.

Modules

Ansible uses “modules” to accomplish most of its Tasks. Modules can do things like install software, copy files, use templates.

Modules are the way to use Ansible, as they can use available context (“Facts”) in order to determine what actions, if any need to be done to accomplish a Task.
If we didn't have modules, we'd be left running arbitrary shell commands like this:

ansible all -s -m shell -a 'apt-get install nginx'

Here, the sudo apt-get install nginx command will be run using the “shell” module. The -a flag is used to pass any arguments to the module. -s to run this command using sudo.

Ansible modules ensure indempotence — we can run the same Tasks over and over without affecting the final result.

For installing software on Debian/Ubuntu servers, the “apt” module will run the same command, but ensure idempotence.

ansible all -s -m apt -a 'pkg=nginx state=installed update_cache=true'

This will use the apt module to update the repository cache and install Nginx (if not installed). Going over the commands:

  • all — Run on all defined hosts from the inventory file.

  • -s — Run using sudo.

  • -m apt — Use the apt module.

  • -a 'pkg=nginx state=installed update_cache=true' — Provide the arguments for the apt module, including the package name, our desired end state and whether to update the package repository cache or not.

All Modules

Refer to this link for all modules details: http://docs.ansible.com/ansible/list_of_all_modules.html

Basic Playbook

Playbooks can run multiple Tasks and provide some more advanced functionality that we would miss out on using ad-hoc commands.
Playbooks and Roles in Ansible all use Yaml.

Create File nginx.yml(vi /etc/ansible/nginx.yml)

---
- hosts: 200.168.65.204
  tasks:
   - name: Install Nginx
     apt: pkg=nginx state=installed update_cache=true

Running Playbook

ansible-playbook -s nginx.yml -k -u nandaraj

Use use -s to tell Ansible to use sudo again, and then pass the Playbook file.

Handlers

A Handler is exactly the same as a Task (it can do anything a Task can), but it will run when called by another Task. This is useful for secondary actions that might be required after running a Task, such as starting a new service after installation or reloading a service after a configuration change

---
- hosts: 200.168.65.204
  tasks:
   - name: Install Nginx
     apt: pkg=nginx state=installed update_cache=true
     notify:
      - Start Nginx

  handlers:
   - name: Start Nginx
     service: name=nginx state=started

Ansible and Docker

Create an ansible yml file:

vi /etc/ansible/rawdocker.yml
- name: Deploy
  hosts: 200.168.65.204
  tasks:
    - name: Install docker-py
      pip: name=docker-py
    - name: Pulling  image:'{{tag}}'
      raw: docker pull ubuntu:'{{tag}}'
    - name: Stop conatiner
      raw: docker stop hopeful_brattain
    - name: Start container
      raw: docker run -d ubuntu

Running the rawdocker.yml file with passing variables on the command line:

ansible-playbook -s rawdocker.yml --extra-vars "tag=15.10" -k -u nandaraj

Discover how to optimize your DevOps workflows with our cloud-based automated testing infrastructure, brought to you in partnership with Sauce Labs

Topics:
dev ops ,ansible ,docker ,devops tools ,infrastructure as a code

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}