杭州搭建网站,闵行区网页设计公司,济南网站建设推荐q479185700上快,网页设计与制作实训报告心得本章主要介绍在RHEL8中如何安装ansible 及 ansible 的基本使用。
ansible是如何工作的在 RHEL8中安装ansible编写ansible.cfg和清单文件ansible 的基本用法
如果管理的服务器很多#xff0c;如几十台甚至几百台#xff0c;那么就需要一个自动化管理工具了#xff0c; ansi…本章主要介绍在RHEL8中如何安装ansible 及 ansible 的基本使用。
ansible是如何工作的在 RHEL8中安装ansible编写ansible.cfg和清单文件ansible 的基本用法
如果管理的服务器很多如几十台甚至几百台那么就需要一个自动化管理工具了 ansible就是这样的一种自动化管理工具。
ansible是通过ssh连接到被管理主机然后执行相关操作的如下图所示。
ansible主机通过ssh连接到被管理主机时需要提前设置密钥登录使得从ansible主机可以无密码登录到被管理主机。
本实验的拓扑图如下图所示。 这里server是 ansible主机以bdqn用户登录。server2和server3是被管理主机在这两台主机上创建bdqn用户并配置好sudo使得这两台主机上的bdqn用户通过sudo-i可以无 密码切换到root下面开始配置。 安装ansible 在server上安装ansible命令如下。
先安装系统的开发工具
步骤1、挂载镜像
操作略
配置本地yum源
操作略
安装开发工具
[rootrhel01 ~]# yum -y groupinstall 开发工具
RHEL8默认的Python版本为3.6不符合项目的需求。现在升级到3.9
[rootrhel01 ~]# python3 --version
Python 3.6.8
[rootrhel01 ~]#
下载Python3.9.5
[rootrhel01 ~]# wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5.tgz解压
[rootrhel01 ~]# ls | grep *.tgz
Python-3.9.9.tgz
[rootrhel01 ~]# tar zxvf Python-3.9.9.tgz 安装必须的包
[rootrhel01 ~]# yum -y install gcc zlib* libffi-devel安装Python
[rootrhel01 Python-3.9.9]# ./configure --prefix/usr/local/python3 --enable-optimizations
[rootrhel01 Python-3.9.9]# make
[rootrhel01 Python-3.9.9]# make install删除原先的Python3和pip3
[rootrhel01 Python-3.9.9]# rm -rf /usr/bin/python3
[rootrhel01 Python-3.9.9]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[rootrhel01 Python-3.9.9]# rm -rf /usr/bin/pip3
[rootrhel01 Python-3.9.9]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
[rootrhel01 Python-3.9.9]# 查看Python3和Pip3是否正确的被安装
[rootrhel01 Python-3.9.9]# python3 --version
Python 3.9.9
[rootrhel01 Python-3.9.9]# pip3 --version
pip 21.2.4 from /usr/local/python3/lib/python3.9/site-packages/pip (python 3.9)
[rootrhel01 Python-3.9.9]# 安装ansible
[rootrhel01 ~]# ls | grep *.rpm
ansible-2.9.11-1.el8ae.noarch.rpm
[rootrhel01 ~]# rpm -ivh ansible-2.9.11-1.el8ae.noarch.rpm
警告ansible-2.9.11-1.el8ae.noarch.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID fd431d51: NOKEY
错误依赖检测失败python3-cryptography 被 ansible-2.9.11-1.el8ae.noarch 需要python3-jinja2 被 ansible-2.9.11-1.el8ae.noarch 需要sshpass 被 ansible-2.9.11-1.el8ae.noarch 需要
[rootrhel01 ~]# 需要解决依赖包问题 前面两个使用本地yum源就可以解决了
[rootrhel01 ~]# yum -y install python3-cryptography python3-jinja2sshpass在下面地址下载 然后上传到rhel01
Non-interactive ssh password auth - Browse Files at SourceForge.net
[rootrhel01 ~]# ls | grep ssh*
sshpass-1.06-3.el8ae.x86_64.rpm
[rootrhel01 ~]# rpm -ivh sshpass-1.06-3.el8ae.x86_64.rpm
Verifying... ################################# [100%]
准备中... ################################# [100%]
正在升级/安装...1:sshpass-1.06-3.el8ae ################################# [100%]
[rootrhel01 ~]# 再次安装ansible
[rootrhel01 ~]# rpm -ivh ansible-2.9.11-1.el8ae.noarch.rpm
警告ansible-2.9.11-1.el8ae.noarch.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID fd431d51: NOKEY
Verifying... ################################# [100%]
准备中... ################################# [100%]
正在升级/安装...1:ansible-2.9.11-1.el8ae ################################# [100%]
[rootrhel01 ~]# 使用命令检查ansible的安装结果
[rootrhel01 ~]# ansible --version
ansible 2.9.11config file /etc/ansible/ansible.cfgconfigured module search path [/root/.ansible/plugins/modules, /usr/share/ansible/plugins/modules]ansible python module location /usr/lib/python3.6/site-packages/ansibleexecutable location /usr/bin/ansiblepython version 3.6.8 (default, Sep 9 2021, 07:49:02) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
[rootrhel01 ~]# 这里安装的ansible的版本是2.9.11同时也显示ansible的默认配置是 letc/ansible/ansible.cfgo还要确保ansible主机能够解析所有的被管理机器这里通过配 置/etc/hosts 来实现/etc/hosts的内容如下。
[rootrhel01 ~]# tail -3 /etc/hosts
192.168.23.31 rhel01 server
192.168.23.32 rhel02 server2
192.168.23.33 rhel03 server3
[rootrhel01 ~]# 在server2和 server3两台机器上确认已经创建好了bdqn用户如果没有请自行创建然 后配置好sudo命令如下。
[rootrhel02 ~]# cat /etc/sudoers.d/bdqn
bdqn ALL(root) NOPASSWD:ALL
[rootrhel02 ~]#
[rootrhel03 ~]# cat /etc/sudoers.d/bdqn
bdqn ALL(root) NOPASSWD:ALL
[rootrhel03 ~]#
这样在这两台机器上bdqn用户通过sudo-i可以无密码切换到root用户。
使用bdqn用户登录server配置好ssh密钥登录使得bdqn用户可以无密码登录到 server2和 server3命令如下。
[bdqnrhel01 ~]$ ssh-keygen -f ~/.ssh/id_rsa -N
[bdqnrhel01 ~]$ ssh-copy-id bdqn192.168.23.32
[bdqnrhel01 ~]$ ssh bdqn192.168.23.32
Activate the web console with: systemctl enable --now cockpit.socketThis system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --registerLast login: Fri Dec 15 16:49:39 2023
[bdqnrhel02 ~]$ 这里只演示配置到server2的秘钥登录server3同样操作 编写ansible.cfg和清单文件
执行ansible或ansible-playbook命令时优先使用当前目录中ansible.cfg的配置。如果当前目录中没有则使用默认的/etc/ansible.cfg中的配置。
下面的操作都是server上的bdqn用户操作的先在家目录下创建ansible.cfg内容如下。
[bdqnrhel01 ~]$ cat ansible.cfg
[defaults]
inventory ./hosts[privilege_escalation]
becomeTrue
become_methodsudo
become_userroot
[bdqnrhel01 ~]$ 这里在[defaults]字段下只添加了一句inventory ./hosts表示把当前目录下名称为 hosts的文件当作清单文件什么是清单文件马上就要讲到)。
在 [privilege_escalation]字段下定义了如何提升权限因为是使用bdqn用户登录到被管 主机的,所以需要提升权限。这个字段下写了3条分别如下。
becomeTrue:登录到被管理主机时要切换到其他用户。become_methodsudo:以 sudo的方式切换。become_user-root :切换到root用户。
这三句的意思是当用ssh登录到被管理主机时以 sudo的方式切换到root这也是为 什么一开始要在被管理主机上配置好sudo的原因
所有的被管理机器都要写入清单文件中。在实验环境中有两台被管理主机那么分别写在 hosts中,内容如下。
[bdqnrhel01 ~]$ cat hosts
server2
server3
[bdqnrhel01 ~]$ 这里一行一台主机我们在使用ansible或ansible-playbook命令时指定的主机名必须 是这个名称才行。要确保能解析server2和server3写成相应的IP也可以。 ansible的基本用法
ansible的基本用法如下。 ansible 机器名 ‐m 模块× ‐a 模块的参数 这里的机器名必须出现在清单文件中整体的意思是在指定的机器上执行模块x。例如,在 server3上执行hostname命令,命令如下。
[bdqnrhel01 ~]$ ansible server3 -m shell -a hostname
server3 | CHANGED | rc0
rhel03
[bdqnrhel01 ~]$ shell模块用于执行操作系统命令执行的命令就作为shell模块的参数这里在-a中写要 执行的系统命令。所以上面的命令就是在server3上执行hostname命令显示的结果正是 rhel03
要完成不同的任务就需要调用不同的模块来实现系统中存在的所有 ansible模块可以通 过ansible-doc -l来查看。
不同的模块有不同的参数模块的参数及使用方法可以通过“ansible-doc模块名”来查 看。
我们将在第下一章中讲解常见的ansible模块。