I had the opportunity to use Ansible in the field, so I tried running Ansible on the Docker container for studying.
People who want to actually move Ansible
Ansible is a tool that automates various setting tasks such as system configuration and software deployment. A variety of processing is realized by describing a task in a YAML-formatted text file called Playbook and executing it with Ansible. The following two points can be mentioned as merits.
.
├── ansible
│ └── install_git.yml
├── docker
│ ├── ansible
│ │ └── Dockerfile
│ └── target
│ └── Dockerfile
└── docker-compose.yml
docker/ansible/Dockerfile Ansible and ssh settings
FROM centos:8
RUN yum update -y \
&& yum install -y epel-release \
&& yum install -y ansible \
&& yum -y install openssh-clients \
&& echo "target" >> /etc/ansible/hosts
docker/target/Dockerfile Set ssh and start sshd so that you can ssh from Ansible server
FROM centos:8
RUN yum -y update \
&& yum -y install openssh-server \
&& sed -ri 's/^#PermitRootLogin yes/PermitRootLogin yes/' /etc/ssh/sshd_config \
&& sed -ri 's/^UsePAM yes/UsePAM no/' /etc/ssh/sshd_config \
&& ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa \
&& ssh-keygen -f /etc/ssh/ssh_host_ecdsa_key -N '' -t ecdsa \
&& sed -ri 's/^#PermitEmptyPasswords no/PermitEmptyPasswords yes/' /etc/ssh/sshd_config \
&& echo "root:" | chpasswd
#Start sshd
CMD ["/usr/sbin/sshd", "-D"]
docker-compose.yml
Start Ansible container and target container
version: "3"
services:
ansible:
tty: true
working_dir:
/opt/ansible
build:
context: .
dockerfile: ./docker/ansible/Dockerfile
volumes:
- ./ansible:/opt/ansible
target:
tty: true
build:
context: .
dockerfile: ./docker/target/Dockerfile
ansible/install_git.yml Playbook file, git is installed
- hosts: target
tasks:
- name: install git
yum: name=git state=latest
#Container startup
~ % docker-compose up -d
Starting ansible_ansible_1 ... done
Starting ansible_target_1 ... done
#Show container
~ % docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
28f39d3e82f0 ansible_ansible "/bin/bash" About an hour ago Up 13 seconds ansible_ansible_1
362d7471778f ansible_target "/usr/sbin/sshd -D" About an hour ago Up 13 seconds ansible_target_1
#Log in to the Ansble container
~ % docker exec -it ansible_ansible_1 bash
#playbook run
[root@28f39d3e82f0 ansible]# ansible-playbook install_git.yml
PLAY [target] ********************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************************************************************
The authenticity of host 'target (172.22.0.2)' can't be established.
ECDSA key fingerprint is SHA256:YJSVW2y2ryrRuU0rn3I8onXAPMwS/k03uj+MNd5JqP0.
Are you sure you want to continue connecting (yes/no/[fingerprint])? y
Please type 'yes', 'no' or the fingerprint: yes
ok: [target]
TASK [install git] ***************************************************************************************************************************************************************************************
changed: [target]
PLAY RECAP ***********************************************************************************************************************************************************************************************
target : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
#Log in to the target container
~ % docker exec -it ansible_target_1 bash
#Check if git is installed
[root@362d7471778f /]# git --version
git version 2.27.0
https://tech-mmmm.blogspot.com/2018/08/ansible-ansibleplaybook.html
Recommended Posts