Towards Ansible -> Test Machine Setup and Tool Familiarization

Share on:

In last post, we learnt what is ansible and why we need tool like ansible. We also setup the development environment. In this post we will setup a test machine using vagrant and get familiarized with ansible config and a few commands.

Verify VirtualBox and Vagrant Installation

Verify virtualbox and vagrant installation

1slashpai@pai  ~/vagrant/ansible  (|kind-pai:default) VBoxManage --version
26.1.4r136177
3slashpai@pai  ~/vagrant/ansible  (|kind-pai:default) vagrant --version
4Vagrant 2.2.7

Create vagrant test machine

  • Create a directory to store vagrant configs and cd to that directory

  • Run vagrant init centos/7. You can see more options for vagrant boxes here

    1slashpai@pai  ~/vagrant/ansible  (|kind-pai:default) vagrant init centos/7
    2==> vagrant: A new version of Vagrant is available: 2.2.10 (installed version: 2.2.7)!
    3==> vagrant: To upgrade visit: https://www.vagrantup.com/downloads.html
    4
    5A `Vagrantfile` has been placed in this directory. You are now
    6ready to `vagrant up` your first virtual environment! Please read
    7the comments in the Vagrantfile as well as documentation on
    8`vagrantup.com` for more information on using Vagrant.
    
  • We need to update network config of Vagrantfile to get a host accessible private ip

  • Open Vagrantfile in a editor and uncomment this line config.vm.network "private_network", ip: "192.168.33.10". You can choose a different ip address if wanted. I am using same that is generated by vagrant init command.

  • This is how Vagrantfile will look like after removing all other commented lines. I removed that to save some space in this post.

    1# -*- mode: ruby -*-
    2# vi: set ft=ruby :
    3
    4Vagrant.configure("2") do |config|
    5  config.vm.box = "centos/7"
    6  config.vm.network "private_network", ip: "192.168.33.10"
    7end
    
  • Run vagrant up to create this machine

  • Run vagrant ssh from the same directory to ssh to the machine created

  • Your test machine is ready now

Ansible Configuration

  • Create a directory /etc/ansible in your machine. This is to store default ansible configs and to use as default location for ansible artifacts

  • Download ansible.cfg and store it in /etc/ansible

  • Run bat ansible.cfg if you have installed bat or simply run cat ansible.cfg to see configs

  • We will configure some default values in this file. We are doing this step here so that we don’t have to pass them explicitely when run ansible commands. ansible uses this as default values for connecting to machine via ssh.

    • Update remote_user as vagrant

    • Update private_key_file as vagrant machine Identity file path. You can get this path if you run vagrant ssh-config from same directory where Vagrantfile exists

       1 slashpai@pai  ~/vagrant/ansible  (|kind-pai:default) vagrant ssh-config
       2 Host default
       3  HostName 127.0.0.1
       4  User vagrant
       5  Port 2222
       6  UserKnownHostsFile /dev/null
       7  StrictHostKeyChecking no
       8  PasswordAuthentication no
       9  IdentityFile /home/slashpai/vagrant/ansible/.vagrant/machines/default/virtualbox/private_key
      10    IdentitiesOnly yes
      11    LogLevel FATAL
      
  • Create a host inventory file where we list down nodes we want to manage

    • Create a file named hosts under /etc/ansible with following contents. The IP specified here is the ip address of vagrant machine we created earlier.

      1[vagrant]
      2192.168.33.10
      

      [vagrant] is the node group name, you can give anything you like. So based on node usage we can create different groups. We will see more complex scenarios later. For now this is good enough

Familiarize with Ansible

Now let’s get familiarized with ansible

  • List all/specific group hosts in inventory

    1ansible all/group name--list-hosts
    
     1slashpai@pai  ~  ( |kind-pai:default) ansible all --list-hosts
     2hosts (1):
     3  192.168.33.10
     4slashpai@pai  ~  ( |kind-pai:default) ansible vagrant --list-hosts
     5hosts (1):
     6  192.168.33.10
     7slashpai@pai  ~  ( |kind-pai:default) ansible unknowngroup --list-hosts
     8[WARNING]: Could not match supplied host pattern, ignoring: unknowngroup
     9[WARNING]: No hosts matched, nothing to do
    10  hosts (0):
    
  • Test connectivity to test machine using ping module

     1slashpai@pai  ~  ( |kind-pai:default) ansible all -m ping
     2192.168.33.10 | SUCCESS => {
     3    "ansible_facts": {
     4        "discovered_interpreter_python": "/usr/bin/python"
     5    },
     6    "changed": false,
     7    "ping": "pong"
     8}
     9slashpai@pai  ~  ( |kind-pai:default) ansible vagrant -m ping
    10192.168.33.10 | SUCCESS => {
    11    "ansible_facts": {
    12        "discovered_interpreter_python": "/usr/bin/python"
    13    },
    14    "changed": false,
    15    "ping": "pong"
    16}
    17slashpai@pai  ~  ( |kind-pai:default)
    
  • Run adhoc commands

    • Adhoc commands are used to run single task on one or more nodes. This is very handy when you need to check something or do a simple task on nodes. The default module for the ansible command-line utility is the ansible.builtin.command module

      1ansible [pattern] -m [module] -a "[module options]"
      
      • pattern here means host group name pattern

    Example:

    1slashpai@pai  ~  ( |kind-pai:default) ansible vagrant -a uptime
    2192.168.33.10 | CHANGED | rc=0 >>
    311:18:18 up  1:23,  1 user,  load average: 0.00, 0.01, 0.02
    4slashpai@pai  ~  ( |kind-pai:default)
    5 slashpai@pai  ~  ( |kind-pai:default) ansible vagrant -a "cat /etc/redhat-release"
    6192.168.33.10 | CHANGED | rc=0 >>
    7CentOS Linux release 7.8.2003 (Core)
    8slashpai@pai  ~  ( |kind-pai:default)
    

    Now let’s try to run a command which needs privileged access. We got error because vagrant user doesn’t have access to run the command

    1slashpai@pai  ~  ( |kind-pai:default) ansible vagrant -a "cat /etc/sudoers.d/vagrant"
    2192.168.33.10 | FAILED | rc=1 >>
    3cat: /etc/sudoers.d/vagrant: Permission deniednon-zero return code
    
    • To fix the error use --become flag which runs command in sudo mode

      1slashpai@pai  ~  ( |kind-pai:default) ansible vagrant -a "cat /etc/sudoers.d/vagrant" --become
      2192.168.33.10 | CHANGED | rc=0 >>
      3%vagrant ALL=(ALL) NOPASSWD: ALL
      4slashpai@pai  ~  ( |kind-pai:default)
      

In next post we will see ansible terminologies, ansible playbook and how to write a playbook using the concepts learnt

comments powered by Disqus