自动化运维之前奏–Ansible安装与配置

  • A+

Ansible基于python开发,集合了众多优秀运维工具的优点,实现了批量运行命令、部署程序、配置系统等功能。默认通过SSH协议进行远程命令执行或下发配置,无需部署任何客户端代理软件,从而使得自动化环境部署变得更加简单。可同时支持多台主机并进行管理,使得管理主机更加便捷。

一.升级python版本

1.查看当前版本
  1. [root@localhost ~]# python -V  
  2. Python 2.7.5  
  3. [root@localhost ~]#         
2.启用EPEL和SCL存储库
  1. [root@localhost ~]# yum install epel-release centos-release-scl    
  2. 已加载插件:fastestmirror, langpacks  
  3. Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast  
  4. Determining fastest mirrors  
  5. epel/x86_64/metalink                                           
  6. ……  
3.安装Python 3.6
  1. [root@localhost ~]# yum install rh-python36 git gcc wget nodejs-less libxslt-devel bzip2-devel openldap-devel libjpeg-devel freetype-devel    
  2. 已加载插件:fastestmirror, langpacks  
  3. Loading mirror speeds from cached hostfile  
  4.  * base: mirrors.aliyun.com  
  5.  * centos-sclo-rh: mirrors.huaweicloud.com  
  6.  * centos-sclo-sclo: mirrors.huaweicloud.com  
  7.  * epel: d2lzkl7pfhq30w.cloudfront.net  
  8.  * extras: mirrors.bfsu.edu.cn  
  9.  * updates: mirrors.huaweicloud.com  
  10. centos-sclo-rh                                                                                                                                                            | 3.0 kB  00:00:00       
  11. centos-sclo-sclo    
  12. ……  
  1. [root@localhost ~]#  scl enable rh-python36 bash    
  2. [root@localhost ~]  
5.查看升级后的版本
  1. [root@localhost ~]# python -V  
  2. Python 3.6.9  

二.安装ansible

1.采用软件源安装
  1. [root@localhost ~]# yum install epel-release && yum install ansible  
  2. 已加载插件:fastestmirror, langpacks  
  3. Loading mirror speeds from cached hostfile  

它的优点如下:
简单、快速、跨平台。

2.pip安装

  1. [root@localhost ~]# pip install ansible  
  2. Collecting ansible  
  3.   Downloading https://files.pythonhosted.org/packages/4a/0b/44b586965bd51135d3915a02d1327fb392843630435cd41d6c89898c5f24/ansible-2.10.0.tar.gz (25.5MB)  
  4.     100% |████████████████████████████████| 25.5MB 15kB/s   
  5. Collecting ansible-base<2.11,>=2.10.1 (from ansible)  

3.Ansible版本查看

  1. [root@localhost ~]# ansible --version  
  2. ansible 2.10.1  
  3.   config file = /etc/ansible/ansible.cfg  
  4.   configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']  
  5.   ansible python module location = /opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/ansible  
  6.   executable location = /opt/rh/rh-python36/root/usr/bin/ansible  
  7.   python version = 3.6.9 (default, Nov 11 2019, 11:24:16) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]  
  8. [root@localhost ~]  

三.配置Ansible并做测试

主要思路:配置host-->配置ssh-->连通测试
1.host文件配置.
这里添加要管理的主机IP信息,如下:
192.168.150.121
192.168.150.71

  1. /etc/ansible/hosts  
  2. ## db-[99:101]-node.example.com  
  3. [mytest]  
  4. 192.168.150.121  
  5. 192.168.150.71  
  6. [root@localhost ansible]  

2.配置远程登录

这里我们在管理端,生成密钥对并将公钥推送给被管理端。

  1. [root@localhost /]# 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. Created directory '/root/.ssh'.  
  5. Enter passphrase (empty for no passphrase):   
  6. Enter same passphrase again:   
  7. Your identification has been saved in /root/.ssh/id_rsa.  
  8. Your public key has been saved in /root/.ssh/id_rsa.pub.  
  9. The key fingerprint is:  
  10. ab:9f:ba:5d:39:33:ae:a4:5a:13:4e:9e:79:33:3e:3e root@localhost.localdomain  
  11. The key's randomart image is:  
  12. +--[ RSA 2048]----+  
  13. |                 |  
  14. |                 |  
  15. |                 |  
  16. |                 |  
  17. |      o S        |  
  18. |     + + . .     |  
  19. |      B * *      |  
  20. |     . OE* +     |  
  21. |    ..==B+.      |  
  22. +-----------------+  
  23. [root@localhost /]  

3.将管理机上生成的秘钥发送到被管理机

  1. [root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.150.71  
  2. The authenticity of host '192.168.150.71 (192.168.150.71)' can't be established.  
  3. ECDSA key fingerprint is 10:15:bf:ca:b5:aa:b5:74:23:68:24:1e:e0:91:0c:fa.  
  4. Are you sure you want to continue connecting (yes/no)? yes  
  5. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed  
  6. /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys  
  7. root@192.168.150.71's password:   
  8.   
  9. Number of key(s) added: 1  
  10.   
  11. Now try logging into the machine, with:   "ssh 'root@192.168.150.71'"  
  12. and check to make sure that only the key(s) you wanted were added.  
  13.   
  14. [root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.150.121  
  15. The authenticity of host '192.168.150.121 (192.168.150.121)' can't be established.  
  16. ECDSA key fingerprint is cd:09:1a:1a:4d:a9:d0:4d:d0:29:45:c4:6f:84:fd:14.  
  17. Are you sure you want to continue connecting (yes/no)? yes  
  18. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed  
  19. /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys  
  20. root@192.168.150.121's password:   
  21.   
  22. Number of key(s) added: 1  
  23.   
  24. Now try logging into the machine, with:   "ssh 'root@192.168.150.121'"  
  25. and check to make sure that only the key(s) you wanted were added.  
  26.   
  27. [root@localhost ~]  

4.测试登录被管理主机

我们先做SSH登录测试

  1. [root@localhost ~]# ssh root@192.168.150.71  
  2. Last login: Thu Sep 24 11:19:21 2020 from 10.128.25.130  
  3. [root@node01 ~]#  
  4. [root@node01 ~]# exit  
  5. 登出  
  6. Connection to 192.168.150.71 closed.  
  7. [root@localhost ~]# ssh root@192.168.150.121  
  8. Last login: Thu Sep 24 11:19:21 2020 from 10.128.25.130  
  9. [root@yunkzbd ~]  

上面两个都登录成功,接着进行ansible登录测试,登录时指定用户

  1. [root@localhost ~]# ansible 192.168.150.71 -m ping --user=root  
  2. 192.168.150.71 | SUCCESS => {  
  3.     "ansible_facts": {  
  4.         "discovered_interpreter_python""/usr/bin/python"  
  5.     },  
  6.     "changed": false,  
  7.     "ping""pong"  
  8. }  
  9. [root@localhost ~]  

#
连通成功,返回一个pong。

四.Ansible命令执行方式

有两种:
ad-hoc:主要用于临时命令的执行
ansible-playbook:是ad-hoc命令的集合,通过一定的规划编排在一起,实现一个完整的功能。这也是我的常用方法。

moonrong
  • 版权声明:本站原创文章,于2020年9月24日14:00:04,由 发表,共 5602 字。
  • 版权声明: 本文由于2020年9月24日14:00:04 发表在 好派笔记,共 5602 字。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: