DEV Community

Cover image for Set up your own Ansible Lab using Docker
Rohit Kalyan Kandulapati
Rohit Kalyan Kandulapati

Posted on

Set up your own Ansible Lab using Docker

Hi there πŸ‘‹
This is Rohit and this is my first post on Dev.to

I know finding the right infra for learning Ansible is hard but not impossible. Today we are going to setup our own ansible lab in our system, which don't have any restrictions and any time limits.

Prerequisites: Docker in your system, Interest in you to learn Ansible.

Steps to setup Ansible lab:

1. Pulling the image

Pull the ubuntu image
docker pull ubuntu

Docker Images

2. Setting Up docker network

Create a docker network with a specific subnet
docker network create ansible-network --subnet 192.168.1.0/24

3. Setting up Ansible Master Container

a. Run the ubuntu image.
docker run -d -it --name ansible-master --net ansible-network --hostname ans_master ubuntu

b. Login into the container
docker exec -it ansible-master /bin/bash

c. Run the below commands

# 1
apt update

# 2
apt install vim python-is-python3 openssh-client iputils-ping ansible -y

# 3
ssh-keygen
# you will be prompted for responses, just keep clicking enter.
Enter fullscreen mode Exit fullscreen mode

4. Setting up Manage Container-1

You can use the below setup to add as many managed nodes as you want, you just need to change the container name.

a. Run the ubuntu image
docker run -d -it --name ansible-manage-1 --net ansible-network --ip 192.168.1.18 --hostname ans_master ubuntu

b. Login in to the container
docker exec -it ansible-manage-1 /bin/bash

c. Run the below commands

# 1
apt update

# 2
apt install vim python-is-python3 openssh-client openssh-server -y 

# 3
apt update

# 4
service ssh start
Enter fullscreen mode Exit fullscreen mode

5. Password-less authentication

a. Copy the contents of .pub file under the path ~/.ssh/ on your control container.
b. Add that to ~/.ssh/authorized_keys in your managed containers. Create the file if that file is not present and you should do the same for all your managed containers.
c. Try running the below command in your control container.
ssh root@ans_manage
Enter yes.

Now you have successfully setup password-less authentication from your control container to managed container.

6. Validate using ping module

a. Create and inventory.ini file in your control node under /home/ubuntu and add the below contents. (Make sure you have exited from the ssh connection)

192.168.1.18 ansible_user=root
Enter fullscreen mode Exit fullscreen mode

b. Run the below command
ansible -i /home/ubuntu/inventory.ini -m ping all
Just enter for one more time(last time).

Ping

Happy Learning πŸŽ‰πŸŽ‰

Let us know in the comments if you find any difficulties.

Top comments (0)