I tried running Ansible on a Docker container

Introduction

I had the opportunity to use Ansible in the field, so I tried running Ansible on the Docker container for studying.

Target

People who want to actually move Ansible

What is 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.

What I tried

File structure

.
├── ansible
│   └── install_git.yml
├── docker
│   ├── ansible
│   │   └── Dockerfile
│   └── target
│       └── Dockerfile
└── docker-compose.yml

File description

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

Run

  1. Start container
  2. Log in to the ansible server and run the playbook
  3. Make sure git is installed in the target container

1. Start container

#Container startup
~ % docker-compose up -d
Starting ansible_ansible_1 ... done
Starting ansible_target_1  ... done

2. Log in to the ansible server and run the playbook

#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

3. Make sure git is installed in the target container

#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

reference

https://tech-mmmm.blogspot.com/2018/08/ansible-ansibleplaybook.html

Recommended Posts

I tried running Ansible on a Docker container
I tried running a Docker container on AWS IoT Greengrass 2.0
I tried running Docker on Windows Server 2019
I tried deploying a Docker container on Lambda with Serverless Framework
I tried running Java on a Mac terminal
Run React on a Docker container
Run PureScript on a Docker container
I tried running WordPress with docker preview on M1 Mac.
Microservices 101-I tried putting Docker on Ubuntu-
I tried running Autoware
I made a Docker container to run Maven
I tried running the route search engine (OSRM) easily with a container
I tried installing docker on an EC2 instance
[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.
Create a Docker container for your development web server in Ansible on MacOS
I built a Java EE environment on AWS and tried running a web application
I tried using Log4j2 on a Java EE server
I tried using Docker Desktop for Windows on Windows 10 Home
I tried the Docker tutorial!
I tried BIND with Docker
I tried running a DB access application on IKS + Db2 on IBM Cloud (6. Preparation of DB access application (java))
I tried to create React.js × TypeScript × Material-UI on docker environment
Try Hello World using plain Java on a Docker container
I tried to create a padrino development environment with Docker
left4dead2 I made a Docker image for the server and tried running it on GCE # 3 (I had a hard time building the server)
I tried a little digdag docker.run_options
Run GUI application on Docker container
I tried installing CentOS 8 on ESXi 6.7
I tried using Pari gp container
I created a Docker image of a container for learning OpenAI Gym
I tried to create a Spring MVC development environment on Mac
I tried to build a laravel operating environment while remembering Docker
I tried a calendar problem in Ruby
I tried running the Angular sample in Auth0 Quick Start with Docker
Access MySQL on a Docker container from a local (host OS) Java program
Launch docker container on EC2 (personal memorandum)
Run NordVPN on Docker (Windows) Ubuntu container
I installed Docker on my Raspberry Pi 3
Directly operate mariadb running in Docker container
I tried to build a Firebase application development environment with Docker in 2020
Try building a GPU container on GCP.
I tried playing with BottomNavigationView a little ①
I made a calculator app on Android
I tried to build AdoptOpenjdk 11 on CentOS 7
What is Docker? I tried to summarize
Created a Docker container image for an OpenLDAP server based on Fedora
I tried migrating the portfolio created on Vagrant to the Docker development environment
I made a bulletin board using Docker 1
I tried embedding a formula in Javadoc
I tried to create a portfolio with AWS, Docker, CircleCI, Laravel [with reference link]
I tried to make a machine learning application with Dash (+ Docker) part3 ~ Practice ~
MYSQL Lock TimeOut error I don't remember while running RSpec on the container
Try running MPLS-VPN with FR Routing on Docker
I tried using Docker for the first time
Run GUI application on Docker container (Japanese input)
I tried using Junit on Mac VScode Maven
I tried putting Java on my Mac easily
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
Create a Lambda Container Image based on Java 15
[Docker] Check the running container and enter there
How to get a heapdump from a Docker container