Ansible

目录

Ansible是什么?

[ansible 具有哪些特点?](#ansible 具有哪些特点?)

[ansible 环境安装部署](#ansible 环境安装部署)

[ansible 命令行模块](#ansible 命令行模块)

[command 模块](#command 模块)

[shell 模块](#shell 模块)

[cron 模块](#cron 模块)

[user 模块](#user 模块)

[group 模块](#group 模块)

[copy 模块](#copy 模块)

[file 模块](#file 模块)

[hostname 模块](#hostname 模块)

[ping 模块](#ping 模块)

[yum 模块](#yum 模块)

[service/systemd 模块](#service/systemd 模块)

[script 模块](#script 模块)

[setup 模块](#setup 模块)

[inventory 主机清单](#inventory 主机清单)


Ansible是什么?

Ansible是一种开源的自动化工具,用于配置管理、应用部署和任务协调。它可以帮助系统管理员和开发人员自动化各种重复性的任务,从而提高效率、减少人为错误。

具体来说,Ansible有以下几个主要的功能和用途:

  1. 配置管理:Ansible可以用于管理和配置大量的服务器和网络设备。通过编写基于文本的Ansible Playbooks(剧本),用户可以定义所需的配置状态和操作,并将其应用于目标主机,以实现统一的配置管理。

  2. 应用部署:Ansible可以帮助自动化应用程序的部署和更新。用户可以使用Ansible Playbooks来描述应用部署过程,包括安装依赖、配置文件管理、服务启动等,从而简化和加速应用程序的部署流程。

  3. 任务协调:Ansible可以用于协调和执行各种任务,例如批量命令执行、文件传输、服务启停等。用户可以通过编写Ad-hoc命令或者使用Ansible Playbooks来定义和执行这些任务,从而实现批量操作和集中式管理。

  4. 云基础设施管理:Ansible提供了丰富的模块和插件,可以用于管理各种云平台的基础设施资源,例如虚拟机、存储、网络等。用户可以使用Ansible来自动化云基础设施的创建、配置和管理。

ansible 具有哪些特点?

1、部署简单,只需在主控端部署Ansible环境, 被控端无需做任何操作

2、默认使用SSH协议设备进行管理;

3、主从集中化管理

4、配置简单、功能强大、扩张性强;

5、支持API及自定义模块,可以通过Pyhton轻松扩展

6、通过playbooks 来定制强大的配置、状态管理

7、对云计算平台、大数据都有很好的支持

ansible 环境安装部署

管理端: 192.168.90.40 ansible

被管理端: 192.168.90.50

被管理端: 192.168.90.60

#只需要管理端安装 ansible

yum install -y epel-release

yum install -y ansible

配置主机清单:

cd /etc/ansible

vim hosts

[webservers] #配置组名

192.168.90.50 #组里包含的被管理的主机IP地址或主机名(主机名需要先修改/etc/hosts文件)

[dbservers]

192.168.90.60

//配置密钥对验证

ssh-keygen -t rsa #一路回车,使用免密登录

sshpass -p '123456' ssh-copy-id root@192.168.90.50

sshpass -p '123456' ssh-copy-id root@192.168.90.60

ansible 命令行模块

命令格式:ansible <组名> -m <模块> -a <参数列表>

ansible-doc -l #列出所有已安装的模块,按q退出

command 模块

//在远程主机执行命令,不支持管道,重定向等shell的特性。

ansible-doc -s command #-s 列出指定模块的描述信息和操作动作

ansible 192.168.90.50 -m command -a 'date' #指定 ip 执行 date

ansible webservers -m command -a 'date' #指定组执行 date

ansible dbservers -m command -a 'date'

ansible all -m command -a 'date' #all 代表所有 hosts 主机

ansible all -a 'ls /' #如省略 -m 模块,则默认运行 command 模块

//常用的参数:

chdir:在远程主机上运行命令前提前进入目录

creates:判断指定文件是否存在,如果存在,不执行后面的操作

removes:判断指定文件是否存在,如果存在,执行后面的操作

ansible all -m command -a "chdir=/home ls ./"

shell 模块

//在远程主机执行命令,相当于调用远程主机的shell进程,然后在该shell下打开一个子shell运行命令(支持管道符号等功能)

ansible-doc -s shell

ansible dbservers -m shell -a 'echo 123456 | passwd --stdin test'

ansible dbservers -m shell -a 'echo $(ifconfig ens33 | awk "NR==2 {print $2}") | cut -d " " -f2'

ansible dbservers -m shell -a 'echo $(ifconfig ens33 | awk "NR==2 {print \$2}")'

cron 模块

//在远程主机定义任务计划。其中有两种状态(state):present表示添加(可以省略),absent表示移除。

ansible-doc -s cron #按 q 退出

//常用的参数:

minute/hour/day/month/weekday:分/时/日/月/周

job:任务计划要执行的命令

name:任务计划的名称

ansible webservers -m cron -a 'minute="*/1" job="/bin/echo helloworld" name="test crontab"'

ansible webservers -a 'crontab -l'

ansible webservers -m cron -a 'name="test crontab" state=absent' #移除计划任务,假如该计划任务没有取名字,name=None即可

user 模块

//用户管理的模块

ansible-doc -s user

//常用的参数:

name:用户名,必选参数

state=present|absent:创建账号或者删除账号,present表示创建,absent表示删除

system=yes|no:是否为系统账号

uid:用户uid

group:用户基本组

shell:默认使用的shell

move_home=yse|no:如果设置的家目录已经存在,是否将已经存在的家目录进行移动

password:用户的密码,建议使用加密后的字符串

comment:用户的注释信息

remove=yes|no:当state=absent时,是否删除用户的家目录

ansible dbservers -m user -a 'name="test01"' #创建用户test01

ansible dbservers -m command -a 'tail /etc/passwd'

ansible dbservers -m user -a 'name="test01" state=absent' #删除用户test01

group 模块

//用户组管理的模块

ansible-doc -s group

ansible dbservers -m group -a 'name=mysql gid=306 system=yes' #创建mysql组

ansible dbservers -a 'tail /etc/group'

ansible dbservers -m user -a 'name=test01 uid=306 system=yes group=mysql' #将test01用户添加到mysql组中

ansible dbservers -a 'tail /etc/passwd'

ansible dbservers -a 'id test01'

copy 模块

//用于复制指定主机文件到远程主机的

ansible-doc -s copy

//常用的参数:

dest:指出复制文件的目标及位置,使用绝对路径,如果是源目录,指目标也要是目录,如果目标文件已经存在会覆盖原有的内容

src:指出源文件的路径,可以使用相对路径或绝对路径,支持直接指定目录,如果源是目录则目标也要是目录

mode:指出复制时,目标文件的权限

owner:指出复制时,目标文件的属主

group:指出复制时,目标文件的属组

content:指出复制到目标主机上的内容,不能与src一起使用

ansible dbservers -m copy -a 'src=/etc/fstab dest=/opt/fstab.bak owner=root mode=640'

ansible dbservers -a 'ls -l /opt'

ansible dbservers -a 'cat /opt/fstab.bak'

ansible dbservers -m copy -a 'content="helloworld" dest=/opt/hello.txt' #将helloworld写入/opt/hello.txt文件中

ansible dbservers -a 'cat /opt/hello.txt'

file 模块

ansible-doc -s file #设置文件属性

ansible dbservers -m file -a 'owner=test01 group=mysql mode=644 path=/opt/fstab.bak' #修改文件的属主属组权限等

ansible dbservers -m file -a 'path=/opt/fstab.link src=/opt/fstab.bak state=link' #设置/opt/fstab.link为/opt/fstab.bak的链接文件

ansible dbservers -m file -a "path=/opt/abc.txt state=touch" #创建一个文件

ansible dbservers -m file -a "path=/opt/abc.txt state=absent" #删除一个文件

hostname 模块

#用于管理远程主机上的主机名

ansible dbservers -m hostname -a "name=mysql01"

ping 模块

#检测远程主机的连通性

ansible all -m ping

yum 模块

在远程主机上安装与卸载软件包

ansible-doc -s yum

ansible webservers -m yum -a 'name=httpd' #安装服务

ansible webservers -m yum -a 'name=httpd state=absent' #卸载服务

service/systemd 模块

#用于管理远程主机上的管理服务的运行状态

ansible-doc -s service

//常用的参数:

name:被管理的服务名称

state=started|stopped|restarted:动作包含启动关闭或者重启

enabled=yes|no:表示是否设置该服务开机自启

runlevel:如果设定了enabled开机自启去,则要定义在哪些运行目标下自启动

ansible webservers -a 'systemctl status httpd' #查看web服务器httpd运行状态

ansible webservers -m service -a 'enabled=true name=httpd state=started' #启动httpd服务

script 模块

#实现远程批量运行本地的 shell 脚本

ansible-doc -s script

vim test.sh

#!/bin/bash

echo "hello ansible from script" > /opt/script.txt

chmod +x test.sh

ansible webservers -m script -a 'test.sh'

ansible webservers -a 'cat /opt/script.txt'

setup 模块

#facts 组件是用来收集被管理节点信息的,使用 setup 模块可以获取这些信息

ansible-doc -s setup

ansible webservers -m setup #获取mysql组主机的facts信息

ansible dbservers -m setup -a 'filter=*ipv4' #使用filter可以筛选指定的facts信息

inventory 主机清单

Inventory(主机清单)是 Ansible 中用于定义目标主机和组织结构的文件。它指定了 Ansible 在哪些主机上执行任务或剧本。Inventory支持对主机进行分组,每个组内可以定义多个主机,每个主机都可以定义在任何一个或多个主机组内。

//如果是名称类似的主机,可以使用列表的方式标识各个主机。

vim /etc/ansible/hosts

[webservers]

192.168.90.50:2222 #冒号后定义远程连接端口,默认是 ssh 的 22 端口

192.168.90.1[2:5]

[dbservers]

db-[a:f].example.org #支持匹配 a~f

//inventory 中的变量

Inventory变量名 含义

ansible_host ansible连接节点时的IP地址

ansible_port 连接对方的端口号,ssh连接时默认为22

ansible_user 连接对方主机时使用的主机名。不指定时,将使用执行ansible或ansible-playbook命令的用户

ansible_password 连接时的用户的ssh密码,仅在未使用密钥对验证的情况下有效

ansible_ssh_private_key_file 指定密钥认证ssh连接时的私钥文件

ansible_ssh_common_args 提供给ssh、sftp、scp命令的额外参数

ansible_become 允许进行权限提升

ansible_become_method 指定提升权限的方式,例如可使用sudo/su/runas等方式

ansible_become_user 提升为哪个用户的权限,默认提升为root

ansible_become_password 提升为指定用户权限时的密码

(1)主机变量

[webservers]

192.168.90.40 ansible_port=22 ansible_user=root ansible_password=123456

(2)组变量

[webservers:vars] #表示为 webservers 组内所有主机定义变量

ansible_user=root

ansible_password=123456

[all:vars] #表示为所有组内的所有主机定义变量

ansible_port=22

(3)组嵌套

[nginx]

192.168.90.40

192.168.90.50

192.168.90.60

[apache]

192.168.90.3[4:6]

[webs:children] #表示为 webs 主机组中包含了 nginx 组和 apache 组内的所有主机

nginx

apache

相关推荐
XY.散人10 分钟前
初识Linux · 文件(1)
linux·运维·服务器
秋夫人1 小时前
http cache-control
网络·网络协议·http
不灭锦鲤2 小时前
ssrf学习(ctfhub靶场)
网络·学习·安全
weixin_548444263 小时前
2024年最新版本神马TV8.5影视APP源码 293TV影视点播系统源码搭建教程 神马TV8.2加强版反编译教程 保姆级小白可搭建 完整版本视频教程
网络
枫叶丹45 小时前
【在Linux世界中追寻伟大的One Piece】进程信号
linux·运维·服务器
网络研究院5 小时前
如何安全地大规模部署 GenAI 应用程序
网络·人工智能·安全·ai·部署·观点
limengshi1383925 小时前
通信工程学习:什么是RIP路由信息协议
网络·网络协议·学习·智能路由器·信息与通信
灯火不休ᝰ6 小时前
[win7] win7系统的下载及在虚拟机中详细安装过程(附有下载文件)
linux·运维·服务器
数云界9 小时前
如何在 DAX 中计算多个周期的移动平均线
java·服务器·前端
limengshi1383929 小时前
通信工程学习:什么是TFTP简单文件传输协议
网络·网络协议·学习·信息与通信