To practice ansible, you need to have at least 2 machines. I suggest using containers rather than VM, since containers can be quickly spawned, and are light on the resources.
First, make sure you have docker Community Edition installed. If not, follow the install guide here.
Check your docker version
# docker version
Start the docker engine
# sudo systemctl start docker
In this exercise, we will use ubuntu as our base operating system. So, run a container using the ubuntu image from docker hub. The options are -i for interactive, -t to allocate pseudo TTY and -d to run the container in the background
# docker run -it -d --name="ansible-master" ubuntu
The ubuntu image does not come with ssh, which is needed for ansible, so we need to install that, together with vim text editor
# docker exec -it apt update; apt install vim openssh-server -y
Change the root password
# docker exec -it ansible-master passwd
Permit root login for ssh
# docker exec -it ansible-master /bin/bash
ansible-master: # cat >> /etc/ssh/sshd_config <<EOF
PermitRootLogin yes
EOF
Start ssh
ansible-master: # service ssh start; exit
Create an image based on ansible-master. This image will be used later to create ansible-client1 container
# docker commit -m "ubuntu with vim and openssh-server" ansible-master myubuntu:2019041001
Run a container called ansible-client1 from the image created above
# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
myubuntu 2019041001 17f43a3ef384 10 minutes ago 265MB
# docker run -d -it --name="ansible-client1" myubuntu:2019041001
Start ssh service on ansible-client1
# docker exec -it ansible-client1 service ssh start
Try to ssh into both machines. Get the ip address of the containers using "docker inspect" command
# docker inspect ansible-client1 | grep -w IPAddress
"IPAddress": "172.17.0.3",
"IPAddress": "172.17.0.3",
# docker inspect ansible-master | grep -w IPAddress
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
# ssh root@172.17.0.2
# ssh root@172.17.0.3
Install ansible on ansible-master
# docker exec -it ansible-master apt install ansible -y
Check ansible version
# docker exec -it ansible --version
Create ssh-key without password
# docker exec -it ssh-keygen
Transfer the key to ansible-client1
# docker exec -it ssh-copy-id 172.17.0.3
Edit /etc/ansible/hosts to include all nodes
# docker exec -it ansible-master /bin/bash
ansible-master: # cat >> /etc/ansible/hosts <<EOF
localhost
ansible-client1 ansible_host=172.17.0.3
[all]
localhost
ansible-client1
EOF
Test ansible using ping module
# docker exec -it ansible-master -m ping all
localhost | SUCCESS => {
"changed": false,
"ping": "pong"
}
ansible-client1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
Congratulations, now you have your own mini ansible lab, using docker. You can add more clients as you wish later.
No comments:
Post a Comment