我尝试在Docker容器上运行Ansible
首先( )
我在现场有机会使用Ansible,所以为了学习,在Docker容器上尝试运行了Ansible。
目标
想要亲自运行Ansible的人
Ansible是什么?
Ansible是一种工具,可以自动执行系统配置、软件部署等各种设置任务。
通过在名为Playbook的YAML格式文本文件中记录任务,并使用Ansible执行它,可以实现各种处理。
其优点有两点,如下所示:
-
- 設定ファイルがyamlでの記述になっておりシンプル
- 冪等性(何度同じ操作しても、同じ状態にする)を担保
尝试过的事情 de
-
- Ansibleコンテナと、環境を適用したいターゲットコンテナを立ち上げる
- Ansibleコンテナからplaybookを実行して、ターゲットコンテナにgitをインストールする
文件的组成。
.
├── ansible
│ └── install_git.yml
├── docker
│ ├── ansible
│ │ └── Dockerfile
│ └── target
│ └── Dockerfile
└── docker-compose.yml
文件说明
Docker/Ansible的Dockerfile
Ansible和SSH的配置。
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 的汉语释义。
为了从Ansible服务器进行ssh连接,需要配置ssh并启动sshd服务。
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
# sshdを起動
CMD ["/usr/sbin/sshd", "-D"]
docker-compose.yml文件
启动Ansible容器和目标容器
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
安装Git的Ansible脚本install_git.yml。
安装了Playbook文件和git。
- hosts: target
tasks:
- name: install git
yum: name=git state=latest
执行
-
- 一种可能的汉语翻译是:
启动容器
登录到ansible服务器并执行playbook
检查目标容器是否安装了git
1. 启动容器
# コンテナ起動
~ % docker-compose up -d
Starting ansible_ansible_1 ... done
Starting ansible_target_1 ... done
登录到ansible服务器并执行playbook。
# コンテナを表示
~ % 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
# Ansbleコンテナにログイン
~ % docker exec -it ansible_ansible_1 bash
# playbook実行
[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. 确认目标容器是否安装了git。
# ターゲットコンテナにログイン
~ % docker exec -it ansible_target_1 bash
# gitがインストールされているか確認
[root@362d7471778f /]# git --version
git version 2.27.0