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!
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.
I found some impressive case studies:
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.
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
- Download and Install
- Add an OS image for VirtualBox
- Find an OS image
I'd like to use
ubuntu/trusty64
-
Add the box
bash vagrant box add ubuntu/trusty64 -
Check whether it's installed
bash vagrant box list -
Make the work directory
mkdir -p ~/vagrant/ubuntu && cd "$_"- Initialize the image
vagrant init ubuntu/trusty64You'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!
- Setting password
Default password is
vagrant. If you want to change it,
passwd- Let's create the RSA Key Pair
ssh-keygen -t rsaIt 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.3I 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- Check config file and inventory file exist
- Config file:
/etc/ansible/ansible.cfg - Inventory file:
/etc/ansible/hosts
- Add localhost as a host Add this line at the end of the inventory file.
127.0.0.1
- Copy SSH key to localhost
ssh-copy-id vagrant@localhost
- Connect to localhost via SSH
ssh vagrant@localhost
- Let's test Ansible
- Type the command
ansible all -m ping --ask-pass
-
You should see:
127.0.0.1 | SUCCESS => { "changed": false, "ping": "pong" } -
Use SSH-agent to skip typing password each time
ssh-agent bash
ssh-add ~/.ssh/id_rsa- Let's test
ansible all -a "/bin/echo hello"- 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'
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