前言
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.-.el7.x86_64 # SMP Thu Nov :: UTC 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 ::
extras | 3.4 kB ::
updates | 3.4 kB ::
(/): extras//x86_64/primary_db | kB ::
(/): updates//x86_64/primary_db | 3.4 MB ::
Resolving Dependencies
--> Running transaction check
---> Package ansible.noarch :2.4.2.0-.el7 will be installed
--> Processing Dependency: sshpass for package: ansible-2.4.2.0-.el7.noarch
--> Processing Dependency: python2-jmespath for package: ansible-2.4.2.0-.el7.noarch
--> Processing Dependency: python-passlib for package: ansible-2.4.2.0-.el7.noarch
--> Processing Dependency: python-paramiko for package: ansible-2.4.2.0-.el7.noarch
--> Processing Dependency: python-jinja2 for package: ansible-2.4.2.0-.el7.noarch
--> Processing Dependency: python-httplib2 for package: ansible-2.4.2.0-.el7.noarch
--> Processing Dependency: python-cryptography for package: ansible-2.4.2.0-.el7.noarch
--> Running transaction check
---> Package python-httplib2.noarch :0.9.-.el7 will be installed
---> Package python-jinja2.noarch :2.7.-.el7 will be installed
--> Processing Dependency: python-babel >= 0.8 for package: python-jinja2-2.7.-.el7.noarch
--> Processing Dependency: python-markupsafe for package: python-jinja2-2.7.-.el7.noarch
---> Package python-paramiko.noarch :2.1.-.el7 will be installed
--> Processing Dependency: python2-pyasn1 for package: python-paramiko-2.1.-.el7.noarch
---> Package python-passlib.noarch :1.6.-.el7 will be installed
---> Package python2-cryptography.x86_64 :1.7.-.el7 will be installed
--> Processing Dependency: python-idna >= 2.0 for package: python2-cryptography-1.7.-.el7.x86_64
--> Processing Dependency: python-cffi >= 1.4. for package: python2-cryptography-1.7.-.el7.x86_64
--> Processing Dependency: python-enum34 for package: python2-cryptography-1.7.-.el7.x86_64
---> Package python2-jmespath.noarch :0.9.-.el7 will be installed
---> Package sshpass.x86_64 :1.06-.el7 will be installed
--> Running transaction check
---> Package python-babel.noarch :0.9.-.el7 will be installed
---> Package python-cffi.x86_64 :1.6.-.el7 will be installed
--> Processing Dependency: python-pycparser for package: python-cffi-1.6.-.el7.x86_64
---> Package python-enum34.noarch :1.0.-.el7 will be installed
---> Package python-idna.noarch :2.4-.el7 will be installed
---> Package python-markupsafe.x86_64 :0.11-.el7 will be installed
---> Package python2-pyasn1.noarch :0.1.-.el7 will be installed
--> Running transaction check
---> Package python-pycparser.noarch :2.14-.el7 will be installed
--> Processing Dependency: python-ply for package: python-pycparser-2.14-.el7.noarch
--> Running transaction check
---> Package python-ply.noarch :3.4-.el7 will be installed
--> Finished Dependency Resolution Dependencies Resolved ==============================================================================================================================================================================================================
Package Arch Version Repository Size
==============================================================================================================================================================================================================
Installing:
ansible noarch 2.4.2.0-.el7 extras 7.6 M
Installing for dependencies:
python-babel noarch 0.9.-.el7 base 1.4 M
python-cffi x86_64 1.6.-.el7 base k
python-enum34 noarch 1.0.-.el7 base k
python-httplib2 noarch 0.9.-.el7 extras k
python-idna noarch 2.4-.el7 base k
python-jinja2 noarch 2.7.-.el7 base k
python-markupsafe x86_64 0.11-.el7 base k
python-paramiko noarch 2.1.-.el7 updates k
python-passlib noarch 1.6.-.el7 extras k
python-ply noarch 3.4-.el7 base k
python-pycparser noarch 2.14-.el7 base k
python2-cryptography x86_64 1.7.-.el7 base k
python2-jmespath noarch 0.9.-.el7 extras k
python2-pyasn1 noarch 0.1.-.el7 base k
sshpass x86_64 1.06-.el7 extras k Transaction Summary
==============================================================================================================================================================================================================
Install Package (+ Dependent packages) Total download size: M
Installed size: M
Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64//base/packages/python-babel-0.9.-.el7.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY ] 0.0 B/s | kB --:--:-- ETA
Public key for python-babel-0.9.-.el7.noarch.rpm is not installed
(/): python-babel-0.9.-.el7.noarch.rpm | 1.4 MB ::
(/): python-idna-2.4-.el7.noarch.rpm | kB ::
(/): python-enum34-1.0.-.el7.noarch.rpm | kB ::
Public key for python-httplib2-0.9.-.el7.noarch.rpm is not installed % [==========- ] 1.5 MB/s | 1.6 MB :: ETA
(/): python-httplib2-0.9.-.el7.noarch.rpm | kB ::
(/): python-jinja2-2.7.-.el7.noarch.rpm | kB ::
(/): python-passlib-1.6.-.el7.noarch.rpm | kB ::
Public key for python-paramiko-2.1.-.el7.noarch.rpm is not installed
(/): python-paramiko-2.1.-.el7.noarch.rpm | kB ::
(/): python-ply-3.4-.el7.noarch.rpm | kB ::
(/): python-pycparser-2.14-.el7.noarch.rpm | kB ::
(/): python2-cryptography-1.7.-.el7.x86_64.rpm | kB ::
(/): python2-pyasn1-0.1.-.el7.noarch.rpm | kB ::
(/): python2-jmespath-0.9.-.el7.noarch.rpm | kB ::
(/): sshpass-1.06-.el7.x86_64.rpm | kB ::
(/): ansible-2.4.2.0-.el7.noarch.rpm | 7.6 MB ::
(/): python-cffi-1.6.-.el7.x86_64.rpm | kB ::
(/): python-markupsafe-0.11-.el7.x86_64.rpm | kB ::
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 1.8 MB/s | MB ::
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: ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
Package : centos-release--6.1810..el7.centos.x86_64 (@anaconda)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : python2-pyasn1-0.1.-.el7.noarch /
Installing : python-enum34-1.0.-.el7.noarch /
Installing : python-httplib2-0.9.-.el7.noarch /
Installing : sshpass-1.06-.el7.x86_64 /
Installing : python-babel-0.9.-.el7.noarch /
Installing : python2-jmespath-0.9.-.el7.noarch /
Installing : python-passlib-1.6.-.el7.noarch /
Installing : python-ply-3.4-.el7.noarch /
Installing : python-pycparser-2.14-.el7.noarch /
Installing : python-cffi-1.6.-.el7.x86_64 /
Installing : python-markupsafe-0.11-.el7.x86_64 /
Installing : python-jinja2-2.7.-.el7.noarch /
Installing : python-idna-2.4-.el7.noarch /
Installing : python2-cryptography-1.7.-.el7.x86_64 /
Installing : python-paramiko-2.1.-.el7.noarch /
Installing : ansible-2.4.2.0-.el7.noarch /
Verifying : python-idna-2.4-.el7.noarch /
Verifying : python-markupsafe-0.11-.el7.x86_64 /
Verifying : python-jinja2-2.7.-.el7.noarch /
Verifying : python-ply-3.4-.el7.noarch /
Verifying : python-passlib-1.6.-.el7.noarch /
Verifying : python2-jmespath-0.9.-.el7.noarch /
Verifying : python-paramiko-2.1.-.el7.noarch /
Verifying : python-babel-0.9.-.el7.noarch /
Verifying : ansible-2.4.2.0-.el7.noarch /
Verifying : python-cffi-1.6.-.el7.x86_64 /
Verifying : sshpass-1.06-.el7.x86_64 /
Verifying : python-httplib2-0.9.-.el7.noarch /
Verifying : python2-pyasn1-0.1.-.el7.noarch /
Verifying : python-enum34-1.0.-.el7.noarch /
Verifying : python-pycparser-2.14-.el7.noarch /
Verifying : python2-cryptography-1.7.-.el7.x86_64 / Installed:
ansible.noarch :2.4.2.0-.el7 Dependency Installed:
python-babel.noarch :0.9.-.el7 python-cffi.x86_64 :1.6.-.el7 python-enum34.noarch :1.0.-.el7 python-httplib2.noarch :0.9.-.el7 python-idna.noarch :2.4-.el7
python-jinja2.noarch :2.7.-.el7 python-markupsafe.x86_64 :0.11-.el7 python-paramiko.noarch :2.1.-.el7 python-passlib.noarch :1.6.-.el7 python-ply.noarch :3.4-.el7
python-pycparser.noarch :2.14-.el7 python2-cryptography.x86_64 :1.7.-.el7 python2-jmespath.noarch :0.9.-.el7 python2-pyasn1.noarch :0.1.-.el7 sshpass.x86_64 :1.06-.el7 Complete!
安装Ansible
结果表明,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./site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7. (default, Oct , ::) [GCC 4.8. (Red Hat 4.8.-)]
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:::7e:7c:::c1:9a:e4:aa:0d:ce::.
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 :: 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 ]----+
| ..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:
% .6KB/s :
id_rsa.pub
- 再次验证SSH登录
从Ansible主机SSH登录到目标主机:
[root@k3s-vip ~]# ssh 192.168.6.193
Last login: Sun Apr :: 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 :: CST
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
-rw-r--r-- Jan ansible.cfg # Ansible配置文件
-rw-r--r-- Jan hosts # Ansible主机文件
drwxr-xr-x Jan 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:
with_items:
- { src: '/root/ansible/show_hosts.sh', dest: '/usr/local/bin/'} - name: run shell
shell: |
/usr/local/bin/show_hosts.sh; sleep ; - 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= changed= unreachable= failed= [root@k3s-vip ansible]#
七、小结
- 本文主要工作如下:
下载安装自动化运维工具Ansible;
建立管理主机和目标主机之间的SSH信任关系;
配置管理主机上的Ansible运行环境:远程目标主机组等;
编写例子脚本show_hosts.sh;
编写playbook剧本:复制shell脚本、远程执行脚本,最后删除脚本;
运行playbook。
- 小结
Ansible是一个功能强大的IT运维工具,本文只是揭开冰山一角。本文的过程略显复杂,但是“写一次,处处运行”的好处的是显而易见的。
希望本文对读者有帮助,欢迎留言交流。