Centos7安装自动化运维Ansible

自动化运维Devops-Ansible

Ansible是新出现的自动化运维工具,基于Python 开发,集合了众多运维工具(puppet 、cfengine、chef、func、fabric)的优点,实现了批量系统配置 、批量程序部署、批量运行命令 等功能。Ansible架构相对比较简单,仅需通过SSH 连接客户机 执行任务即可

Ansible是一个同事管理多个远程主机的软件(ssh),可以是linux也可以是Windows主机

测试环境

|------------|----------------|---------|
| 操作系统版本 | IP 地址 | 主机名 |
| Centos7.6 | 192.168.68.133 | manage |
| Centos7.6 | 192.168.68.134 | node01 |
| Centos7.6 | 192.168.68.135 | node02 |

关闭selinux

修改/etc/sysconfig/selinux文件中的SELINUX=disable

sed -ri 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

#关闭防火墙

systemctl stop firewalld.service

systemctl disable firewalld.service

Ansible的认证方式有:SSH、公私钥认证

安装python3

yum -y install python3 python3-pip -y

Manage安装ansible

yum安装

安装依赖epel-release

yum -y install epel-release

#安装ansible

yum -y install ansible

ansible --version

二进制安装

可以自己选择版本

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

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命令语法

Ansible 192,168,68,124 -m shell -a "free -m"

Ansible webservers -m shell -a "free -m"

Ansible 192,168,68,124:200 -m shell -a "free -m"

Ansible主机清单

/etc/ansible/hosts

编写node组

vim + /etc/ansible/hosts

配置认证

认证

将master虚拟机的公钥分发给需要免密登录的虚拟机

ssh-copy-id root@192.168.68.134

ansible 192.168.68.134 -m shell -a "hostname"

密码认证

vim + /etc/ansible/hosts

默认ansible_user=root

Ansible连接指纹确认

~/.ssh/known_hosts文件存放了目标机器的指纹信息

首次连接确认需要指纹确认,可以忽略

host_key_checking = False 不做检查主机密钥

ansible报错

> app | FAILED! => {

> "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support

> this. Please add this host's fingerprint to your known_hosts fihost."

> }

解决办法:

vi /etc/ansible/ansible.cfg

在文件找到host_key_checking = False

修改前

uncomment this to disable SSH key host checking

#host_key_checking = False

修改后:就是把前面得#号去掉

uncomment this to disable SSH key host checking

host_key_checking = False

相关推荐
zzzsde9 分钟前
【Linux】线程同步和互斥(5):线程池的实现&&线程安全
linux·运维·服务器·开发语言·算法·安全
雾岛心情16 分钟前
【小铭邮件】小铭邮件工具箱公司版从PST提取EML邮件
运维·工具·exchage·o365·小铭邮件工具箱(公司版)
随便做点啥16 分钟前
鲲鹏CPU + 8卡910A NPU服务器大语言模型推理部署测试报告
运维·服务器·语言模型
非凡大爹17 分钟前
实验十 华为路由器和交换机实现RIP 动态路由协议配置实验指导书
运维·网络·计算机网络·华为
春日见23 分钟前
自动驾驶数据驱动规控进化之路
运维·服务器·人工智能·深度学习·算法·机器学习·自动驾驶
ACP广源盛1392462567325 分钟前
GSV2231@ACP#三屏扩展旗舰芯片,TRAE SOLO 多任务并行开发核心引擎
运维·网络·人工智能·嵌入式硬件·gpt·电脑·音视频
程序猿乐锅25 分钟前
Linux常用命令详解:目录、文件、压缩、编辑与查找
linux·运维·服务器
Shadow(⊙o⊙)25 分钟前
库的制作与原理1.0,库打包,协作,目标文件.o、ELF格式。
linux·运维·服务器·开发语言
ai_coder_ai25 分钟前
使用web ide开发和调试自动化脚本
前端·ide·自动化
AOwhisky35 分钟前
Ceph系列第五期:Ceph 对象存储(RADOS Gateway)精讲
linux·运维·笔记·ceph·gateway·对象存储