前言
流程
- 准备好需要通信的公钥私钥,准备个主机清单hosts:
vim ./hosts
[fe-servers]
xxxx.xx.x.x.x
xxxx.xx.x.x.x
FROM centos:7
# 安装必要依赖,openssh-clients是为了支持ssh连接
RUN yum -y install wget curl vim openssh-clients
RUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
RUN yum clean all
RUN yum makecache
# 拷贝公钥私钥进镜像内
COPY ssh /root/.ssh/
# 公钥私钥赋权
RUN chmod 755 ~/.ssh/
RUN chmod 600 ~/.ssh/id_rsa ~/.ssh/id_rsa.pub
# 安装 ansible
RUN yum -y install ansible
# 拷贝主机组清单进 ansible 目录
COPY hosts /etc/ansible/
# 关闭 known_hosts 校验
RUN sed -i 's/^#host_key_checking = False/host_key_checking = False/' /etc/ansible/ansible.cfg
RUN ansible --version
docker build -t ansible:t1 .
docker run -itd --name ansible ansible:t1
docker exec -it ansible ansible all -m command -a "chdir=~ docker ps"
- 如果连上说明ok,连不上需要自己去ansible机子里执行`ssh-copy-id -i .ssh/id_rsa.pub 用户名字@ip