经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Linux/Shell » 查看文章
在CentOS7.6上安装自动化运维工具Ansible
来源:cnblogs  作者:清如许99  时间:2019/4/8 10:42:43  对本文有异议

前言

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的主机。

  • 查看系统环境:
  1. [root@k3s-vip ~]# uname -a
  2. 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。

  1. [root@k3s-vip ~]# yum install ansible
  2. Loaded plugins: fastestmirror, langpacks
  3. Determining fastest mirrors
  4. * base: centos.ustc.edu.cn
  5. * extras: mirrors.cqu.edu.cn
  6. * updates: mirrors.cqu.edu.cn
  7. base | 3.6 kB 00:00:00
  8. extras | 3.4 kB 00:00:00
  9. updates | 3.4 kB 00:00:00
  10. (1/2): extras/7/x86_64/primary_db | 187 kB 00:00:01
  11. (2/2): updates/7/x86_64/primary_db | 3.4 MB 00:00:15
  12. Resolving Dependencies
  13. --> Running transaction check
  14. ---> Package ansible.noarch 0:2.4.2.0-2.el7 will be installed
  15. --> Processing Dependency: sshpass for package: ansible-2.4.2.0-2.el7.noarch
  16. --> Processing Dependency: python2-jmespath for package: ansible-2.4.2.0-2.el7.noarch
  17. --> Processing Dependency: python-passlib for package: ansible-2.4.2.0-2.el7.noarch
  18. --> Processing Dependency: python-paramiko for package: ansible-2.4.2.0-2.el7.noarch
  19. --> Processing Dependency: python-jinja2 for package: ansible-2.4.2.0-2.el7.noarch
  20. --> Processing Dependency: python-httplib2 for package: ansible-2.4.2.0-2.el7.noarch
  21. --> Processing Dependency: python-cryptography for package: ansible-2.4.2.0-2.el7.noarch
  22. --> Running transaction check
  23. ---> Package python-httplib2.noarch 0:0.9.2-1.el7 will be installed
  24. ---> Package python-jinja2.noarch 0:2.7.2-2.el7 will be installed
  25. --> Processing Dependency: python-babel >= 0.8 for package: python-jinja2-2.7.2-2.el7.noarch
  26. --> Processing Dependency: python-markupsafe for package: python-jinja2-2.7.2-2.el7.noarch
  27. ---> Package python-paramiko.noarch 0:2.1.1-9.el7 will be installed
  28. --> Processing Dependency: python2-pyasn1 for package: python-paramiko-2.1.1-9.el7.noarch
  29. ---> Package python-passlib.noarch 0:1.6.5-2.el7 will be installed
  30. ---> Package python2-cryptography.x86_64 0:1.7.2-2.el7 will be installed
  31. --> Processing Dependency: python-idna >= 2.0 for package: python2-cryptography-1.7.2-2.el7.x86_64
  32. --> Processing Dependency: python-cffi >= 1.4.1 for package: python2-cryptography-1.7.2-2.el7.x86_64
  33. --> Processing Dependency: python-enum34 for package: python2-cryptography-1.7.2-2.el7.x86_64
  34. ---> Package python2-jmespath.noarch 0:0.9.0-3.el7 will be installed
  35. ---> Package sshpass.x86_64 0:1.06-2.el7 will be installed
  36. --> Running transaction check
  37. ---> Package python-babel.noarch 0:0.9.6-8.el7 will be installed
  38. ---> Package python-cffi.x86_64 0:1.6.0-5.el7 will be installed
  39. --> Processing Dependency: python-pycparser for package: python-cffi-1.6.0-5.el7.x86_64
  40. ---> Package python-enum34.noarch 0:1.0.4-1.el7 will be installed
  41. ---> Package python-idna.noarch 0:2.4-1.el7 will be installed
  42. ---> Package python-markupsafe.x86_64 0:0.11-10.el7 will be installed
  43. ---> Package python2-pyasn1.noarch 0:0.1.9-7.el7 will be installed
  44. --> Running transaction check
  45. ---> Package python-pycparser.noarch 0:2.14-1.el7 will be installed
  46. --> Processing Dependency: python-ply for package: python-pycparser-2.14-1.el7.noarch
  47. --> Running transaction check
  48. ---> Package python-ply.noarch 0:3.4-11.el7 will be installed
  49. --> Finished Dependency Resolution
  50. Dependencies Resolved
  51. ==============================================================================================================================================================================================================
  52. Package Arch Version Repository Size
  53. ==============================================================================================================================================================================================================
  54. Installing:
  55. ansible noarch 2.4.2.0-2.el7 extras 7.6 M
  56. Installing for dependencies:
  57. python-babel noarch 0.9.6-8.el7 base 1.4 M
  58. python-cffi x86_64 1.6.0-5.el7 base 218 k
  59. python-enum34 noarch 1.0.4-1.el7 base 52 k
  60. python-httplib2 noarch 0.9.2-1.el7 extras 115 k
  61. python-idna noarch 2.4-1.el7 base 94 k
  62. python-jinja2 noarch 2.7.2-2.el7 base 515 k
  63. python-markupsafe x86_64 0.11-10.el7 base 25 k
  64. python-paramiko noarch 2.1.1-9.el7 updates 269 k
  65. python-passlib noarch 1.6.5-2.el7 extras 488 k
  66. python-ply noarch 3.4-11.el7 base 123 k
  67. python-pycparser noarch 2.14-1.el7 base 104 k
  68. python2-cryptography x86_64 1.7.2-2.el7 base 502 k
  69. python2-jmespath noarch 0.9.0-3.el7 extras 39 k
  70. python2-pyasn1 noarch 0.1.9-7.el7 base 100 k
  71. sshpass x86_64 1.06-2.el7 extras 21 k
  72. Transaction Summary
  73. ==============================================================================================================================================================================================================
  74. Install 1 Package (+15 Dependent packages)
  75. Total download size: 12 M
  76. Installed size: 57 M
  77. Is this ok [y/d/N]: y
  78. Downloading packages:
  79. 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
  80. Public key for python-babel-0.9.6-8.el7.noarch.rpm is not installed
  81. (1/16): python-babel-0.9.6-8.el7.noarch.rpm | 1.4 MB 00:00:00
  82. (2/16): python-idna-2.4-1.el7.noarch.rpm | 94 kB 00:00:00
  83. (3/16): python-enum34-1.0.4-1.el7.noarch.rpm | 52 kB 00:00:00
  84. 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
  85. (4/16): python-httplib2-0.9.2-1.el7.noarch.rpm | 115 kB 00:00:00
  86. (5/16): python-jinja2-2.7.2-2.el7.noarch.rpm | 515 kB 00:00:00
  87. (6/16): python-passlib-1.6.5-2.el7.noarch.rpm | 488 kB 00:00:00
  88. Public key for python-paramiko-2.1.1-9.el7.noarch.rpm is not installed
  89. (7/16): python-paramiko-2.1.1-9.el7.noarch.rpm | 269 kB 00:00:00
  90. (8/16): python-ply-3.4-11.el7.noarch.rpm | 123 kB 00:00:00
  91. (9/16): python-pycparser-2.14-1.el7.noarch.rpm | 104 kB 00:00:00
  92. (10/16): python2-cryptography-1.7.2-2.el7.x86_64.rpm | 502 kB 00:00:00
  93. (11/16): python2-pyasn1-0.1.9-7.el7.noarch.rpm | 100 kB 00:00:00
  94. (12/16): python2-jmespath-0.9.0-3.el7.noarch.rpm | 39 kB 00:00:00
  95. (13/16): sshpass-1.06-2.el7.x86_64.rpm | 21 kB 00:00:00
  96. (14/16): ansible-2.4.2.0-2.el7.noarch.rpm | 7.6 MB 00:00:03
  97. (15/16): python-cffi-1.6.0-5.el7.x86_64.rpm | 218 kB 00:00:04
  98. (16/16): python-markupsafe-0.11-10.el7.x86_64.rpm | 25 kB 00:00:04
  99. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  100. Total 1.8 MB/s | 12 MB 00:00:06
  101. Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
  102. Importing GPG key 0xF4A80EB5:
  103. Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
  104. Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
  105. Package : centos-release-7-6.1810.2.el7.centos.x86_64 (@anaconda)
  106. From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
  107. Is this ok [y/N]: y
  108. Running transaction check
  109. Running transaction test
  110. Transaction test succeeded
  111. Running transaction
  112. Installing : python2-pyasn1-0.1.9-7.el7.noarch 1/16
  113. Installing : python-enum34-1.0.4-1.el7.noarch 2/16
  114. Installing : python-httplib2-0.9.2-1.el7.noarch 3/16
  115. Installing : sshpass-1.06-2.el7.x86_64 4/16
  116. Installing : python-babel-0.9.6-8.el7.noarch 5/16
  117. Installing : python2-jmespath-0.9.0-3.el7.noarch 6/16
  118. Installing : python-passlib-1.6.5-2.el7.noarch 7/16
  119. Installing : python-ply-3.4-11.el7.noarch 8/16
  120. Installing : python-pycparser-2.14-1.el7.noarch 9/16
  121. Installing : python-cffi-1.6.0-5.el7.x86_64 10/16
  122. Installing : python-markupsafe-0.11-10.el7.x86_64 11/16
  123. Installing : python-jinja2-2.7.2-2.el7.noarch 12/16
  124. Installing : python-idna-2.4-1.el7.noarch 13/16
  125. Installing : python2-cryptography-1.7.2-2.el7.x86_64 14/16
  126. Installing : python-paramiko-2.1.1-9.el7.noarch 15/16
  127. Installing : ansible-2.4.2.0-2.el7.noarch 16/16
  128. Verifying : python-idna-2.4-1.el7.noarch 1/16
  129. Verifying : python-markupsafe-0.11-10.el7.x86_64 2/16
  130. Verifying : python-jinja2-2.7.2-2.el7.noarch 3/16
  131. Verifying : python-ply-3.4-11.el7.noarch 4/16
  132. Verifying : python-passlib-1.6.5-2.el7.noarch 5/16
  133. Verifying : python2-jmespath-0.9.0-3.el7.noarch 6/16
  134. Verifying : python-paramiko-2.1.1-9.el7.noarch 7/16
  135. Verifying : python-babel-0.9.6-8.el7.noarch 8/16
  136. Verifying : ansible-2.4.2.0-2.el7.noarch 9/16
  137. Verifying : python-cffi-1.6.0-5.el7.x86_64 10/16
  138. Verifying : sshpass-1.06-2.el7.x86_64 11/16
  139. Verifying : python-httplib2-0.9.2-1.el7.noarch 12/16
  140. Verifying : python2-pyasn1-0.1.9-7.el7.noarch 13/16
  141. Verifying : python-enum34-1.0.4-1.el7.noarch 14/16
  142. Verifying : python-pycparser-2.14-1.el7.noarch 15/16
  143. Verifying : python2-cryptography-1.7.2-2.el7.x86_64 16/16
  144. Installed:
  145. ansible.noarch 0:2.4.2.0-2.el7
  146. Dependency Installed:
  147. 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
  148. 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
  149. 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
  150. Complete!
安装Ansible

结果表明,Ansible已经正确安装。

  • 查看Ansible版本号
  1. [root@k3s-vip ~]# ansible --version
  2. ansible 2.4.2.0
  3. config file = /etc/ansible/ansible.cfg
  4. configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  5. ansible python module location = /usr/lib/python2.7/site-packages/ansible
  6. executable location = /usr/bin/ansible
  7. 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运维能力。

  1.  

三、建立主机信任关系

 Ansible是基于SSH来管理远程主机的,为了能自动化运行,需要在Ansible管理主机与远程目标主机之间建立SSH信任关系。

信任关系建立后,Ansible就可以自由访问目标主机。

  • 从Ansible管理主机远程登录到目标主机:

以root用户远程登录目标主机,提示时选择yes,输入远程目标主机上的root用户密码。因为需要在远程目标主机上安装软件包,使用root用户是必须的。

  1. [root@k3s-vip ~]# ssh root@k3s-node3 
  2. The authenticity of host 'k3s-node3 (192.168.6.193)' can't be established.
  3. ECDSA key fingerprint is SHA256:v8yvQmBzZWZrQzhIn4lNMgTsVK7C0lHaEcvdL9cqdno.
  4. ECDSA key fingerprint is MD5:a0:aa:14:14:7e:7c:98:67:c1:9a:e4:aa:0d:ce:32:83.
  5. Are you sure you want to continue connecting (yes/no)? yes
  6. Warning: Permanently added 'k3s-node3,192.168.6.193' (ECDSA) to the list of known hosts.
  7. root@k3s-node3's password:
  8. Last login: Sun Apr 7 11:09:20 2019 from 192.168.6.1
  •  在Ansible管理主机生成RSA密钥对(公钥/私钥)
  1. [root@k3s-vip ~]# ssh-keygen -t rsa
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/root/.ssh/id_rsa):
  4. Enter passphrase (empty for no passphrase):
  5. Enter same passphrase again:
  6. Your identification has been saved in /root/.ssh/id_rsa.
  7. Your public key has been saved in /root/.ssh/id_rsa.pub.
  8. The key fingerprint is:
  9. SHA256:3GNNAxe62c9R505oFL0mkoKYJ53d2XjvP0cIWEsjvX8 root@k3s-vip
  10. The key's randomart image is:
  11. +---[RSA 2048]----+
  12. | ..o... |
  13. | .+= ..|
  14. | + + o=B+. +|
  15. | + * +.%+* *.|
  16. | o S B =oB.o|
  17. | . . +o=E|
  18. | +o.|
  19. | o.|
  20. | =|
  21. +----[SHA256]-----+
  • 复制Ansible管理主机的公钥到远程目标主机
  1. [root@k3s-vip ~]# scp -p ~/.ssh/id_rsa.pub root@192.168.6.193:/root/.ssh/authorized_keys
  2. root@192.168.6.193's password:
  3. 100% 405 605.6KB/s 00:00
  4. id_rsa.pub
  • 再次验证SSH登录

从Ansible主机SSH登录到目标主机:

  1. [root@k3s-vip ~]# ssh 192.168.6.193
  2. 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脚本
  1. [root@k3s-vip ~]# mkdir -p /root/ansible
  2. [root@k3s-vip ~]# cd /root/ansible/
  3. [root@k3s-vip ansible]# vi show_hosts.sh
  4. [root@k3s-vip ansible]# chmod +x show_hosts.sh
  5. [root@k3s-vip ansible]# cat show_hosts.sh
  6. #!/usr/bin/bash
  7. date
  8. cat /etc/hosts
  • 在本地测试脚本
  1. [root@k3s-vip ~]# ./show_hosts.sh
  2. Sun Apr 7 14:42:22 CST 2019
  3. 127.0.0.1 localhost localhost.localdomain
  4. 192.168.6.190 k3s-vip
  5. 192.168.6.193 k3s-node3

输出了当前日期时间和/etc/hosts文件的内容。

  • 查看Ansible配置目录
  1. [root@k3s-vip ~]# ls -ln /etc/ansible/
  2. total 24
  3. -rw-r--r-- 1 0 0 19179 Jan 30 2018 ansible.cfg # Ansible配置文件
  4. -rw-r--r-- 1 0 0 1016 Jan 30 2018 hosts # Ansible主机文件
  5. drwxr-xr-x 2 0 0 6 Jan 30 2018 roles # 角色文件
  • 修改/etc/ansible/hosts文件

修改hosts文件,在文件最后添加主机组k3s-node以及组内主机IP:

[k3s-node]

192.168.6.193

  1. [root@k3s-vip ~]# cd /etc/ansible/
  2. [root@k3s-vip ansible]# vi hosts
  3. [root@k3s-vip ansible]# cat hosts
  4. # This is the default ansible 'hosts' file.
  5. #
  6. ...
  7. [k3s-node]
  8. 192.168.6.193
  • 测试远程主机的连通性
  1. [root@k3s-vip ansible]# ansible k3s-node -m ping
  2. 192.168.6.193 | SUCCESS => {
  3. "changed": false,
  4. "ping": "pong"
  5. }

子命令ping会依次向主机组k3s-node内的各个主机发出报文,验证网络连通性。

 

五、编写playbook剧本文件

 playbook是预先编写的描述性剧本,ansible会逐个执行playbook剧本内的命令。

  •  编写playbook文件

 playbook文件是yaml语言格式描述的。show_hosts.yaml剧本分为三个子任务:

复制shell脚本到远程主机;

在远程主机执行shell脚本,休眠3秒;

最后删除脚本。

  1. [root@k3s-vip ansible]# cd /root/ansible/
  2. [root@k3s-vip ansible]#
  3. [root@k3s-vip ansible]# cat show_hosts.yaml
  4. - name: a sample for show /etc/hosts
  5. hosts: k3s-node
  6. remote_user: root
  7. tasks:
  8. - name: copy show_hosts shell
  9. file:
  10. src: '{{ item.src }}'
  11. dest: '{{ item.dest }}'
  12. mode: 755
  13. with_items:
  14. - { src: '/root/ansible/show_hosts.sh', dest: '/usr/local/bin/'}
  15. - name: run shell
  16. shell: |
  17. /usr/local/bin/show_hosts.sh; sleep 3;
  18. - name: delete shell
  19. file:
  20. src: '{{ item.src }}'
  21. dest: '{{ item.dest }}'
  22. state: '{{ item.state }}'
  23. with_items:
  24. - { src: '', dest: '/usr/local/bin/show_hosts.sh', state: 'absent' }

万事具备,只欠东风了。激动人心的时刻即将到来。

 

六、远程执行playbook剧本

  • 远程执行playbook剧本

在Ansible主机运行ansible-playbook命令,按顺序执行playbook剧本子任务,从结果看所有子任务全部执行成功。

  1. [root@k3s-vip ansible]# ansible-playbook show_hosts.yaml
  2. PLAY [a sample for show /etc/hosts] **************************************************************************************************************************************************************************
  3. TASK [Gathering Facts] ***************************************************************************************************************************************************************************************
  4. ok: [192.168.6.193]
  5. TASK [copy show_hosts shell] *********************************************************************************************************************************************************************************
  6. changed: [192.168.6.193] => (item={u'dest': u'/usr/local/bin/', u'src': u'/root/ansible/show_hosts.sh'})
  7. TASK [run shell] *********************************************************************************************************************************************************************************************
  8. changed: [192.168.6.193]
  9. TASK [delete shell] ******************************************************************************************************************************************************************************************
  10. changed: [192.168.6.193] => (item={u'dest': u'/usr/local/bin/show_hosts.sh', u'src': u'', u'state': u'absent'})
  11. PLAY RECAP ***************************************************************************************************************************************************************************************************
  12. 192.168.6.193 : ok=4 changed=3 unreachable=0 failed=0
  13. [root@k3s-vip ansible]#

 

七、小结

  • 本文主要工作如下:

下载安装自动化运维工具Ansible;

建立管理主机和目标主机之间的SSH信任关系;

配置管理主机上的Ansible运行环境:远程目标主机组等;

编写例子脚本show_hosts.sh;

编写playbook剧本:复制shell脚本、远程执行脚本,最后删除脚本;

运行playbook。

  • 小结

Ansible是一个功能强大的IT运维工具,本文只是揭开冰山一角。本文的过程略显复杂,但是“写一次,处处运行”的好处的是显而易见的。

希望本文对读者有帮助,欢迎留言交流。

 

 

原文链接:http://www.cnblogs.com/solomonxu/p/10664207.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号