ansible 常用模块(二)
用户与租户
group
添加或删除组
| 参数 | 含义 |
|---|---|
| name(required) | 要管理的组的名称 |
| gid | 组id号 |
| state | 该组是否应存在于远程主机上, 默认 present |
| system | 该组是否为系统组,默认 no |
# 创建test组,组id为1888
$ ansible -i hosts local -m group -a "name=test gid=188 state=present"
# 删除test组
$ ansible -i hosts local -m group -a "name=test state=absent"
user
管理用户帐户
| 参数 | 含义 |
|---|---|
| name(required) | 要创建、删除或修改的用户的名称。别名 user |
| uid | 设置用户id |
| group/groups | 用户所属组 |
| create_home | 是否创建家目录,默认:yes |
| password | (可选)将用户的密码设置为此加密值 |
| state | 帐户是否应该存在,如果状态与声明的不同则采取行动,默认present |
| shell | 设置用户登录shell类型,默认 /bin/bash |
| system | 账号是否为系统账号, 默认 no |
| expires | 用户过期时间 |
| remove | 删除用户时,是否删除家目录。默认 no |
| comment | 账号的注释 |
# 创建test用户,密码为JIf.54*2s,有效期为一个月
$ ansible -i hosts local -e pwd="JIf.54*2s" -m user -a "name=test state=present password={{ pwd | password_hash('sha512') }} expires=`date -d '1 month' +%s`"
# 更新密码,与创建用户一样
$ ansible -i hosts local -e pwd="JIf.54*2sfs" -m user -a "name=test state=present password={{ pwd | password_hash('sha512') }}"
# 创建test用户,用户id为1888,组是root,不创建家目录,有效期3个月
$ ansible -i hosts local -m user -a "name=test uid=1888 group=root create_home=no expires=`date -d '3 month' +%s`"
# 删除test用户并删除家目录
$ ansible -i hosts local -m user -a 'name=test state=absent remove=true'
ssh密钥文件(分发)
authorized_key
添加或删除特定用户帐户的 SSH 授权密钥
# 分发ssh密钥文件(免密登录)
$ ansible -i hosts local -m authorized_key -a "user=ops key={{ lookup('file', '/home/ops/.ssh/id_rsa.pub') }} state=present"
系统配置
cron
管理 cron.d 和 crontab 条目
| 参数 | 含义 |
|---|---|
| name(required) | crontab 条目的描述,或者如果设置了 env,则为环境变量的名称 |
| user | 应修改其 crontab 的特定用户 |
| job | 要执行的命令,或者如果设置了 env,则为环境变量的值。 该命令不应包含换行符 |
| minute/hour/day/month/weekday | 分别设置分钟、小时、日期、月份、周几的时间力度,默认是 "*" |
| state | 是否确保作业或环境变量 present 或 absent。默认是present |
# 每割两分制执行 ls /tmp 命令
$ ansible -i hosts local -m cron -a "name=test user=root job='ls /tmp > /dev/null' minute='*/2' state=present"
# 删除某条定时任务
$ ansible -i hosts local -m cron -a "name=test state=absent"
firewalld
使用firewalld管理任意端口/服务
| 参数 | 含义 |
|---|---|
| port | 要添加到防火墙或从防火墙中删除的端口或端口范围的名称 |
| service | 要添加到防火墙或从防火墙中删除的服务的名称 |
| source | 您想要在firewalld中添加/删除的源/网络 |
| rich_rule | 用于向/从firewalld添加/删除的丰富规则 |
| state | 启用或禁用设置 |
| zone | 要添加/删除的firewalld区域 |
| permanent | 此配置应该在正在运行的firewalld配置中还是在重新启动后仍然存在 |
注意事项:
- 一次只能在port、service、rich_rule、masquerade、icmp_block、icmp_block_inversion、interface 或 source 上操作。如果有多个规则组件的话,使用rich_rule进行处理
# 放通9092端口(永久生效,但需要reload服务才生效)
$ ansible -i hosts local -m firewalld -a 'port=9092/tcp state=enabled permanent=true'
# 放通9092端口(临时生效)
$ ansible -i hosts local -m firewalld -a 'port=9092/tcp state=enabled'
# 移除9092端口(永久生效,但需要reload服务才生效)
$ ansible -i hosts local -m firewalld -a 'port=9092/tcp state=disabled permanent=true'
# 移除9092端口(临时生效)
$ ansible -i hosts local -m firewalld -a 'port=9092/tcp state=disabled'
iptables
修改iptables规则
| 参数 | 含义 |
|---|---|
| action | 规则是否应附加在底部或插入在顶部 |
| ctstate | conntrack 模块中要匹配的连接状态列表,可能的值为 INVALID、NEW、ESTABLISHED、RELATED、UNTRACKED、SNAT、DNAT |
| chain | 指定要修改的iptables链, 这可以是用户定义的链或标准 iptables 链之一,例如 INPUT、FORWARD、OUTPUT、PREROUTING、POSTROUTING、SECMARK 或 CONNSECMARK |
| comment | 这指定将添加到规则中的注释 |
| destination | 目的地规范 |
| dst_range | 指定要在 iprange 模块中匹配的目标 IP 范围 |
| destination_port | 目标端口或端口范围规范 |
| source | 来源地址 |
| src_range | 指定 iprange 模块中要匹配的源 IP 范围 |
| source_port | 源端口或端口范围规范 |
| flush | 刷新指定表和链的所有规则,默认是no |
| icmp_type | 指定 ICMP 类型 |
| jump | 匹配该主机需要做什么动作。eg:丢弃(DROP),放通(ACCEPT) |
| protocol | 要检查的规则或数据包的协议,指定的协议可以是 tcp、udp、udplite、icmp、ipv6-icmp 或 icmpv6、esp、ah、sctp 或特殊关键字 all 之一,也可以是一个数值,代表这些协议之一或不同的协议 |
| state | 规则应该 absent 还是 present |
| table | 该选项指定命令应操作的数据包匹配表(filter、nat、mangle、raw或security),默认是filter |
# 放通ping协议
# 等价于 iptables -t filter -A INPUT -p icmp --icmp-type 8 -j ACCEPT
$ ansible -i hosts local -m iptables -a "table=filter chain=INPUT protocol=icmp icmp_type=8 jump=ACCEPT state=present"
# 放通连接状态是STABLISHED,RELATED
# 等价于 iptables -t filter -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
$ ansible -i hosts local -m iptables -a "table=filter chain=INPUT ctstate=ESTABLISHED,RELATED jump=ACCEPT"
# 修改默认策略
# 等价于 iptables -t filter -P INPUT DROP
$ ansible -i hosts local -m iptables -a "table=filter chain=INPUT policy=DROP"
# 放通etcd数据库,插入到INPUT第一行
# 等价于 iptables -t filter -I INPUT -p tcp --dport 2379:2381 -m comment --comment "etcd Component ports" -j ACCEPT
$ ansible -i hosts local -m iptables -a "table=filter action=insert chain=INPUT protocol=tcp destination_port=2379:2381 comment='etcd Component ports' jump=ACCEPT state=present"
# 查看iptables规则
$ ansible -i hosts local -a "iptables -nv -L INPUT --line-numbers"
sysctl
管理 sysctl.conf 中的条目
| 参数 | 含义 |
|---|---|
| name(required) | 指定 sysctl 变量的点分隔路径(也称为键),别名:key |
| value | sysctl 键的所需值,别名:val |
| reload | 是否生效参数,默认是yes |
| sysctl_file | sysctl文件绝对路径 |
| state | sysctl 文件中是否应存在该条目,默认present |
$ ansible -i hosts local -m sysctl -a "name=net.ipv4.ip_forward value=1 sysctl_file=/etc/sysctl.d/test.conf reload=yes state=present"
selinux
更改 SELinux 的政策和状态
| 参数 | 含义 |
|---|---|
| state | SELinux 模式, 可选项:disabled,enforcing 或 permissive |
# 将selinxu状态修改为disabled状态,但需要重启主机才生效,原来运行时由Enforcing改成Permissive
$ ansible -i hosts local -m selinux -a "state=disabled"
mount
控制活动和配置的挂载点
| 参数 | 含义 |
|---|---|
| path(required) | |
| state(required) | 挂载状态,可选:absent、mounted、present、unmounted 或 remounted |
| src | 要安装在路径上的设备(或 NFS 卷或其他内容) |
| fstype | 文件系统类型 |
| opts | 挂载选项 |
# 挂载并将配置写入/etc/fstab文件中
$ ansible -i hosts local -m mount -a "src=192.168.32.166:/data/nfs path=/app opts=defaults,_netdev state=mounted fstype=nfs state=mounted"
# 取消挂载并删除/etc/fstab文件中
$ ansible -i hosts local -m mount -a "src=192.168.32.166:/data/nfs path=/app opts=defaults,_netdev state=mounted fstype=nfs state=absent"
rsync
rsync 的包装器,可让您的 playbook 中的常见任务快速轻松地完成
注意事项:
- 目前,同步仅限于通过无密码 sudo 提升权限。 这是因为 rsync 本身正在连接到远程计算机,并且 rsync 没有为我们提供传递 sudo 凭据的方法
| 参数 | 含义 |
|---|---|
| src(required) | 将同步到目标的源主机上的路径 |
| dest(required) | 将从源同步的目标主机上的路径 |
| archive | 存档模式; 等于 -rlptgoD(无 -H、-A、-X), 默认yes |
| compress | 在传输过程中压缩文件数据,默认yes |
# 主机目录之间同步数据
$ ansible -i hosts local -m synchronize -a "src=/etc/ dest=/tmp/test"
# 同步数据到另一台主机
$ ansible -i hosts local -m synchronize -a "src=/etc/ dest=192.168.32.166:/tmp/test"
查看帮助文档
命令方式
# 查看有哪些模块
ansible-doc -l
# 查看具体模块使用方式
ansible-doc -s 模块名称
官网文档
ansible builtin 官方文档传送门 : https://docs.ansible.com/ansible/2.10/collections/ansible/builtin/index.html#modules
ansible posix 官方文档传送门: https://docs.ansible.com/ansible/2.10/collections/ansible/posix/index.html#modules

浙公网安备 33010602011771号