Ansible模块1--系统管理模块¶
一. hostname模块¶
1. 简介¶
该模块用于设置或修改远程主机的 hostname
注意:
该模块的作用就是设置远程目标主机的主机名属性,但是他会有一个缺陷:如果批量设置主机名,所有的远程主机名称就一致了。所以后面我们需要根据变量的方式自动生成某种格式特性的主机名。
2. 命令格式¶
hostname模块在ansible使用时,标准的写法是:
ansible <目标主机> -m hostname -a 'name=主机名'
3. 注意¶
- 主机名的设置一定要规范
- 这种方式更改的主机名,是立即生效且永久生效的
4. 实践¶
设置主机名为2,3
[root@1 ~]# ansible -m hostname -a 'name=2' web_servers1
2 | CHANGED => {
"ansible_facts": {
"ansible_domain": "",
"ansible_fqdn": "2",
"ansible_hostname": "2",
"ansible_nodename": "2",
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"name": "2"
}
[root@1 ~]# ansible -m hostname -a 'name=3' web_servers2
3 | CHANGED => {
"ansible_facts": {
"ansible_domain": "",
"ansible_fqdn": "3",
"ansible_hostname": "3",
"ansible_nodename": "3",
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"name": "3"
}
查看验证
[root@1 ~]# ansible -m shell -a 'hostname' web_servers*
2 | CHANGED | rc=0 >>
2
3 | CHANGED | rc=0 >>
3
二. user模块¶
1. 简介¶
user模块,主要是用于对远程主机的用户进行干预操作
2. 常见属性¶
name: 设置用户名
comment: 用户的描述信息
group: 设定用户的主组信息
groups: 设置用户的附加组
home: 设置用户家目录
password: 设置登录密码
shell: 设定用户的专用shell类型
state: 设定用户的存活状态
常见选项有present(存在)
absent(不存在)
system: 设定用户是否为系统用户
uid: 设定用户的UID
remove: 设定删除用户以及其家目录等数据。默认
remove=no,推荐使用yes
注意:
上面属性是常用的属性,还有其他属性未来可能会用到
state主要有两个属性值absent(没有),present(存在)
groups可以有多个值,多个值之间可以用','隔开
3. 命令格式¶
user模块在ansible使用时,标准的写法是
ansible <目标主机> -m user -a '属性1=值1 属性2=值2 属性3=值3 ... 属性n=值n'
注意:
用户名的设置一定要做好规划。
在工作中,uid的值一定要提前做好规划并且必须唯一
4. 实践¶
a. 创建一个定制的系统用户webapp,属组是root和bin其中主组为root,uid为10086,禁止登录,家目录在/tmp/webapp
[root@1 ~]# ansible -m user -a 'name=webapp system=yes comment="ansible测试用户" group=root groups=root,bin uid=10086 shell=/sbin/nologin home=/tmp/webapp state=present' web_servers1
2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"append": false,
"changed": true,
"comment": "ansible测试用户",
"group": 0,
"groups": "root,bin",
"home": "/tmp/webapp",
"move_home": false,
"name": "webapp",
"shell": "/sbin/nologin",
"state": "present",
"uid": 10086
}
检查效果
[root@1 ~]# ansible -a "id webapp" web_servers1
2 | CHANGED | rc=0 >>
用户id=10086(webapp) 组id=0(root) 组=0(root),1(bin)
b. b. 创建一个携带ssh认证的用户
[root@1 ~]# ansible -m user -a "name=webapp2 home=/tmp/webapp2 generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa" web_servers2
3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1002,
"home": "/tmp/webapp2",
"name": "webapp2",
"shell": "/bin/bash",
"ssh_fingerprint": "2048 SHA256:YVwaEr2Py7Kacz0vrlFQNaH0YyQriLR+n6rLL3ijYik ansible-generated on 3 (RSA)",
"ssh_key_file": "/tmp/webapp2/.ssh/id_rsa",
"ssh_public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCMm4dCzsFnLrVcTiYPMBx8SlA4/VUucc/Pp+ZbHrzf0UqU5euHPtk95cHycQx/ajJn5jbAyNQonCiPaVLfXJ1HEdppY2g/kFCRhjZRavf/naeCLR5X0qhcWBqiBvPHSr60VFTIwknUNxC4+/HtkKV75Y9SdS+M4revo1+OjKMFMpQnCF2pcdD5LVki39naaxVdjesRejngfG/Mzf463YKAfnPe1rn3MGTZwTfHbZ3cKejGoZfiM8aFepVnknAslbKMWX0yMx9LElpT5wk4XW+fk2tNSLU+B2mqe7JGyPYko4KcpPJwXf/eONYqP+EaoRKD+CdHXIcNmcpfPwfM71Sf ansible-generated on 3",
"state": "present",
"system": false,
"uid": 1002
}
验证效果
[root@1 ~]# ansible -a "getent passwd webapp2" web_servers2
3 | CHANGED | rc=0 >>
webapp2:x:1002:1002::/tmp/webapp2:/bin/bash
[root@1 ~]# ansible -a "ls /tmp/webapp2/.ssh" web_servers2
3 | CHANGED | rc=0 >>
id_rsa
id_rsa.pub
# 注意: getent命令可以用来查看系统数据库相关信息,经常使用getent查看用户账户
c. 删除用户
[root@1 ~]# ansible -m user -a "name=webapp state=absent remove=yes" web_servers1
2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"force": false,
"name": "webapp",
"remove": true,
"state": "absent"
}
[root@1 ~]# ansible -m user -a "name=webapp2 state=absent remove=yes" web_servers2
3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"force": false,
"name": "webapp2",
"remove": true,
"state": "absent"
}
查看验证
[root@1 ~]# ansible -m shell -a "id webapp webapp2" web_servers*
2 | FAILED | rc=1 >>
id: “webapp”:无此用户
id: “webapp2”:无此用户non-zero return code
3 | FAILED | rc=1 >>
id: “webapp”:无此用户
id: “webapp2”:无此用户non-zero return code
三. group模块¶
1. 简介¶
该模块应用于用户组的管理
2. 命令格式¶
group模块在ansible使用时,标准写法是
ansible <目标主机> -m group -a '属性1=值1 属性2=值2 ... 属性n=值n'
注意: 组名的设置一定要规范,gid一定是唯一的
3. 实践¶
1) 创建一个用户组¶
# 在2创建一个系统组,组名webap,gid为10088
[root@1 ~]# ansible -m group -a "name=webap system=yes gid=10088" web_servers1
2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"gid": 10088,
"name": "webap",
"state": "present",
"system": true
}
# 查看验证
[root@1 ~]# ansible -a "getent group webap" web_servers1
2 | CHANGED | rc=0 >>
webap:x:10088:
2) 删除一个用户组¶
# 删除用户2的该组
[root@1 ~]# ansible -m group -a "name=webap state=absent" web_servers1
2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"name": "webap",
"state": "absent"
}
# 查看验证
[root@1 ~]# ansible -m shell -a "getent group webap" web_servers1
2 | FAILED | rc=2 >>
non-zero return code
# 此时报错,因为该组已经不存在了
四. cron模块¶
1. 简介¶
cron模块主要做一些类似于周期性任务相关的任务执行动作
2. 常见属性¶
day: 设定天效果
hour: 设定时效果
minute: 设定分钟效果
weekday: 周表示显示
month: 设定月效果
job: 如果是env则内容是变量。如果是
state=present内容是定时任务name: 如果有env则是变量名;如果有
state=present则是定时任务名disabled: 结合
state=present实现禁用已设置的定时任务disabled=yes|true禁用定时任务
disabled=no|false启用定时任务
state: 设置定时任务的状态,用于设置或删除定时任务
state=present: 创建定时任务
state=absent: 删除定时任务
注意:
上面是常用属性
name和job最好要作为强制选项
3. 命令格式¶
cron模块在ansible使用的时候,标准的写法是
ansible <目标主机> -m cron -a '属性1=值1 属性2=值2 ... 属性n=值n'
注意: job的命令一定要规范,最好使用绝对路径
4. 实践¶
1) 定制计划任务¶
在指定节点上定义一个计划任务,每天凌晨3:30执行一次时间同步
# 先安装cron
[root@1 ~]# ansible -m dnf -a 'name=cronie state=present disable_gpg_check=no update_cache=no' web_servers2
3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"msg": "Nothing to do",
"rc": 0,
"results": []
}
# 现在设置定时任务
[root@1 ~]# ansible -m cron -a 'name="sync time" state=present minute=30 hour=3 day=* month=* weekday=* job="/usr/sbin/ntpdate time1.aliyun.com"' web_servers2
3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"envs": [],
"jobs": [
"sync time"
]
}
查看是否设置完毕
[root@1 ~]# ansible -a 'crontab -l' web_servers2
3 | CHANGED | rc=0 >>
#Ansible: sync time
30 3 * * * /usr/sbin/ntpdate time1.aliyun.com
2) 禁用定时任务¶
[root@1 ~]# ansible -m cron -a 'name="sync time" disabled=yes job="/usr/sbin/ntpdate time1.aliyun.com"' web_servers2
3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"envs": [],
"jobs": [
"sync time"
]
}
# 禁用的时候,job参数必须要写
# 查看验证
[root@1 ~]# ansible -a 'crontab -l' web_servers2
3 | CHANGED | rc=0 >>
#Ansible: sync time
#* * * * * /usr/sbin/ntpdate time1.aliyun.com
3) 重新启用定时任务¶
# 重新启用定时任务
# 注意: 必须重新带上 minute=30 hour=3 恢复原时间,否则会用默认值 * 变成每分钟执行
[root@1 ~]# ansible -m cron -a 'name="sync time" disabled=no minute=30 hour=3 job="/usr/sbin/ntpdate time1.aliyun.com"' web_servers2
3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"envs": [],
"jobs": [
"sync time"
]
}
# 查看验证
[root@1 ~]# ansible -a 'crontab -l' web_servers2
3 | CHANGED | rc=0 >>
#Ansible: sync time
30 3 * * * /usr/sbin/ntpdate time1.aliyun.com
4) 删除定时任务¶
[root@1 ~]# ansible -m cron -a 'name="sync time" state=absent' web_servers2
3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"envs": [],
"jobs": []
}
查看验证
5) 注意¶
- 禁用定时任务时,name必须写清楚,job也得要有,少一个就会报错。
- 删除定时任务时,name必须写清楚,但是job也可不写
- 禁用的属性是
disabled=true|yes,默认为false|no - 不写 name 时每次都会新建一个无
#Ansible标记的条目,不会自动去重 - 禁用/重新启用定时任务时,cron 模块会用本次命令给的参数重建条目,未指定的时间字段(minute/hour/day...)默认变成
*。所以重新启用时要把 minute、hour 等原时间字段重新带上,否则任务会变成每分钟执行
五. SELinux模块¶
1. 简介¶
SELinux模块的作用是对远程主机的SELinux服务进行管理
2. 参数解析¶
常用选项
configfile=/path/file: selinux配置文件的默认路径
/etc/selinux/configstate: 设置selinux的运行模式
enforcing: 强制模式。SELinux 开启,违规直接拒绝并记录日志
permissive: 宽容模式。SELinux 开启,违规只记日志不拦截(相当于"试运行")
disabled: 禁用。SELinux 完全关闭(改配置文件 + 重启才彻底生效)
policy: SELinux的策略类型
targeted: 只对特定目标进程(网络守护进程等)启用 SELinux,其他进程不受限。
minimum: targeted 的精简版,只保护极少数核心进程。
mls: 多级安全(Multi-Level Security),最严格,按密级隔离。
3. 实践¶
1) 开启远程主机上的selinux¶
# 启用SELinux
[root@1 ~]# ansible -m selinux -a 'state=enforcing policy=targeted' web_servers2
3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"configfile": "/etc/selinux/config",
"msg": "",
"policy": "targeted",
"reboot_required": false,
"state": "enforcing"
}
# 查看验证
[root@1 ~]# ansible -m shell -a 'getenforce' web_servers2
3 | CHANGED | rc=0 >>
Enforcing
2) 禁用SELinux¶
# 禁用SELinux
[root@1 ~]# ansible -m selinux -a 'state=disabled policy=targeted' web_servers2
[WARNING]: SELinux state temporarily changed from 'enforcing' to 'permissive'. State change will take effect next reboot.
3 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"configfile": "/etc/selinux/config",
"msg": "Config SELinux state changed from 'enforcing' to 'disabled'",
"policy": "targeted",
"reboot_required": true,
"state": "disabled"
}
# 查看验证
[root@1 ~]# ansible -m shell -a 'getenforce' web_servers2
3 | CHANGED | rc=0 >>
Permissive
六. setup模块¶
1. 简介¶
setup模块是用来收集目标主机的属性信息
2. 常用参数¶
fact_path: 设定facts的文件路径
filter: 指定过滤格式,支持通配符(fnmatch),如 ansible_、memtotal*、ansible_eth[0-2]
注意:
常用的属性只有上面的两个
setup模块是通过调用ansible的facts组件来获取主机的所有属性信息
3. 命令格式¶
setup模块在ansible使用的时候,标准的写法为
ansible <目标主机> -m setup -a '属性1=值1 属性2=值2 ... 属性n=值n'
注意:
不加-a表示获取目标主机的所有属性信息
过滤信息的规则一定要合理设计
这个功能很重要,尤其是自动化管理的时候
4. 实践¶
1) 获取目标主机的所有属性¶
[root@1 ~]# ansible -m setup web_servers1
2 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
...
所有的信息都是以"属性名: 属性值"的格式来展示的
2) 获取指定主机属性: 内存总量¶
[root@1 ~]# ansible -m setup -a 'filter=ansible_memtotal_mb' web_servers1
2 | SUCCESS => {
"ansible_facts": {
"ansible_memtotal_mb": 1738,
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false
}
3) 获取某些主机属性: 所有的网卡信息¶
[root@1 ~]# ansible -m setup -a 'filter=*ens160*' web_servers1
2 | SUCCESS => {
"ansible_facts": {
"ansible_ens160": {
"active": true,
"device": "ens160",
...
七. sysctl模块¶
1. 简介¶
sysctl模块用来修改远程主机上的内核参数
2. 参数解析¶
name=str: 参数名称
value=str: 参数值
reload=yes|no: 是否重载,调用
/sbin/sysctl -p生效。默认为yesstate=present|absent: 是否保存到文件,默认值为present
sysctl_file=/path/file: 指定文件保存路径。默认为
/etc/sysctl.confsysctl_set=yes|no: 是否使用
sysctl -w校验。默认不使用
3. 实践¶
1) 修改内核参数,并写入文件¶
[root@1 ~]# ansible -m sysctl -a 'name=net.ipv4.ip_forward value=1' web_servers1
2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true
}
查看验证
[root@1 ~]# ansible -m shell -a 'sysctl -a|grep "ip_forward = "' web_servers1
2 | CHANGED | rc=0 >>
net.ipv4.ip_forward = 1
2) 在文件中删除刚才添加的属性¶
[root@1 ~]# ansible -m sysctl -a 'name=net.ipv4.ip_forward reload=yes state=absent' web_servers1
2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true
}
此时查看验证
[root@1 ~]# ansible -m shell -a "grep ip_forward /etc/sysctl.conf; echo ---; sysctl net.ipv4.ip_forward" web_servers1
2 | CHANGED | rc=0 >>
---
net.ipv4.ip_forward = 1
文件中的属性已经被删除,但是因为内核的参数尚未被修改所以删除属性不会修改属性当前的值
八. mount模块¶
1. 简介¶
mount模块用于管理远程主机的挂载
2. 参数解析¶
src: 要挂载的设备路径,可以是网络地址。格式是
src=/path/devicepath: 指定挂载点。格式为
path=/path/pointstate: 挂载状态
absent: 取消挂载,并删除永久挂载
mounted: 永久挂载,立即生效。挂载点不存在会自动创建
present: 永久挂载,写入配置文件,但不会立即生效
unmounted: 临时取消挂载,不会改变配置文件
remounted: 重新挂载。不会改变配置文件
fstab: 指定挂载配置文件路径。默认
/etc/fstabfstype: 设备文件系统。格式是fstype=str。文件系统比如
xfs|ext4|swap|iso9660...opts: 挂载选项,常用选项如下
defaults: 默认组合(rw,suid,dev,exec,auto,nouser,async)
ro / rw: 只读 / 读写
exec / noexec: 允许 / 禁止执行二进制
suid / nosuid: 允许 / 禁止 suid 位
dev / nodev: 允许 / 禁止设备文件
auto / noauto: 是否开机自动挂载
sync / async: 同步 / 异步写入
noatime / relatime: 不更新访问时间 / 相对时间
loop: 挂载镜像文件(如 .iso)需要
umask=xxx: 文件权限掩码(FAT/光盘适用)
iocharset=utf8: 字符集(FAT 中文文件名适用)
3. 实践¶
1) 挂载光盘,永久挂载并立即生效 --- 一定要保证能够获取到光盘镜像文件,否则会报错¶
# 先创建挂载点
[root@1 ~]# ansible -m file -a 'path=/media/cdrom state=directory' web_servers1
2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/media/cdrom",
"secontext": "unconfined_u:object_r:mnt_t:s0",
"size": 6,
"state": "directory",
"uid": 0
}
# 挂载光盘,永久生效
[root@1 ~]# ansible -m mount -a 'src=/dev/sr0 path=/media/cdrom state=mounted fstype=iso9660 opts=ro' web_servers1
2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"backup_file": "",
"boot": "yes",
"changed": true,
"dump": "0",
"fstab": "/etc/fstab",
"fstype": "iso9660",
"name": "/media/cdrom",
"opts": "ro",
"passno": "0",
"src": "/dev/sr0"
}
查看验证
[root@1 ~]# ansible -m shell -a "mount|grep 'sr0';grep sr0 /etc/fstab" web_servers1
2 | CHANGED | rc=0 >>
/dev/sr0 on /media/cdrom type iso9660 (ro,relatime,nojoliet,check=s,map=n,blocksize=2048)
/dev/sr0 /media/cdrom iso9660 ro 0 0
2) 取消挂载,并永久生效¶
path必须写,src可写可不写
[root@1 ~]# ansible -m mount -a 'path=/media/cdrom state=absent' web_servers1
2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"backup_file": "",
"boot": "yes",
"changed": true,
"dump": "0",
"fstab": "/etc/fstab",
"name": "/media/cdrom",
"opts": "defaults",
"passno": "0"
}
查看验证