Setup Ansible on docker containers

  1. free: It is a open source tool.
  2. Simple to use : No special coding skills are required to write ansible playbooks.
  3. Agentless : You don’t need to install any other software,firewalls,ansible on the worker nodes that you want to perform actions on.
  4. Powerful : Ansible let you automate complex IT workflows with the help of rich modules available.
$ docker — version
Docker version 18.03.1-ce, build 9ee9f40
$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
345e3491a907: Pull complete
57671312ef6f: Pull complete
5e9250ddb7d0: Pull complete
Digest: sha256:b4aa552dd3f2ed84f3214b0e8add3648aee0205ef58295c92fe0899f96ad8755
Status: Downloaded newer image for ubuntu:latest
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 7e0aa2d69a15 4 weeks ago 72.7MB
redis latest 4760dc956b2d 3 years ago 107MB
ubuntu <none> f975c5035748 3 years ago 112MB
alpine latest 3fd9065eaf02 3 years ago 4.14MB
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$ docker run -itd — name ansible_master ubuntu /bin/bash
d34f89c763f9a87ffe7cd723618133edc42eac3b530c7a93cf7030c041ce1cb9
$ docker run -itd — name ansible_node ubuntu /bin/bash
3c3bc9c76b203246ad5014142972abb0b41693f4edf473cf2a5df0501a4ead54
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3c3bc9c76b20 ubuntu “/bin/bash” 58 seconds ago Up 56 seconds ansible_node
d34f89c763f9 ubuntu “/bin/bash” 3 minutes ago Up 3 minutes ansible_master
$ docker attach d34f89c763f9
root@d34f89c763f9:/# apt update; apt install python ansible openssh-client vim iputils-ping -y
Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
$ docker attach 3c3bc9c76b20
root@3c3bc9c76b20:/#
root@3c3bc9c76b20:/#
root@3c3bc9c76b20:/# apt update;apt install ssh vim python -y
Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
root@ca07a033db25:/# passwd root
New password:
Retype new password:
passwd: password updated successfully
root@ca07a033db25:/# vi /etc/ssh/ssh_config
root@ca07a033db25:/# service ssh restart
* Restarting OpenBSD Secure Shell server sshd
`$ docker network inspect bridge
[
{
“Name”: “bridge”,
“Id”: “dd6c9c4d9825c74b829ca5911bf8cca2eb164fb1ca1b2afc2a243d41d0e5acbb”,
“Created”: “2021–05–27T06:52:56.066958456Z”,
“Scope”: “local”,
=======================================================
ping 172.18.0.3
PING 172.18.0.3 (172.18.0.3) 56(84) bytes of data.
64 bytes from 172.18.0.3: icmp_seq=1 ttl=64 time=0.104 ms
64 bytes from 172.18.0.3: icmp_seq=2 ttl=64 time=0.076 ms
root@0cf0f6c5b629:/# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
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:7jR/K8DMCj88dctvLXN75DJ4KYFGljYSwkuL4/24u8c root@0cf0f6c5b629
The key's randomart image is:
+---[RSA 3072]----+
| . |
| + . |
| o + . . |
| o o . * |
| . o +S= o |
| o ..* + . .|
| + *+= . + + |
| BoEo+ B B o|
| +B. .=oB.= |
+----[SHA256]-----+
root@0cf0f6c5b629:/# ssh-copy-id root@172.18.0.3
- hosts: webservers

become: yes

tasks:
- ping
[webservers]
foo.example.com
ansible-playbook ping.yaml -i inventory.txt

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akshay Bobade

Akshay Bobade

I have total 3 Plus years of experience as a Devops engineer and currently dealing with Cloud, Containers, Kubernates and Bigdata technologies.