Ansible Vagrant beginners guide

Ansible Vagrant beginners guide
I wrote this little tutorial for a collegue, and thought, why not share it with everyone else.
So with this tutorials you’ll a very basic Ansible setup running with Vagrant.
I’m assuming you have some prior experience with Vagrant, and have it already installed.

Ansible

Requirements:

  • Vagrant
  • Vagrant box (Ubuntu or Centos)
  • Basic knowledge of Linux

Preperation

To initialize Vagrant, go to your desired Vagrant directory(in my case /home/username/vagrant), and run a Vagrant init
Open your Vagrantfile, and paste the following config:

BOX_IMAGE = "ubuntu/xenial64"
NODE_COUNT = 2

Vagrant.configure("2") do |config|
  config.vm.define "master" do |subconfig|
    subconfig.vm.box = BOX_IMAGE
    subconfig.vm.hostname = "master"
    subconfig.vm.network :private_network, ip: "10.0.0.10"
    config.vm.provision "shell", inline: <<-SHELL
     apt-get update
     apt-get install -y ansible
   SHELL
  end

  (1..NODE_COUNT).each do |i|
    config.vm.define "node#{i}" do |subconfig|
      subconfig.vm.box = BOX_IMAGE
      subconfig.vm.hostname = "node#{i}"
      subconfig.vm.network :private_network, ip: "10.0.0.#{i + 10}"
    end
  end
end

This should get you up and running with 1 master node (Ansible node) and 2 slaves. In the script above the Ansible packages are automatically installed on the Master.
Run vagrant up To start all machines.
After everything is finished and up and running it’s time to get your hand dirty and start configuring Ansible.

Getting started

Ansible communicates with it’s nodes via SSH. That’s why we need to configure our ssh key from our master to our slaves. We’re using the authorized_keys file for this.

Login to the master node:
vagrant ssh master

Become root:
sudo su -

First let’s check if Ansible installed successfully:
ansible —-version

The output should be something like this:

root@master:/home/ubuntu# ansible --version
ansible 2.0.0.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides

If your output was similar, please continue. Otherwise try reinstalling Ansible apt-get update and then apt-get install ansible
If this doesn’t work, please check your internet connection.

Now we need to create a ssh key pair. To do this, run the following command:
ssh-keygen -t rsa
Just leave everything as default, and hit enter. Don’t set a password either (this is a testing environment)
Output should be something like this:

root@master:/home/ubuntu# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:HIYPk8SinANTQqaR6ZgVVqRGAj75dKHFu3o5FBsICLw root@master
The key's randomart image is:
+---[RSA 2048]----+
|%*=+++.          |
|XOo.+ooo         |
|+X==.o* o        |
|oEO..+ * .       |
|   o  = S        |
|     +           |
|    o .          |
|   . +           |
|    . .          |
+----[SHA256]-----+

Okay great! Now it’s time to save your public key in a notepad (or in my case Atom 😁)
Run the command:
cat ~/.ssh/id_rsa.pub
And copy/paste the output to your text editor.

Now login to the first node called node1 via a new terminal window:
vagrant ssh node1

Become root:
sudo su -

Now edit the file autorized_keys and paste the saved public key from earlier.
vi ~/.ssh/authorized_keys

Paste the key, and save and quit.

Now let’s get the ip of the node, run the following command:
ip a

The output should be something like this:

ubuntu@node1:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 02:e5:38:c9:4b:72 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::e5:38ff:fec9:4b72/64 scope link 
       valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:a3:5d:80 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.11/24 brd 10.0.0.255 scope global enp0s8
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fea3:5d80/64 scope link 
       valid_lft forever preferred_lft forever

Note that we configured a private network in our Vagrant file, so you should pick 10.0.0.x
In my case 10.0.0.11

Now go back to the master node in your other terminal window, and see if you can ssh into node1 using the ip.

As root user run the following command:
ssh 10.0.0.11
You should get a message if you want to accept the fingerprint id, hit yes:

root@master:/home/ubuntu# ssh 10.0.0.11
The authenticity of host '10.0.0.11 (10.0.0.11)' can't be established.
ECDSA key fingerprint is SHA256:Jh/qFOKESgDurbcqupSg6KdZy1rgAUlZ4T5NqeFrJtQ.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.11' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-78-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.


Last login: Tue May 23 07:58:42 2017 from 10.0.2.2
root@node1:~# 

Okay great! This is now working correctly.
Repeat the steps we just did (adding the key of the master node to the autorized_keys) on node2. If you’ve done this, please continue.

Getting started with Ansible

So now we’ve got the basics covered, it’s time to get started with Ansible.
First we need to tell Ansible what hosts are in our pool. To do so, we need to edit the file /etc/ansible/hosts as root on the master.
I created a test group in which I placed the nodes:

[test]
10.0.0.11
10.0.0.12

Now it’s time to test if all that hard work was worth it.
Run the following command as root to see if Ansible can connect to the hosts: ansible all -m ping
If everything went ok you should see the following message:

root@master:/home/ubuntu# ansible all -m ping
10.0.0.12 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
10.0.0.11 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

Wooohooo, you did it.
Now it’s time to continue doing cool stuff, if you enjoyed it please continue to round 2.

ROUND 2 AVAILABLE HERE: https://www.inpimation.com/ansible-round-2-awesome-stuff/

Sources/Helpfull link
Vagrant Tutorial – From Nothing To Multi-Machine | manski’s blog
Getting Started — Ansible Documentation

1 Comment

  1. Pingback: Ansible round 2, the awesome stuff - inpimation.com

Leave a comment

Your email address will not be published. Required fields are marked *