DevOps Use Docker to hands-on Ansible

In this tutorial, I use Docker to hands-on Ansible. Now, let’s go DevOps Use Docker.

DevOps Use Docker

My Laptop Environment

  • Windows 10
  • Docker and Docker-compose
DevOps Use Docker to hands-on Ansible

By using Docker to create one Ansible container and Server01 and Server02 containers. From Ansible command is executed in Ansible container to Two target container.

Let go use Docker to hands-on Ansible

Directory Structure

├── Docker
│   ├── Ansible_Control_node
│   │   └── Dockerfile      
│   └── Target_Server
│       └── Dockerfile      
├── docker-compose.yml      
├── hosts            
└── playbook.yml

Explain File and Directory Structure

I will not explain it to Docker because it is out of this post. The basic file of Ansible.

  1. hosts the file describes the target server running
  2. playbook.yml the file I will create a new file is devopsroles.txt for two targets.

The content of files as below

docker-compose.yml file

version: '3'
services:
  ansible:
    container_name: ansible
    build: ./Ansible_Control_node
    tty: true
    working_dir: "/var/data"
    volumes:
      - .:/var/data

  server01:
    container_name: server01
    build: ./Target_Server
    tty: true
  server02:
    container_name: server02
    build: ./Target_Server
    tty: true

playbook.yml file

- hosts: target
  tasks:
  - name: "Create new file devopsroles.txt"
    shell: |
        touch devopsroles.txt

hosts file

[target]
server01
server02

Ansible_Control_node/Dockerfile file

FROM centos
ENV ANSIBLE_HOST_KEY_CHECKING False
RUN yum install epel-release -y && \
    yum update -y && \
        yum install -y openssh-server openssh-clients net-tools && \
    yum install -y ansible
CMD /bin/bash

Target_Server/Dockerfile file

# Centos image latest
FROM centos:latest

# Install OpenSSh server with yum
RUN yum -y install openssh-server openssh-clients

# Created because public key is required when starting sshd
RUN ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa
RUN ssh-keygen -f /etc/ssh/ssh_host_ecdsa_key -N '' -t ecdsa

# Allow login as root
RUN sed -ri 's/^#PermitEmptyPasswords no/PermitEmptyPasswords yes/' /etc/ssh/sshd_config

# Specify root password
RUN echo "root:" | chpasswd

EXPOSE 22

# Start sshd
CMD ["/usr/sbin/sshd", "-D"]

Start Ansible and two target containers.

docker-compose up -d

Connect to Ansible container

docker exec -it ansible /bin/bash

SSH connection without password from Ansible container to Two target container.

	ssh server01
	exit
	ssh server02
	exit

Run the Ansible command.

ansible-playbook -i hosts playbook.yml

Execution result of Ansible

DevOps Use Docker

Link Youtube

DevOps Use Docker to hands-on Ansible. Thank you for reading the DevOpsRoles.com page.

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

1 thought on “DevOps Use Docker to hands-on Ansible

  1. Please next time write your sentences this way :
    subject verb complement object
    The purpose of a tutorial is to help people to understand.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.