通过以下步骤在RHEL 8/CentOS 8系统上安装和配置Ansible。
简介 Ansible由Red Hat主导,它是领先的开源配置管理系统,它使管理员和运营团队可以轻松地从中央机器控制数千台服务器,而无需在其上安装代理。与其他配置管理系统(如Puppet,Chef和Salt)相比,Ansible是最简单的使用和管理方式。它易于安装,学习和使用。远程服务器上唯一需要的依赖项是SSH服务和Python。
第一步、在RHEL 8/CentOS 8上安装Python 1、要在RHEL 8上安装和设置默认Python,请参考在RHEL 8系统上安装Python 3和Python 2.7及设置默认Python版本一文,按照里面的步骤安装Python,方法同样适用在CentOS 8系统中。 2、安装完成后,继续安装Pip,这是一个用于安装Ansible的Python包管理器。 1]、如果使用的是Python3,请安装python3-pip包: sudo yum -y install python3-pip sudo pip3 install --upgrade pip 2]、对于Python2用户,必须安装python2-pip: sudo yum -y install python2-pip sudo pip2 install --upgrade pip
第二步、在RHEL 8/CentOS 8上安装Ansible 1、安装Pip后,使用它来安装RHEL 8计算机中的Ansible: $ pip3 install ansible --user 注:对于Python2,请使用下面命令: $ pip2 install ansible --user 2、然后使用以下命令查看Ansible安装成果: $ ansible --version ansible 2.7.5 config file = None configured module search path = ['/home/jmutai/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /home/jmutai/.local/lib/python3.6/site-packages/ansible executable location = /home/jmutai/.local/bin/ansible python version = 3.6.6 (default, Oct 16 2018, 01:53:53) [GCC 8.2.1 20180905 (Red Hat 8.2.1-3)]
第三步、在RHEL 8/CentOS 8上测试Ansible 1、要测试Ansible,应该在远程服务器上运行OpenSSH服务: $ sudo systemctl status sshd sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2018-12-29 20:17:11 EAT; 39min ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 820 (sshd) Tasks: 1 (limit: 11510) Memory: 4.6M CGroup: /system.slice/sshd.service └─820 /usr/sbin/sshd -D -oCiphers=aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr,aes256-cbc,aes128-gcm@openssh.com,aes128-> Dec 29 20:17:11 rhel8.local systemd[1]: Starting OpenSSH server daemon… Dec 29 20:17:11 rhel8.local sshd[820]: Server listening on 0.0.0.0 port 22. Dec 29 20:17:11 rhel8.local sshd[820]: Server listening on :: port 22. Dec 29 20:17:11 rhel8.local systemd[1]: Started OpenSSH server daemon. Dec 29 20:19:03 rhel8.local sshd[1499]: Accepted publickey for jmutai from 192.168.122.1 port 35902 ssh2: RSA SHA256:b/8AoYgbThoBYPcFh7CetJuGY/Tl7s4fi> Dec 29 20:19:03 rhel8.local sshd[1499]: pam_unix(sshd:session): session opened for user jmutai by (uid=0) 2、创建Ansible库存文件,默认为/etc/ansible/hosts,然后在工作目录中创建库存文件: $ vim hosts 3、复制远程服务器的IP地址以管理并添加到Ansible清单文件: $ echo "192.168.122.197" > hosts 4、你还可以创建一组主机,如下所示: [web] 192.168.122.197 [db] 192.168.122.198 [staging] 192.168.122.199 192.168.122.200 192.168.122.201 5、生成SSH密钥并将其复制到远程服务器: $ ssh-keygen $ ssh-copy-id jmutai@192.168.122.197 6、使用ping模块测试ansible: $ ansible -i hosts 192.168.122.197 -m ping 192.168.122.197 | SUCCESS => { "changed": false, "ping": "pong" } 7、-i选项用于提供库存文件的路径,你应该获得主机组名称的相同输出: $ ansible -i hosts web -m ping 192.168.122.197 | SUCCESS => { "changed": false, "ping": "pong" } 8、对于需要sudo的命令,请传递选项--ask-become-pass。这将要求提供权限的密码,这可能需要安装sshpass程序: $ ansible -i hosts web -m command -a "sudo yum install vim" --ask-become-pass .... 192.168.122.197 | CHANGED | rc=0 >> Updating Subscription Management repositories. Updating Subscription Management repositories. Last metadata expiration check: 0:52:23 ago on Sat 29 Dec 2018 08:28:46 PM EAT. Package vim-enhanced-2:8.0.1763-7.el8.x86_64 is already installed. Dependencies resolved. Nothing to do. Complete! 到这里,已经在RHEL 8/CentOS 8系统上安装Ansible成功并且通过了测试。
相关主题 |