Table of Contents
Introduction
In this tutorial, I demonstrate how to use Docker for hands-on Ansible automation. Learn how to leverage Docker in a DevOps workflow and streamline configuration management with Ansible. A practical guide for DevOps professionals and beginners.
DevOps Use Docker
My Laptop Setup:
- Operating System: Windows 10
- Tools: Docker and Docker Compose
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.
- hosts the file describes the target server running
- 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
Link Youtube
Conclusion
Using Docker with Ansible in DevOps simplifies automation and improves efficiency. By integrating these powerful tools, you can streamline your deployment and configuration management processes. Thank you for visiting the DevOpsRoles.com page.
Please next time write your sentences this way :
subject verb complement object
The purpose of a tutorial is to help people to understand.