Skip to content

Instantly share code, notes, and snippets.

@wataruoguchi
Last active September 10, 2016 04:10
Show Gist options
  • Select an option

  • Save wataruoguchi/b0790c2625151a7f52e4c30be5eef70c to your computer and use it in GitHub Desktop.

Select an option

Save wataruoguchi/b0790c2625151a7f52e4c30be5eef70c to your computer and use it in GitHub Desktop.
Getting Started With Ansible

Getting Started with Ansible

Happy long weekend! Some of my plans have been canceled due to the weather. The summer in Vancouver this year was insanely short.

Raincouver, I don't think most people like it but I. I think it is time to focus on studying, or to try something new inside!

This weekend, I am playing with Ansible.

The motivation why I am playing with it is that simply my company is using, and I still don't know what it is. The only thing I know is that my coworkers are all smart, awesome guys. And if they love to use Ansible, it must be a super awesome tool with no doubt. Do I sound silly? Whatever! Let me have a try!

What is Ansible?

If you have worked in a developer team, you might have said: "It is working in my environment but not yours!" If each developer is using a different version of the language, server, and any other software, it would happen. Or, have you set up multiple servers at once? It is hard and boring to install software on each server. You have to type the same command on each of them.

From my understanding, Ansible provides a solution for those cases.

Who uses Ansible?

I found some impressive case studies:

I am just a front end developer, why should I need to know?

When I was taking a web development course, I used WordPress couple of times. Develop a website in my local environment, upload the assets to a public server, as well as migrate the database. What if you can it automatically? It seems Ansible helps to do it.

I'm getting interested into it. How do I start?

As I don't want to mess up my local machine, let's start building a virtual environment. Using virtual environment is good for these reasons:

  • You can keep your local machine secure
  • You can destroy the environment when you want to start over
  • You can choose the OS you like
  • You can make an only environment for the project

Okay, let me start!

Build a virtual environment

  1. Download and Install
  1. Add an OS image for VirtualBox
  1. Add the box bash vagrant box add ubuntu/trusty64

  2. Check whether it's installed bash vagrant box list

  3. Make the work directory

mkdir -p ~/vagrant/ubuntu && cd "$_"
  1. Initialize the image
vagrant init ubuntu/trusty64

You'll see Vagrantfile got generated 5. Launch Vagrant and connect it via SSH

vagrant up
vagrant ssh

Note: Quit Vagrant

vagrant halt

Ta-da! Your own virtual environment is here!

Setup your virtual environment

  1. Setting password Default password is vagrant. If you want to change it,
passwd
  1. Let's create the RSA Key Pair
ssh-keygen -t rsa

It should be saved in /home/vagrant/.ssh/id_rsa 3. Install Ansible and dependencies

sudo apt-get -y install python-setuptools python-dev libffi-dev libssl-dev
sudo easy_install pip
sudo pip install paramiko PyYAML Jinja2 httplib2 six markupsafe docker-py ansible setuptools==11.3

I installed Ansible via pip, though it didn't generate Ansible's inventory file. So additinally,

sudo apt-get -y install software-properties-common
sudo apt-add-repository ppa:ansible/ansible
sudo apt-get update
sudo apt-get -y install ansible
  1. Check config file and inventory file exist
  • Config file: /etc/ansible/ansible.cfg
  • Inventory file: /etc/ansible/hosts
  1. Add localhost as a host Add this line at the end of the inventory file.
127.0.0.1
  1. Copy SSH key to localhost
ssh-copy-id vagrant@localhost
  1. Connect to localhost via SSH
ssh vagrant@localhost
  1. Let's test Ansible
  2. Type the command
ansible all -m ping --ask-pass
  1. You should see: 127.0.0.1 | SUCCESS => { "changed": false, "ping": "pong" }

  2. Use SSH-agent to skip typing password each time

ssh-agent bash
ssh-add ~/.ssh/id_rsa
  1. Let's test
ansible all -a "/bin/echo hello"
  1. You should see
127.0.0.1 | SUCCESS | rc=0 >>
hello

I like using shorthands. These lines are added in my ~/.profile

alias aconf='sudo vi /etc/ansible/ansible.cfg'
alias ahost='sudo vi /etc/ansible/hosts'

FYI

Date: 2016.09.03
Host: OS X El Capitan 10.11.6
      VirtualBox 5.1.4
      Vagrant 1.8.5
Client: Ubuntu 14.04.5 LTS
        ansible 2.1.1.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment