6

Centos 7安装ansible自动化运维工具 - 谭咏麟

 1 year ago
source link: https://www.cnblogs.com/Merbleue/p/17241418.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Centos 7安装ansible自动化运维工具

1、介绍:

    ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、SaltStack、chef、func)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
    本期为大家展示安装ansible和一些基本的配置和命令使用,后期会讲述具体一些模块和其他内容。

2、部署环境

操作系统版本 IP地址 主机名
Centos7.6 192.168.0.200 manage
Centos7.6 192.168.0.201 node01
Centos7.6 192.168.0.202 node02
1983031-20230321205817463-692833740.png

关闭selinux和防火墙

# 关闭selinux
修改/etc/sysconfig/selinux文件中的SELINUX=disable
sed -ri 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

#关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service

3、安装ansible

  • yum安装
# 安装依赖epel-release
yum -y install epel-release
#安装ansible
yum -y install ansible
  • 二进制编译安装
# 可以自己选择版本
wget https://releases.ansible.com/ansible/ansible-2.9.3.tar.gz
# 解压安装
tar xvf ansible-2.9.3.tar.gz
mv ansible-2.9.3.tar.gz /opt/ansible
cd /opt/ansible
# 安装依赖,pip3为python的安装,先确认是否安装python3
pip3 install -r requirements.txt -i https://pypi.yuna.tsinghua.edu.cn/simple/
# 创建软连接
ln -s /usr/bin/pip3 /usr/bin/pip
# 安装ansible
pip install --user ansible -i https://pypi.yuna.tsinghua.edu.cn/simple/
# 创建python软连接
ln -s /usr/bin/python3 /usr/bin/python
# 创建ansible软连接
ln -s /opt/ansible/bin/* /usr/bin
# 将配置文件复制到ansible 工作路径下
cp /opt/ansible/examples/ansible.cfg /etc/ansible
cp /opt/ansible/examples/hosts /etc/ansible

4、ansible配置文件

  • ansible的主配置文件为/etc/ansible/ansible.cfg
inventory :管理的主机清单文件路径
library:ansible的模块存放的目录
remote_tmp:上述工作原理中提到的将脚本发送至对端的临时目录
local_tmp:上述工作原理中提到本地生成脚本存放的临时目录
forks:并发连接数,默认为5
sudo_user :命令执行用户
remote_port :访问管理主机的端口        
host_key_checking:设置是否检查SSH主机的密钥,默认为false
timeout :ssh连接被管理主机的超时时间
log_path:ansilbe日志文件路径
  • ansible的主机配置文件为/etc/ansible/hosts
# 这个清单就是我们的主机清单,里面保存的是一些 ansible 需要连接管理的主机列表
1、直接指明主机地址或者主机名
## green.example.com
## blue.example.com
192.168.0.200
192.168.0.201
node02
# 主机地址范围
192.168.0.[200:202]

2、定义一个主机组[组名]把地址或主机名加进去
例如mysql组
[mysqlserver]
192.168.0.201

web组
[webserver]
192.168.0.201
192.168.0.202
# 根据自己需求修改配置

5、ansbile常用命令

# 查看ansible版本
ansible --version
1983031-20230321205909897-1403326790.png
# 常用于获取模块信息及其使用帮助
ansible-doc -l | grep mysql
# 例如查找出mysql相关的模块信息
1983031-20230321205937280-1560402240.png
# ansible命令格式如下
ansible <host-pattern> [-f forks] [-m module_name] [-a args]

6、配置服务器间的ssh免密登录

# 在主控机上生成密钥
ssh-keygen # 接下来一路回车即可
# 将密钥传给客户端
ssh-copy-id [email protected]
ssh-copy-id [email protected]
# 期间会输一次客户端的登录密码即可
1983031-20230321205956912-2113828194.png

/

1983031-20230321211028803-1268203757.png

/

1983031-20230321211209077-298591553.png

/

7、使用ansible的ping模块进行验证

# 测试两台客户机网络是否连通
ansible 192.168.0.201 -m ping
1983031-20230321210037033-1230749831.png
# 使用分组的host主机来验证
ansible webserver -m ping
1983031-20230321210047624-758199476.png

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK