在alpine镜像中添加ansible服务的方法

2025-05-27 0 55

使用apk add ansible即可在alpine镜像中添加ansible服务

添加步骤

在alpine中使用ansible需要如下几步

在alpine镜像中添加ansible服务的方法

Dockerfile

在Dockerfile中添加上面的语句:

?

1

2

3

4

5

6

7

8

9

10

11
#update apk for install

RUN apk update

#install ansible and openssh

RUN apk add ansible openssh

#init ansible hosts file

RUN mkdir -p /etc/ansible

RUN echo "localhost" >/etc/ansible/hosts

#init rsa ssh key pair

RUN ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa

#delete cache files

RUN rm -rf /var/cache/apk/*

使用镜像

拉取镜像

大概103M左右

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17
[root@occluster ~]# docker pull liumiaocn/ansible

Using default tag: latest

Trying to pull repository docker.io/liumiaocn/ansible ...

latest: Pulling from docker.io/liumiaocn/ansible

ff3a5c916c92: Already exists

42a81541f983: Pull complete

86602ac17bcb: Pull complete

73cb14e49a3f: Pull complete

bdfcad59d746: Pull complete

455ffeaa5bf1: Pull complete

dff32643cfa5: Pull complete

Digest: sha256:46284f3d3b9e3880f9b65fa6b1fb225b19bc88222badf74aa85936aabc24090f

Status: Downloaded newer image for docker.io/liumiaocn/ansible:latest

[root@occluster ~]#

[root@occluster ~]# docker images |grep ansible

docker.io/liumiaocn/ansible latest 8a5cfdf8243f Less than a second ago 103 MB

[root@occluster ~]#

使用

进行版本确认

?

1

2

3

4

5

6

7

8

9
[root@occluster ~]# docker run -it --name ansible liumiaocn/ansible sh

/ # ansible --version

ansible 2.4.1.0

config file = None

configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']

ansible python module location = /usr/lib/python2.7/site-packages/ansible

executable location = /usr/bin/ansible

python version = 2.7.14 (default, Dec 14 2017, 15:51:29) [GCC 6.4.0]

/ #

与宿主机(192.168.163.172)进行沟通,事先设定ssh通路,可以使用ssh-copy-id

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
/ # ssh-copy-id 192.168.163.172

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"

The authenticity of host '192.168.163.172 (192.168.163.172)' can't be established.

ECDSA key fingerprint is SHA256:uh84Xw56LhiCXb1b0uFbX/CoFhkqHGZjtmLwl3qkTOY.

Are you sure you want to continue connecting (yes/no)? yes

/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

expr: warning: '^ERROR: ': using '^' as the first character

of a basic regular expression is not portable; it is ignored

/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys

root@192.168.163.172's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh '192.168.163.172'"

and check to make sure that only the key(s) you wanted were added.

/ #

ssh通路确认

?

1

2

3
/ # ssh 192.168.163.172 hostname

occluster

/ #

ansible动作确认

?

1

2

3

4

5

6

7

8
/ # echo "192.168.163.172" >>/etc/ansible/hosts

/ # ansible 192.168.163.172 -m ping

192.168.163.172 | SUCCESS => {

"changed": false,

"failed": false,

"ping": "pong"

}

/ #

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对快网idc的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/liumiaocn/article/details/80218294

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 行业资讯 在alpine镜像中添加ansible服务的方法 https://www.kuaiidc.com/68197.html

相关文章

猜你喜欢
发表评论
暂无评论