【简洁篇】使用Docker(yum,pip)安装Ansible自动化运维工具,Dockerfile一步到位,稳打稳扎

继上篇在虚拟机中安装Ansible,现在我们用docker来安装一下,这里只讲安装步骤,不过多叙述相关概念与基础配置。其余相关知识大家可以参考前篇文章,见下方链接。

虚拟机centos安装,及ansible介绍,环境配置

我这里介绍两种方式

一、基础安装

二、dockerfile安装

目录

Docker基础安装ansible

yum方式安装ansible

pip3方式安装ansible

 Dockerfile安装ansible


一、Docker基础安装ansible

提前准备一台主机,装好docker容器

[root@ansible ~]# yum install -y docker
[root@ansible ~]# docker --version
Docker version 1.13.1, build 7d71120/1.13.1

配置一个镜像加速

针对Docker客户端版本大于 1.10.0 的可以通过修改daemon配置文件 /etc/docker/daemon.json 来使用加速器
[root@ansible ~]# sudo mkdir -p /etc/docker
[root@ansible ~]# sudo tee /etc/docker/daemon.json <<-'EOF'
{
   "registry-mirrors": ["https://6nntr2bf.mirror.aliyuncs.com"]
}
EOF

#最后别忘了重启docker
[root@ansible ~]# sudo systemctl daemon-reload
[root@ansible ~]# sudo systemctl restart docker

拉取镜像 

以Centos 7 Docker镜像为例,启动容器后,在容器中安装ansible
[root@ansible ~]# docker pull centos:7
Trying to pull repository docker.io/library/centos ...
7: Pulling from docker.io/library/centos
2d473b07cdd5: Pull complete
Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
Status: Downloaded newer image for docker.io/centos:7


查看镜像
[root@ansible ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos    7                   eeb6ee3f44bd        2 years ago         204 MB

 运行一个容器

[root@ansible ~]# docker run --rm -itd --name ansible centos:7
cae45ee9df21850523ed7692e390af2409469342a2234a3ccd5af259cfabd701

通过docker exec命令以交互式的方式进入容器 

[root@ansible ~]# docker exec -it ansible bash
[root@cae45ee9df21 /]#

  • yum方式安装ansible

进入容器内部,使用yum安装ansible

[root@cae45ee9df21 /]# yum install -y epel-release
[root@cae45ee9df21 /]# yum install ansible -y
[root@cae45ee9df21 /]# ansible --version
ansible 2.9.27
  config file = /etc/ansible/ansible.cfg
  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.5 (default, Oct 14 2020, 14:45:30) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]

如果这里使用yum安装失败可以使用python安装,没有失败当我没说,因为我也没有,但是多个方法多条路,学就完了!

  • pip3方式安装ansible

重新运行一个容器
[root@ansible ~]# docker run --rm -itd --name ansible1 centos:7
ed1de4758cec136dc514ce26f08faa03848c6f670967b673c773eb1aa161c344

进入容器
[root@ansible ~]# docker exec -it ansible1 bash

把pip3安装好
[root@ed1de4758cec /]# yum install python3 python3-pip

[root@ed1de4758cec /]# pip3 install --upgrade pip
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting pip
  Downloading https://files.pythonhosted.org/packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl (1.7MB)
    100% |################################| 1.7MB 19kB/s
Installing collected packages: pip
Successfully installed pip-21.3.1


[root@ed1de4758cec /]# pip3 install setuptools-rust
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
Collecting setuptools-rust
  Using cached setuptools_rust-1.1.2-py3-none-any.whl (21 kB)
Collecting semantic-version<3,>=2.8.2
  Using cached semantic_version-2.10.0-py2.py3-none-any.whl (15 kB)
Collecting typing-extensions>=3.7.4.3
  Using cached typing_extensions-4.1.1-py3-none-any.whl (26 kB)
Collecting setuptools>=46.1
  Downloading setuptools-59.6.0-py3-none-any.whl (952 kB)
     |################################| 952 kB 12 kB/s
Installing collected packages: typing-extensions, setuptools, semantic-version, setuptools-rust
  Attempting uninstall: setuptools
    Found existing installation: setuptools 39.2.0
    Uninstalling setuptools-39.2.0:
      Successfully uninstalled setuptools-39.2.0
Successfully installed semantic-version-2.10.0 setuptools-59.6.0 setuptools-rust-1.1.2 typing-extensions-4.1.1
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

控制台/终端必须使用UTF-8区域设置,而不是ASCII,这里如果不改变编码方式,就会报错,所以我为大家避坑了,你们的坑我来采,你们尽管看,将环境变量LC_CTYPE或LC_ALL设置为C.UTF-8或en_US.UTF-8。安装后,ansible可以与任何语言环境一起使用。只有安装需要UTF-8。
[root@ed1de4758cec /]# LC_CTYPE=en_US.UTF-8
[root@ed1de4758cec /]# export LC_CTYPE

现在开装
[root@ed1de4758cec /]# pip3 install ansible


安装完成查看版本
[root@ed1de4758cec /]# ansible --version
[DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the controller starting with Ansible 2.12. Current version: 3.6.8 (default, Nov 14 2023, 16:29:52) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)].
 This feature will be removed from ansible-core in version 2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
/usr/local/lib/python3.6/site-packages/ansible/parsing/vault/__init__.py:44: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography. The next release of cryptography will remove support for Python 3.6.
  from cryptography.exceptions import InvalidSignature
ansible [core 2.11.12]
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.6.8 (default, Nov 14 2023, 16:29:52) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
  jinja version = 3.0.3
  libyaml = True

pip安装ansible完成图

安装完成后,输入ansible再Tab键可以看见已经可以使用ansible命令 

至此, Docker基础安装ansible完成!

按下Ctrl+D组合键,退出容器

查看容器状态
[root@ansible ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
cae45ee9df21        centos:7            "/bin/bash"         10 minutes ago      Up 10 minutes                           ansible

停止容器
[root@ansible ~]# docker stop ansible
ansible

再次查看容器状态发现容器已经完全销毁,因为启动(创建)容器是加入了参数--rm
[root@ansible ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

二、Dockerfile安装ansible

编写dockerfile

先创建一下存放目录,咱是个喜欢管理的人
[root@ansible ~]# mkdir /ansible;cd /ansible

[root@ansible ansible]# vi dockerfile
from centos:7
workdir /etc/yum.repos.d/
run yum install -y wget
run wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
run yum clean all
run yum makecache
run yum install -y ansible openssh-server openssh-clients
run echo "000000" | passwd --stdin root            #密码改成自己的root密码
run ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N "" -q && \
ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N "" -q && \
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" -q
cmd /usr/sbin/sshd && tail -f /var/log/wtmp

定制名为ansible的镜像,镜像名字可以自定义,格式:docker build -t 自定义镜像名称 .
[root@ansible ansible]# docker build -t ansible .
Successfully built 9b48701d9a16



制作完成后查看镜像

[root@ansible ansible]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
ansible             latest              9b48701d9a16        About a minute ago   1.6 GB
docker.io/centos    7                   eeb6ee3f44bd        2 years ago          204 MB

使用ansible镜像运行一个容器(启动镜像)

[root@ansible ansible]# docker run -itd --rm ansible bash
a84150885740590a3f7e59aafac72c026be60ad479ebb5601149970025f43e38


查看容器,并进入
[root@ansible ansible]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
a84150885740        ansible             "bash"              54 seconds ago      Up 54 seconds                           gracious_ritchie
[root@ansible ansible]# docker exec -it a8 bash
[root@a84150885740 yum.repos.d]#
[root@a84150885740 yum.repos.d]# cd
[root@a84150885740 ~]# ansible
ansible               ansible-config        ansible-console-2     ansible-doc-2         ansible-galaxy-2      ansible-playbook      ansible-pull          ansible-vault
ansible-2             ansible-connection    ansible-console-2.7   ansible-doc-2.7       ansible-galaxy-2.7    ansible-playbook-2    ansible-pull-2        ansible-vault-2
ansible-2.7           ansible-console       ansible-doc           ansible-galaxy        ansible-inventory     ansible-playbook-2.7  ansible-pull-2.7      ansible-vault-2.7

查看版本
[root@a84150885740 ~]# ansible --version



至此,dockerfile安装ansible完成! 

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值