网站架构知识之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'

结果

相关推荐
hhzz6 天前
Ansible自动化运维实战--通过role远程部署nginx并配置(8/8)
运维·自动化·ansible
_Eden_6 天前
Ansible入门学习之Playbook快速入门
学习·ansible
_Eden_6 天前
Ansible介绍与入门学习
运维·学习·ansible
hhzz8 天前
ansible自动化运维实战--fetch、cron和group模块(5)
运维·自动化·ansible
hhzz8 天前
ansible自动化运维实战--yaml的使用和配置(7)
运维·自动化·ansible
hhzz8 天前
ansible自动化运维实战--软件包管理模块、服务模块、文件模块和收集模块setup(4)
运维·自动化·ansible
hhzz10 天前
ansible自动化运维实战--script、unarchive和shell模块(6)
运维·自动化·ansible
蘑菇丁10 天前
ansible 批量按用户名创建kerberos主体,并分发到远程主机
大数据·服务器·ansible
阿无@_@11 天前
1、ceph的安装——方式一ceph-ansible
ceph·ansible
牙牙70511 天前
ansible一键安装nginx二进制版本
服务器·nginx·ansible