首页 > 其他 > 详细

Ansible自动化运维简介及批量管理方式

时间:2021-05-19 12:54:51      阅读:16      评论:0      收藏:0      [点我收藏+]

  自动化运维的优势是能提高工作效率,减少重复性的工作,可减少认为出错的可能,是数据化管理、数据化汇报、所有的问题可追溯,当然而减少企业运维人员的成本。

  Ansible 是一个可同时管理多个远程主机的软件。这里的远程主机是指任何可以通过SSH协议登录的主机,所以它既可以是远程虚拟主机或物理机,也可以是本地主机。Ansible通过SSH协议实现管理节点与远程节点的通信。理论上来说,只要是能通过SSH登录到远程主机来完成的操作,都可以通过Ansible实现批量自动化操作。

  Ansible 可以管理的操作有批量复制、批量删除、批量修改、批量查看文件和目录、批量安装软件、批量启动服务等。Ansible的编排引擎可以出色的完成配置管理,流程控制、资源部署等多方面的工作,与其他IT自动化产品相比较,Ansible可提供一种不需要安装客户端软件、管理简便、功能强大的基础架构配置、维护工具。

首先进行基本的环境安装并检查

[root@localhost ~]# yum install -y epel-release   #安装epel源
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com

---------------------------------------------------------------------------------------------------------------

省略部分安装信息

---------------------------------------------------------------------------------------------------------------

验证中 : epel-release-7-13.noarch 1/1

已安装:
epel-release.noarch 0:7-13

完毕!
[root@localhost ~]# yum install -y ansible       #安装批量管理软件Ansible
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile

----------------------------------------------------------------------------------------------------------------

省略部分安装信息

-----------------------------------------------------------------------------------------------------------------

python-markupsafe.x86_64 0:0.11-10.el7 python-paramiko.noarch 0:2.1.1-9.el7
python2-httplib2.noarch 0:0.18.1-3.el7 python2-jmespath.noarch 0:0.9.4-2.el7
sshpass.x86_64 0:1.06-2.el7

完毕!
[root@localhost ~]# yum install -y libselinux-python    #安装Python依赖包软件
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
file:///media/cdrom/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn‘t open file /media/cdrom/repodata/repomd.xml"

-------------------------------------------------------------------------------------------------------------------

省略部分安装信息

--------------------------------------------------------------------------------------------------------------------
完毕!
查看Ansible软件安装了那些软件

[root@localhost ~]# rpm -ql ansible

/usr/share/man/man1/ansible-vault.1.gz
/usr/share/man/man1/ansible.1.gz

省略部分,这边的文件很多的

--------------------------------------------------------------------------------------------------------------------

[root@localhost ~]# ansible --version      #查看软件版本信息
ansible 2.9.18
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, Nov 16 2020, 22:23:17) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
[root@localhost ~]#

在被管理的主机中安装软件分别为主机名为web01和主机名为backup的主机中安装,发现都已经安装过了,不需要任何处理。

[root@web1 ~]# yum install -y libselinux-python
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
---------------------------------------------------------------------------------------------------------------

省略部分信息

----------------------------------------------------------------------------------------------------------------
(2/2): epel/x86_64/primary_db | 6.9 MB 00:00:04
软件包 libselinux-python-2.5-15.el7.x86_64 已安装并且是最新版本
无须任何处理
[root@web1 ~]#

[root@backup ~]# yum install -y libselinux-python     #安装依赖的python 环境
已加载插件:fastestmirror
---------------------------------------------------------------------------------------------------------------

省略部分信息

----------------------------------------------------------------------------------------------------------------
软件包 libselinux-python-2.5-15.el7.x86_64 已安装并且是最新版本
无须任何处理

  利用Ansible批量远程管理主机有两种管理方式,一个是传统的密码认证管理方式另一个是利用密钥的管理方式,在介绍批量管理方式之前先准备基础环境,编写简单的Ansible被管理主机的清单(在管理机上编辑)。这里注意我的管理主机的主机名为localhost,被管理主机名为web01 和backup。

[root@localhost ~]# cp /etc/ansible/hosts{,.osi}
[root@localhost ~]# >/etc/ansible/hosts    #将原本的文件内容清空
[root@localhost ~]# vim /etc/ansible/hosts
[root@localhost ~]# cat /etc/ansible/hosts

[wangju] #组名
192.168.146.110 #被管理主机的ip

[feifei]
192.168.146.120

通过SSH密码认证的方式批量管理主机

  Ansible是利用本地的SSH及一些远程SSH服务作为客户端管理主机的,由于客户端的SSH服务默认就是开启的状态,因此不需要进行单独的配置,直接利用远程主机的SSH服务即可实现批量管理。

[root@localhost ~]# ansible wangju -m command -a "cat /etc/redhat-release" -k -u root   #利用ansible命令进行远程管理,进行的服务是查看系统版本信息
SSH password:
192.168.146.110 | FAILED | rc=-1 >>
Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host‘s fingerprint to your known_hosts file to manage this host.  #出现红色的报错,说明无法执行,

上述报错提示说明使用密码方式登录,主机Host Key检查被激活的情况下,sshpass是不支持这种用法的,需要在管理机本地的 ~/ssh./known_hosts 添加对应的管理主机的密钥文件才行。

[root@localhost ~]# cat ~/.ssh/known_hosts    #查看发现没有密钥文件,需要手动访问
cat: /root/.ssh/known_hosts: 没有那个文件或目录
[root@localhost ~]# ssh 192.168.146.110 cat /etc/redhat-release  #手动访问生成密钥文件
The authenticity of host ‘192.168.146.110 (192.168.146.110)‘ can‘t be established.
ECDSA key fingerprint is SHA256:XQ5/hDk4Max66x7IVSWWADRGAP8e2wAfp1Fgyf1zprk.
ECDSA key fingerprint is MD5:6e:3d:03:6f:c4:7f:ae:e1:5b:47:7c:e9:55:1a:bb:97.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.146.110‘ (ECDSA) to the list of known hosts.
root@192.168.146.110‘s password:
CentOS Linux release 7.9.2009 (Core)
[root@localhost ~]# ssh 192.168.146.120 cat /etc/redhat-release     #手动访问生成密钥文件
The authenticity of host ‘192.168.146.120 (192.168.146.120)‘ can‘t be established.
ECDSA key fingerprint is SHA256:u0w5mU2RUx/iW9nk651JWcPzM6d9U29zVtcFUmWwztM.
ECDSA key fingerprint is MD5:49:8e:d7:7b:80:81:8f:e5:26:3e:3d:71:a1:b5:e2:f0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.146.120‘ (ECDSA) to the list of known hosts.
root@192.168.146.120‘s password:
CentOS Linux release 7.8.2003 (Core)

[root@localhost ~]# tail -2 ~/.ssh/known_hosts    #重新检查,发现多了密钥文件
192.168.146.110 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBJjGKw+d0gQ5TFZon6FtvKTZJQN8JW9+53x2SOovJk7GXcnsPrNrZ34EplgjLw4WbBbGGIPPyy7QXu3YyeWBLQA=
192.168.146.120 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBP4rwbnUYYQ/Sus+ck13UlElZGOsSpksZSnyErNUeLJvwax3J9Yq1Y6EtFDaKrNEQx7EI4mq77c3bL1F/+dHwXA=

在重新执行批量查看所有主机系统的版本

[root@localhost ~]# ansible wangju -m command -a "cat /etc/redhat-release" -k -u root    #查看wangju 组中的主机的版本信息
SSH password:
192.168.146.110 | CHANGED | rc=0 >>
CentOS Linux release 7.9.2009 (Core)
[root@localhost ~]# ansible all -m command -a "cat /etc/redhat-release" -k -u root        #查看所有被管理主机的版本信息
SSH password:
192.168.146.110 | CHANGED | rc=0 >>
CentOS Linux release 7.9.2009 (Core)
192.168.146.120 | CHANGED | rc=0 >>
CentOS Linux release 7.8.2003 (Core)
[root@localhost ~]#

  配置不用输入密码就能进行的批量管理(如果每次登录,都需要输入密码,再加上每台主机的root的密码都不一样,这样就是一个很麻烦的工程,因此才有接下来的配置)

编辑/etc/ansible/hosts 文件

[root@localhost ~]# vim /etc/ansible/hosts

技术分享图片

[root@localhost ~]# ansible wangju -m command -a "cat /etc/redhat-release"      #这边不需要其他的配置,只需要去掉 -k  -u root 的参数即可
192.168.146.110 | CHANGED | rc=0 >>
CentOS Linux release 7.9.2009 (Core)

这才是我们想要的不输入密码就能进行批量管理远程主机的方式,采用SSH密码认证方式批量管理是值得推荐的Ansible批量管理方式之一,另一种方式就是利用免密认证(事先配置密钥)的方式进行批量管理。

通过SSH密钥方式进行批量管理

前面讲的批量管理的方式是利用Ansible进行管理的,还需要在SSH登录的密码信息,或者在hosts文件中配置用户和密码参数信息,看起来初始管理略微复杂了点,读者还可以利用SSH密钥方式实现批量管理。

[root@localhost ~]# ssh-keygen -f /root/.ssh/id_rsa -P "" >/dev/null 2>&1   #免交互创建密钥对
[root@localhost ~]# ls -l ~/.ssh/
总用量 12
-rw-------. 1 root root 1675 5月 19 08:30 id_rsa      #生成的私钥
-rw-r--r--. 1 root root 408 5月 19 08:30 id_rsa.pub     #生成的公钥
-rw-r--r--. 1 root root 354 5月 18 21:59 known_hosts

采用交互方式分发公钥信息,将SSH服务创建的公钥信息分发到被管理主机

[root@localhost ~]# sshpass -proot ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 192.168.146.110
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"

Number of key(s) added: 1

Now try logging into the machine, with: "ssh -o ‘ StrictHostKeyChecking=no‘ ‘192.168.146.110‘"
and check to make sure that only the key(s) you wanted were added.

[root@localhost ~]# sshpass -proot ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 192.168.146.120
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"

Number of key(s) added: 1

Now try logging into the machine, with: "ssh -o ‘ StrictHostKeyChecking=no‘ ‘192.168.146.120‘"
and check to make sure that only the key(s) you wanted were added.

[root@localhost ~]# vim /etc/ansible/hosts
[root@localhost ~]# cat /etc/ansible/hosts

[wangju] #组名
192.168.146.110 ansible_ssh_user=root ansible_ssh_pass=root
#在主配置文件指定用户与登录的密码,执行命令的时候就不需要密码了

[feifei]
192.168.146.120 
[root@localhost ~]#

[root@localhost ~]# ansible all -m command -a "cat /etc/redhat-release"
192.168.146.110 | CHANGED | rc=0 >>        #但是在这里就不确定192.168.146.100 是采用那种方式登录的
CentOS Linux release 7.9.2009 (Core)
192.168.146.120 | CHANGED | rc=0 >>
CentOS Linux release 7.8.2003 (Core)  

到这里Ansible的两种批量管理的方式就介绍完了。

Ansible自动化运维简介及批量管理方式

原文:https://www.cnblogs.com/luncy/p/14778642.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!