网站架构知识之Ansible模块(day021)

1.Ansible模块

作用:通过ansible模块实现批量管理

2.command模块与shell模块

command模块是ansible默认的模块,适用于执行简单的命令,不支持特殊符号

案列01,批量获取主机名

ansible all -m command -a 'hostname'

all表示对主机清单所有组进行操作,-m command表示使用command模块(默认是此模块),-a指定模块中的选项。

shell模块相比command可以识别特殊符号

案列02,删除/tmp目录下的所有文件

ansible all -m shell -a 'rm -rf /tmp/*'

3.script模块

执行流程:分发脚本,在被管理端执行脚本

案例03,被管理端执行指定目录下的脚本

ansible all -m script -a '/server/scripts/ansible-scripts.sh'

4.file模块

管理文件,目录,软链接

相关选项

path 路径(目录,文件)(必须要写)

state state=directory 创建目录

state=file 默认,更新文件,文件不存在也不创建

state=link 创建软连接

state=touch 创建文件

state=absent 删除

mode mode=755 创建并修改权限

owner owner=root

group group=root

案例03:所有被管理端的/tmp目录下创建day021.txt

ansible all -m file -a 'path=/tmp/day021.txt state=touch'

案例04:将/tmp目录下的day021.txt新建软连接到tmpls目录中

ansible all -m file -a 'src=/tmp/day021.txt path=/tmpls/day021.txt state=link'

5.copy模块

相关选项

src 管理端源文件

dest 被管理端目录/文件

backup backup=yes,会在覆盖前备份

mode 修改权限

owner 修改为指定所有者

group 修改为指定用户组

案例05:将管理端hosts文件发送给被管理端/tmp/day021/的目录下

ansible all -m copy -a 'src=/etc/hosts dest=/tmp/day021/'

5.systemd模块

name 用于指定服务名称

enabled yes开机自启动

state started 开启

stopped 关闭

reloaded 重载

restarted 重启

daemon-reload yes是否重新加载对应服务的管理配置文件

6.yum模块

包含了yum和apt

name 指定软件包名字

state installed安装

removed删除

lastest安装或更新

update_cache 更新本地yum缓存

7.get_url模块

url 指定要下载的地址

dest 下载到哪一个目录

后续可以调用yum模块安装本地的软件 name=/app/tools/xxxxx.rpm即可

8.user模块

name 用户名

uid 指定uid

group 指定用户组

shell 指定解释器

crete_home (yes/no)是否创建用户家目录

state present(添加)absent(删除)

password 加密的密码

更新密码

ansible all -m user -a "name=用户名 password={{ '新密码' | password_hash('sha512', '加密字符串') }} state=present"

9.mount模块

实现mount命令进行挂载,同时修改/etc/fstab进行永久挂载

fstype 指定文件系统

src 源地址(挂载nfs,eg 172.16.1.31:/data)

path 挂载点

state mounted,挂载并修改fstab

absent,卸载并修改fstab

unmounted,卸载但不修改fstab

present,仅修改fstab,不挂载

10.cron模块

-name 定时任务名字,对应注释内容

minute 分钟 minute="*/2"

hour 默认*

day

month

week

job 脚本位置

state present(添加),absent(删除)

将时间同步写到定时任务,每五分钟同步一次

ansible all -m cron -a 'name=sync_time minute="*/5" job="/usr/sbin/ntpdate ntp1.aliyun.com &>/dev/null" state=present'

结果

相关推荐
lbb 小魔仙10 小时前
【Linux】Ansible 自动化运维实战:2000+ 节点配置标准化教程
linux·运维·ansible
扑火的小飞蛾3 天前
【Ansible学习笔记01】 批量执行 shell 命令
笔记·学习·ansible
oMcLin3 天前
如何在 Red Hat Linux 服务器上使用 Ansible 自动化部署并管理多节点 Hadoop 集群?
linux·服务器·ansible
linux修理工6 天前
vagrant ubuntu 22.04 ansible 配置
ubuntu·ansible·vagrant
biubiubiu07067 天前
Ansible自动化
运维·自动化·ansible
秋4278 天前
ansible配置与模块介绍
ansible
秋4278 天前
ansible剧本
linux·服务器·ansible
码农101号9 天前
Ansible - Role介绍 和 使用playbook部署wordPress
android·ansible
2301_8000509911 天前
Ansible
运维·ansible
阎*水13 天前
Ansible 核心要点总结
ansible