HowTo Setup Ansible Practice Lab Using Single Virtual Machine

Now a days Ansible is the most popular automation tool in DevOps industry. If you want learn it there are lot of documentation and video tutorial on youtube, That’s fine but you definitely need an lab to practice. Practice makes man more perfect. Let’s see HowTo Setup Ansible Practice Lab Using Single Virtual Machine with Docker containers.

HowTo Setup Ansible Practice Lab Using Single Virtual Machine with Docker Containers

To Install and configure Oracle Virtual box refer this Guide. After setting up Virtual Box and it’s network properly then create an Virtual Machine and install operating system Like Ubuntu, Centos and Debian.

run virtualized containers you have to install Docker in your virtual machine 

# apt-get install docker.io #for Ubuntu


[root@Ansible-Master aravi]# yum install docker  #for Centos

Installed:
 docker.x86_64 2:1.12.6-68.gitec8512b.el7.centos

Dependency Installed:
 audit-libs-python.x86_64 0:2.7.6-3.el7 checkpolicy.x86_64 0:2.5-4.el7
 container-selinux.noarch 2:2.33-1.git86f33cd.el7 container-storage-setup.noarch 0:0.8.0-3.git1d27ecf.el7
 docker-client.x86_64 2:1.12.6-68.gitec8512b.el7.centos docker-common.x86_64 2:1.12.6-68.gitec8512b.el7.centos
 libcgroup.x86_64 0:0.41-13.el7 libsemanage-python.x86_64 0:2.5-8.el7
 oci-register-machine.x86_64 1:0-3.13.gitcd1e331.el7 oci-systemd-hook.x86_64 1:0.1.14-1.git1ba44c6.el7
 oci-umount.x86_64 2:2.3.0-1.git51e7c50.el7 policycoreutils-python.x86_64 0:2.5-17.1.el7
 python-IPy.noarch 0:0.75-6.el7 setools-libs.x86_64 0:3.3.8-1.1.el7
 skopeo-containers.x86_64 1:0.1.26-2.dev.git2e8377a.el7.centos yajl.x86_64 0:2.0.4-4.el7

Complete!

Enable and Start Docker Services

[root@Ansible-Master aravi]# systemctl status docker
 ● docker.service - Docker Application Container Engine
 Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
 Active: inactive (dead)

[root@Ansible-Master aravi]# systemctl enable docker
 Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

[root@Ansible-Master aravi]# systemctl start docker

[root@Ansible-Master aravi]# systemctl status docker
 ● docker.service - Docker Application Container Engine
 Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
 Active: active (running) since Tue 2018-01-16 05:52:05 EST; 6s ago
 Docs: http://docs.docker.com
 Main PID: 4090 (dockerd-current)
 CGroup: /system.slice/docker.service
 ├─4090 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupd...
 └─4097 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --shim docker-containerd-shim --metrics-interval...

[root@Ansible-Master aravi]# docker version
 Client:
 Version: 1.12.6
 API version: 1.24
 Package version: docker-1.12.6-68.gitec8512b.el7.centos.x86_64
 Go version: go1.8.3
 Git commit: ec8512b/1.12.6
 Built: Mon Dec 13 16:08:42 2017
 OS/Arch: linux/amd64

Server:
 Version: 1.12.6
 API version: 1.24
 Package version: docker-1.12.6-68.gitec8512b.el7.centos.x86_64
 Go version: go1.8.3
 Git commit: ec8512b/1.12.6
 Built: Mon Dec 12 16:08:42 2017
 OS/Arch: linux/amd64

Test docker is working fine

[root@Ansible-Master ~]# docker run hello-world
docker test hello world centos 7

docker test hello world centos 7

Search Docker Images

Docker image searching and finding required image is the key here

# docker search centos

Pull Docker image to local for installation

# docker pull docker.io/kinogmt/centos-ssh

Deploy Docker Image

# docker run -it -d docker.io/kinogmt/centos-ssh

Verify docker image is running correctly

[root@Ansible-Master ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
68aca7419d36 docker.io/kinogmt/centos-ssh "/bin/sh -c 'service " 9 seconds ago Up 5 seconds 22/tcp trusting_cray

To Know docker container IP Address run below command

[arkit]# docker inspect 68a |grep IPAddress
Docker Container IP Address

Docker Container IP Address

Reset user password because we are not known root user password of docker machine

# docker exec -u 0 -it 68a bash
Change Docker container Root User Password

Change Docker container Root User Password

In the same way as shown above repeat and install another docker containers with Ubuntu and Debian Operating systems

Deploy Ubuntu Docker container

Deploy Ubuntu Docker container

Preparing Ansible Master machine

Making ready of Ansible master machine we have install Ansible in base machine.

# yum install ansible

Check install ansible version

[root@Ansible-Master ~]# ansible --version
ansible 2.4.1.0
 config file = /etc/ansible/ansible.cfg
 configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
 ansible python module location = /usr/lib/python2.7/site-packages/ansible
 executable location = /usr/bin/ansible
 python version = 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]

edit ansible config file and change below parameter to false

[root@Ansible-Master ~]# cat /etc/ansible/ansible.cfg |grep host_key
host_key_checking = False

Sample Ansible Hosts File

[root@Ansible-Master ~]# cat /etc/ansible/hosts
localhost ansible_ssh_pass=redhat ansible_user=aravi
Client01 ansible_host=172.17.0.2 ansible_user=root ansible_ssh_pass=redhat
Client02 ansible_host=172.17.0.3 ansible_user=root ansible_ssh_pass=redhat

[all]
localhost
Client01
Client02

Now test ansible using ping module

Ansible command testing

Ansible command testing

that’s it ready to rock. Happy Automation. HowTo Setup Ansible Practice Lab using Docker Container.

Related Articles

First Step is Writing Playbooks

Access Inbuilt Help Documents

Tower Installation Step by Step Guide

Copy SSH keys to remote hosts for the first time

Docker docs

Thanks for your wonderful Support and Encouragement

blank

Ravi Kumar Ankam

My Name is ARK. Expert in grasping any new technology, Interested in Sharing the knowledge. Learn more & Earn More

Leave a Reply

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