在CentOS7.6上安装自动化运维工具Ansible以及playbook案例实操
在CentOS7.6上安装自动化运维工具Ansible以及playbook案例实操
前言
Ansible是一款优秀的自动化IT运维工具,具有远程安装、远程部署应用、远程管理能力,支持Windows、Linux、Unix、macOS和大型机等多种操作系统。
下面就以CentOS 7.6为主机操作系统,演示安装Ansible工具的过程,将一个本地应用例子安装到远程主机,并在远程主机上运行应用。
一、环境准备
完成这个例子,最少需要两台主机,一台安装Ansible运维工具本身,用作管理主机,一台用作远程目标主机。
两台主机都预先安装了CentOS 7.6操作系统。
# Ansible管理主机
192.168.6.190 root@k3s-vip
# 远程目标主机
192.168.6.193 root@k3s-node3
二、安装Ansible运维工具
使用SSH登录到拟安装Ansible的主机。
- 查看系统环境:
[root@k3s-vip ~]# uname -a Linux k3s-vip 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
- 运行安装命令
Ansible的安装过程很简单,只需要一条yum命令。
使用CentOS自带工具yum自动下载、安装Ansible。Ansible依赖于Python语言,安装Ansible时会自动下载、安装Python语言包。
安装过程中会提示是否安装依赖软件包,全部选y。
[root@k3s-vip ~]# yum install ansible Loaded plugins: fastestmirror, langpacks Determining fastest mirrors * base: centos.ustc.edu.cn * extras: mirrors.cqu.edu.cn * updates: mirrors.cqu.edu.cn base | 3.6 kB 00:00:00 extras | 3.4 kB 00:00:00 updates | 3.4 kB 00:00:00 (1/2): extras/7/x86_64/primary_db | 187 kB 00:00:01 (2/2): updates/7/x86_64/primary_db | 3.4 MB 00:00:15 Resolving Dependencies --> Running transaction check ---> Package ansible.noarch 0:2.4.2.0-2.el7 will be installed --> Processing Dependency: sshpass for package: ansible-2.4.2.0-2.el7.noarch --> Processing Dependency: python2-jmespath for package: ansible-2.4.2.0-2.el7.noarch --> Processing Dependency: python-passlib for package: ansible-2.4.2.0-2.el7.noarch --> Processing Dependency: python-paramiko for package: ansible-2.4.2.0-2.el7.noarch --> Processing Dependency: python-jinja2 for package: ansible-2.4.2.0-2.el7.noarch --> Processing Dependency: python-httplib2 for package: ansible-2.4.2.0-2.el7.noarch --> Processing Dependency: python-cryptography for package: ansible-2.4.2.0-2.el7.noarch --> Running transaction check ---> Package python-httplib2.noarch 0:0.9.2-1.el7 will be installed ---> Package python-jinja2.noarch 0:2.7.2-2.el7 will be installed --> Processing Dependency: python-babel >= 0.8 for package: python-jinja2-2.7.2-2.el7.noarch --> Processing Dependency: python-markupsafe for package: python-jinja2-2.7.2-2.el7.noarch ---> Package python-paramiko.noarch 0:2.1.1-9.el7 will be installed --> Processing Dependency: python2-pyasn1 for package: python-paramiko-2.1.1-9.el7.noarch ---> Package python-passlib.noarch 0:1.6.5-2.el7 will be installed ---> Package python2-cryptography.x86_64 0:1.7.2-2.el7 will be installed --> Processing Dependency: python-idna >= 2.0 for package: python2-cryptography-1.7.2-2.el7.x86_64 --> Processing Dependency: python-cffi >= 1.4.1 for package: python2-cryptography-1.7.2-2.el7.x86_64 --> Processing Dependency: python-enum34 for package: python2-cryptography-1.7.2-2.el7.x86_64 ---> Package python2-jmespath.noarch 0:0.9.0-3.el7 will be installed ---> Package sshpass.x86_64 0:1.06-2.el7 will be installed --> Running transaction check ---> Package python-babel.noarch 0:0.9.6-8.el7 will be installed ---> Package python-cffi.x86_64 0:1.6.0-5.el7 will be installed --> Processing Dependency: python-pycparser for package: python-cffi-1.6.0-5.el7.x86_64 ---> Package python-enum34.noarch 0:1.0.4-1.el7 will be installed ---> Package python-idna.noarch 0:2.4-1.el7 will be installed ---> Package python-markupsafe.x86_64 0:0.11-10.el7 will be installed ---> Package python2-pyasn1.noarch 0:0.1.9-7.el7 will be installed --> Running transaction check ---> Package python-pycparser.noarch 0:2.14-1.el7 will be installed --> Processing Dependency: python-ply for package: python-pycparser-2.14-1.el7.noarch --> Running transaction check ---> Package python-ply.noarch 0:3.4-11.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ============================================================================================================================================================================================================== Package Arch Version Repository Size ============================================================================================================================================================================================================== Installing: ansible noarch 2.4.2.0-2.el7 extras 7.6 M Installing for dependencies: python-babel noarch 0.9.6-8.el7 base 1.4 M python-cffi x86_64 1.6.0-5.el7 base 218 k python-enum34 noarch 1.0.4-1.el7 base 52 k python-httplib2 noarch 0.9.2-1.el7 extras 115 k python-idna noarch 2.4-1.el7 base 94 k python-jinja2 noarch 2.7.2-2.el7 base 515 k python-markupsafe x86_64 0.11-10.el7 base 25 k python-paramiko noarch 2.1.1-9.el7 updates 269 k python-passlib noarch 1.6.5-2.el7 extras 488 k python-ply noarch 3.4-11.el7 base 123 k python-pycparser noarch 2.14-1.el7 base 104 k python2-cryptography x86_64 1.7.2-2.el7 base 502 k python2-jmespath noarch 0.9.0-3.el7 extras 39 k python2-pyasn1 noarch 0.1.9-7.el7 base 100 k sshpass x86_64 1.06-2.el7 extras 21 k Transaction Summary ============================================================================================================================================================================================================== Install 1 Package (+15 Dependent packages) Total download size: 12 M Installed size: 57 M Is this ok [y/d/N]: y Downloading packages: warning: /var/cache/yum/x86_64/7/base/packages/python-babel-0.9.6-8.el7.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY ] 0.0 B/s | 574 kB --:--:-- ETA Public key for python-babel-0.9.6-8.el7.noarch.rpm is not installed (1/16): python-babel-0.9.6-8.el7.noarch.rpm | 1.4 MB 00:00:00 (2/16): python-idna-2.4-1.el7.noarch.rpm | 94 kB 00:00:00 (3/16): python-enum34-1.0.4-1.el7.noarch.rpm | 52 kB 00:00:00 Public key for python-httplib2-0.9.2-1.el7.noarch.rpm is not installed 13% [==========- ] 1.5 MB/s | 1.6 MB 00:00:06 ETA (4/16): python-httplib2-0.9.2-1.el7.noarch.rpm | 115 kB 00:00:00 (5/16): python-jinja2-2.7.2-2.el7.noarch.rpm | 515 kB 00:00:00 (6/16): python-passlib-1.6.5-2.el7.noarch.rpm | 488 kB 00:00:00 Public key for python-paramiko-2.1.1-9.el7.noarch.rpm is not installed (7/16): python-paramiko-2.1.1-9.el7.noarch.rpm | 269 kB 00:00:00 (8/16): python-ply-3.4-11.el7.noarch.rpm | 123 kB 00:00:00 (9/16): python-pycparser-2.14-1.el7.noarch.rpm | 104 kB 00:00:00 (10/16): python2-cryptography-1.7.2-2.el7.x86_64.rpm | 502 kB 00:00:00 (11/16): python2-pyasn1-0.1.9-7.el7.noarch.rpm | 100 kB 00:00:00 (12/16): python2-jmespath-0.9.0-3.el7.noarch.rpm | 39 kB 00:00:00 (13/16): sshpass-1.06-2.el7.x86_64.rpm | 21 kB 00:00:00 (14/16): ansible-2.4.2.0-2.el7.noarch.rpm | 7.6 MB 00:00:03 (15/16): python-cffi-1.6.0-5.el7.x86_64.rpm | 218 kB 00:00:04 (16/16): python-markupsafe-0.11-10.el7.x86_64.rpm | 25 kB 00:00:04 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Total 1.8 MB/s | 12 MB 00:00:06 Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 Importing GPG key 0xF4A80EB5: Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>" Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5 Package : centos-release-7-6.1810.2.el7.centos.x86_64 (@anaconda) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 Is this ok [y/N]: y Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : python2-pyasn1-0.1.9-7.el7.noarch 1/16 Installing : python-enum34-1.0.4-1.el7.noarch 2/16 Installing : python-httplib2-0.9.2-1.el7.noarch 3/16 Installing : sshpass-1.06-2.el7.x86_64 4/16 Installing : python-babel-0.9.6-8.el7.noarch 5/16 Installing : python2-jmespath-0.9.0-3.el7.noarch 6/16 Installing : python-passlib-1.6.5-2.el7.noarch 7/16 Installing : python-ply-3.4-11.el7.noarch 8/16 Installing : python-pycparser-2.14-1.el7.noarch 9/16 Installing : python-cffi-1.6.0-5.el7.x86_64 10/16 Installing : python-markupsafe-0.11-10.el7.x86_64 11/16 Installing : python-jinja2-2.7.2-2.el7.noarch 12/16 Installing : python-idna-2.4-1.el7.noarch 13/16 Installing : python2-cryptography-1.7.2-2.el7.x86_64 14/16 Installing : python-paramiko-2.1.1-9.el7.noarch 15/16 Installing : ansible-2.4.2.0-2.el7.noarch 16/16 Verifying : python-idna-2.4-1.el7.noarch 1/16 Verifying : python-markupsafe-0.11-10.el7.x86_64 2/16 Verifying : python-jinja2-2.7.2-2.el7.noarch 3/16 Verifying : python-ply-3.4-11.el7.noarch 4/16 Verifying : python-passlib-1.6.5-2.el7.noarch 5/16 Verifying : python2-jmespath-0.9.0-3.el7.noarch 6/16 Verifying : python-paramiko-2.1.1-9.el7.noarch 7/16 Verifying : python-babel-0.9.6-8.el7.noarch 8/16 Verifying : ansible-2.4.2.0-2.el7.noarch 9/16 Verifying : python-cffi-1.6.0-5.el7.x86_64 10/16 Verifying : sshpass-1.06-2.el7.x86_64 11/16 Verifying : python-httplib2-0.9.2-1.el7.noarch 12/16 Verifying : python2-pyasn1-0.1.9-7.el7.noarch 13/16 Verifying : python-enum34-1.0.4-1.el7.noarch 14/16 Verifying : python-pycparser-2.14-1.el7.noarch 15/16 Verifying : python2-cryptography-1.7.2-2.el7.x86_64 16/16 Installed: ansible.noarch 0:2.4.2.0-2.el7 Dependency Installed: python-babel.noarch 0:0.9.6-8.el7 python-cffi.x86_64 0:1.6.0-5.el7 python-enum34.noarch 0:1.0.4-1.el7 python-httplib2.noarch 0:0.9.2-1.el7 python-idna.noarch 0:2.4-1.el7 python-jinja2.noarch 0:2.7.2-2.el7 python-markupsafe.x86_64 0:0.11-10.el7 python-paramiko.noarch 0:2.1.1-9.el7 python-passlib.noarch 0:1.6.5-2.el7 python-ply.noarch 0:3.4-11.el7 python-pycparser.noarch 0:2.14-1.el7 python2-cryptography.x86_64 0:1.7.2-2.el7 python2-jmespath.noarch 0:0.9.0-3.el7 python2-pyasn1.noarch 0:0.1.9-7.el7 sshpass.x86_64 0:1.06-2.el7 Complete!
结果表明,Ansible已经正确安装。
- 查看Ansible版本号
[root@k3s-vip ~]# ansible --version ansible 2.4.2.0 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 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
Ansible的版本号是2.4.2.0,依赖的Python语言包版本号是2.7.5。
安装Ansible不是目的,Ansible的强大功能是自动化安装、部署和运行。下面以一个小例子来演示Ansible的IT运维能力。
三、建立主机信任关系
Ansible是基于SSH来管理远程主机的,为了能自动化运行,需要在Ansible管理主机与远程目标主机之间建立SSH信任关系。
信任关系建立后,Ansible就可以自由访问目标主机。
- 从Ansible管理主机远程登录到目标主机:
以root用户远程登录目标主机,提示时选择yes,输入远程目标主机上的root用户密码。因为需要在远程目标主机上安装软件包,使用root用户是必须的。
[root@k3s-vip ~]# ssh root@k3s-node3 The authenticity of host 'k3s-node3 (192.168.6.193)' can't be established. ECDSA key fingerprint is SHA256:v8yvQmBzZWZrQzhIn4lNMgTsVK7C0lHaEcvdL9cqdno. ECDSA key fingerprint is MD5:a0:aa:14:14:7e:7c:98:67:c1:9a:e4:aa:0d:ce:32:83. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'k3s-node3,192.168.6.193' (ECDSA) to the list of known hosts. root@k3s-node3's password: Last login: Sun Apr 7 11:09:20 2019 from 192.168.6.1
- 在Ansible管理主机生成RSA密钥对(公钥/私钥)
[root@k3s-vip ~]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:3GNNAxe62c9R505oFL0mkoKYJ53d2XjvP0cIWEsjvX8 root@k3s-vip The key's randomart image is: +---[RSA 2048]----+ | ..o... | | .+= ..| | + + o=B+. +| | + * +.%+* *.| | o S B =oB.o| | . . +o=E| | +o.| | o.| | =| +----[SHA256]-----+
- 复制Ansible管理主机的公钥到远程目标主机
[root@k3s-vip ~]# scp -p ~/.ssh/id_rsa.pub root@192.168.6.193:/root/.ssh/authorized_keys root@192.168.6.193's password: 100% 405 605.6KB/s 00:00 id_rsa.pub
- 再次验证SSH登录
从Ansible主机SSH登录到目标主机:
[root@k3s-vip ~]# ssh 192.168.6.193 Last login: Sun Apr 7 12:00:19 2019 from k3s-vip
[root@k3s-node3 ~]#
结果显示,可以免密码登录了。
配置主机信任的过程有点麻烦,但是配置一次就可以永久免密登录,比起每次登录输入密码还是值得的。
四、配置Ansible环境
新建一个Shell脚本文件show_hosts.sh,功能很简单,输出日期时间和/etc/hosts文件的内容到控制台。
编写远程安装的Ansible部署文件,将show_hosts.sh文件安装到远程主机的/user/loca/bin目录下。
在远程主机运行脚本,然后删除脚本。
- 新建Shell脚本
[root@k3s-vip ~]# mkdir -p /root/ansible [root@k3s-vip ~]# cd /root/ansible/ [root@k3s-vip ansible]# vi show_hosts.sh [root@k3s-vip ansible]# chmod +x show_hosts.sh [root@k3s-vip ansible]# cat show_hosts.sh #!/usr/bin/bash date cat /etc/hosts
- 在本地测试脚本
[root@k3s-vip ~]# ./show_hosts.sh Sun Apr 7 14:42:22 CST 2019 127.0.0.1 localhost localhost.localdomain 192.168.6.190 k3s-vip 192.168.6.193 k3s-node3
输出了当前日期时间和/etc/hosts文件的内容。
- 查看Ansible配置目录
[root@k3s-vip ~]# ls -ln /etc/ansible/ total 24 -rw-r--r-- 1 0 0 19179 Jan 30 2018 ansible.cfg # Ansible配置文件 -rw-r--r-- 1 0 0 1016 Jan 30 2018 hosts # Ansible主机文件 drwxr-xr-x 2 0 0 6 Jan 30 2018 roles # 角色文件
- 修改/etc/ansible/hosts文件
修改hosts文件,在文件最后添加主机组k3s-node以及组内主机IP:
[k3s-node]
192.168.6.193
[root@k3s-vip ~]# cd /etc/ansible/ [root@k3s-vip ansible]# vi hosts [root@k3s-vip ansible]# cat hosts # This is the default ansible 'hosts' file. # ... [k3s-node] 192.168.6.193
- 测试远程主机的连通性
[root@k3s-vip ansible]# ansible k3s-node -m ping 192.168.6.193 | SUCCESS => { "changed": false, "ping": "pong" }
子命令ping会依次向主机组k3s-node内的各个主机发出报文,验证网络连通性。
五、编写playbook剧本文件
playbook是预先编写的描述性剧本,ansible会逐个执行playbook剧本内的命令。
- 编写playbook文件
playbook文件是yaml语言格式描述的。show_hosts.yaml剧本分为三个子任务:
复制shell脚本到远程主机;
在远程主机执行shell脚本,休眠3秒;
最后删除脚本。
[root@k3s-vip ansible]# cd /root/ansible/ [root@k3s-vip ansible]# [root@k3s-vip ansible]# cat show_hosts.yaml - name: a sample for show /etc/hosts hosts: k3s-node remote_user: root tasks: - name: copy show_hosts shell file: src: '{{ item.src }}' dest: '{{ item.dest }}' mode: 755 with_items: - { src: '/root/ansible/show_hosts.sh', dest: '/usr/local/bin/'} - name: run shell shell: | /usr/local/bin/show_hosts.sh; sleep 3; - name: delete shell file: src: '{{ item.src }}' dest: '{{ item.dest }}' state: '{{ item.state }}' with_items: - { src: '', dest: '/usr/local/bin/show_hosts.sh', state: 'absent' }
万事具备,只欠东风了。激动人心的时刻即将到来。
六、远程执行playbook剧本
- 远程执行playbook剧本
在Ansible主机运行ansible-playbook命令,按顺序执行playbook剧本子任务,从结果看所有子任务全部执行成功。
[root@k3s-vip ansible]# ansible-playbook show_hosts.yaml PLAY [a sample for show /etc/hosts] ************************************************************************************************************************************************************************** TASK [Gathering Facts] *************************************************************************************************************************************************************************************** ok: [192.168.6.193] TASK [copy show_hosts shell] ********************************************************************************************************************************************************************************* changed: [192.168.6.193] => (item={u'dest': u'/usr/local/bin/', u'src': u'/root/ansible/show_hosts.sh'}) TASK [run shell] ********************************************************************************************************************************************************************************************* changed: [192.168.6.193] TASK [delete shell] ****************************************************************************************************************************************************************************************** changed: [192.168.6.193] => (item={u'dest': u'/usr/local/bin/show_hosts.sh', u'src': u'', u'state': u'absent'}) PLAY RECAP *************************************************************************************************************************************************************************************************** 192.168.6.193 : ok=4 changed=3 unreachable=0 failed=0 [root@k3s-vip ansible]#
七、小结
- 本文主要工作如下:
下载安装自动化运维工具Ansible;
建立管理主机和目标主机之间的SSH信任关系;
配置管理主机上的Ansible运行环境:远程目标主机组等;
编写例子脚本show_hosts.sh;
编写playbook剧本:复制shell脚本、远程执行脚本,最后删除脚本;
运行playbook。
- 小结
Ansible是一个功能强大的IT运维工具,本文只是揭开冰山一角。本文的过程略显复杂,但是“写一次,处处运行”的好处的是显而易见的。
希望本文对读者有帮助,欢迎留言交流。