6、user模块:用于管理用户账号和用户属性
| 常用参数 | 说明 |
|---|
| name="" | 指定用户名 |
| password="" | 指定密码,必须是密文,如果密码是明文需要先转换成密文:如密码是123时,echo 123|openssl passwd -1 -stdin |
| state= absent|present | 删除|创建 |
| system= yes|no | 是否为系统用户 |
| shell="" | 指定登陆shell |
| generate_ssh_key= yes|no | 是否创建秘钥对 |
| uid= | 指定用户的uid |
| append= yes|no | 用户是否追加到其他组 |
| group= | 用户属组 |
| groups= | 将现有用户加入到某个组,空值就会把该用户从所有所属组中删除 |
| create_home= yes|no | 是否建立家目录 |
| remove= yes|no | 删除家目录 |
# 创建名为sky的用户
echo 123|openssl passwd -1 -stdin
密码转为密文就是:$1$yvjRMUYz$TkIBxP400WTZsajsDv6ZJ1
ansible -m user group1 -a 'name=sky password="$1$yvjRMUYz$TkIBxP400WTZsajsDv6ZJ1" state=present system=yes shell=/sbin/nologin generate_ssh_key=yes'
# 查看用户
cat /etc/passwd
# 删除用户
ansible -m user group1 -a "name=sky state=absent remove=yes"
7、group模块:用于管理用户组和用户组属性
| 参数 | 说明 |
|---|
| name= | 组名 |
| state= persent|absent | 创建|删除 |
| system= yes|no | 是否为系统组 |
| gid | gid |
# 创建组
ansible -m group group1 -a "name=admin gid=4444 state=present"
# 查看组
cat /etc/group
# 删除组
ansible -m group group1 -a "name=admin state=absent"
8、cron模块:用于管理周期性时间任务
| 参数 | 说明 |
|---|
| name | 计划任务的名称 |
| user | 执行计划任务的用户 |
| job | 计划任务命令 |
| minute | 执行计划任务的分 默认为* |
| hour | 执行计划任务的时 默认为* |
| day | 执行计划任务的日 默认为* |
| month | 执行计划任务的月 默认为* |
| week | 执行计划任务的周 默认为* |
| state absent | 删除计划任务 |
# 设置cron任务
ansible -m cron -a 'name="cron test" user=root job="echo haha > /root/test" minute=53 hour=15' 192.168.221.40
# 呈现出的结果
[root@node3 ~]# cat /root/test
[root@node3 ~]# cat /root/test
[root@node3 ~]# cat /root/test
haha
# 删除cron任务
ansible -m cron 192.168.221.40 -a 'name="cron test" state=absent'
9、yum_repository模块:用于配置yum仓库(centos系统适用)
| 参数 | 说明 |
|---|
| name | 仓库名 name.repo 源的名称 [name] |
| description | 描述 |
| baseurl | 包下载路径 |
| gpgcheck= 1 or 0 | 包gpg验证 |
| enabled = yes|no | 是否开启本源 |
| state= absent | 删除源 |
# 添加yum源
ansible -m yum_repository group1 -a "name=dvd description=BaseOS baseurl=file:///media/cdrom gpgcheck=0 enabled=yes"
# 查看
[root@node3 ~]# ls /etc/yum.repos.d/
centos7.repo dvd.repo
# 删除yum源
ansible -m yum_repository group1 -a "name=dvd state=absent"
# 查看
[root@node3 ~]# ls /etc/yum.repos.d/
centos7.repo
10、apt_repository模块:用于配置apt仓库(Ubuntu系统适用)
| 参数 | 说明 |
|---|
| repo | 指定要添加或删除的仓库字符串 |
| filename | 指定生成或删除的 APT 配置文件的名称(不包括 .list 扩展名) |
| state | 指定仓库的状态,present 表示添加仓库,absent 表示删除仓库 |
| url | 指定GPG密钥的URL,使用 apt_key 模块来添加 GPG 密钥。 |
# 添加APT 仓库
ansible localhost -m ansible.builtin.apt_repository -a "repo='deb http://example.com/repo stable main' filename='myrepo' state=present"
参数说明
repo: 指定要添加的仓库字符串。
filename: 指定生成的 APT 配置文件的名称(不包括 .list 扩展名)。
state: 指定仓库的状态,present 表示添加仓库。
# 删除 APT 仓库
ansible localhost -m ansible.builtin.apt_repository -a "repo='deb http://example.com/repo stable main' filename='myrepo' state=absent"
11、yum模块:用于使用yum命令来实现软件包的安装与卸载
在Ubuntu系统中将yum替换为apt命令即可
| 参数 | 说明 |
|---|
| name | 需要安装软件包的名称 |
| list= installed, updates, available and repos | 列出包信息:列出已安装 需要更新 可获得的 和 yum源 |
| state= absent removed installed present latest | 删除、删除、安装确认、安装确认、安装最新版本 |
# 使用yum安装vsftpd
ansible -m yum 192.168.221.40 -a "name=vsftpd"
# 查看是否安装
[root@node3 ~]# which vsftpd
/usr/sbin/vsftpd
# 删除软件包
ansible -m yum 192.168.221.40 -a "state=absent name=vsftpd"
# 查看是否卸载
which vsftpd
/usr/bin/which: no vsftpd in (/usr/local/mysql/bin:/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
12、service模块:用于控制服务的启动,关闭,开机自启动等
| 参数 | 说明 |
|---|
| name | 服务名称 |
| state reloaded, restarted, started, stopped | 服务管理 |
| enabled yes|no | 开启是否启动 |
# 启动vsftpd服务,并设为开机自动启动
ansible -m service 192.168.221.40 -a "name=vsftpd state=started enabled=on"
# 关闭vsftpd服务,并设为开机不自动启动
ansible -m service 192.168.221.40 -a "name=vsftpd state=stopped enabled=false"
13、script模块:用于在远程机器上执行本地脚本
-
script模块的作用是将本地script传送到远程主机之后执行
-
给定的脚本将通过远程节点上的shell环境进行处理
-
script模块在远程系统上不需要python的支持
在manage01上创建脚本,通过ansible将脚本分发到被管理端
[root@manage01 ~]# vim ansible_test.sh
------------------------------------------------------------
#!/bin/bash
#ansible script module test script
mkdir /opt/log
find / -name "*.log" -exec cp -rpf {} /opt/log \;
------------------------------------------------------------
#脚本不用给执行权限
[root@manage01 ~]# ansible -m script group1 -a "/root/ansible_test.sh"
14、command模块:用于执行linux命令(无法使用管道符)
| 名称 | 必选 | 备注 |
|---|
| chdir | no | 运行command命令前先cd到这个目录 |
| creates | no | 如果这个参数对应的文件存在,就不运行command |
| free_form | yes | 需要执行的脚本(没有真正的参数为free_form) |
| executable | no | 改变用来执行命令的shell,应该是可执行文件的绝对路径。 |
| removes | no | 如果这个参数对应的文件不存在,就不运行command,与creates参数的作用相反 |
| stdin(2.4后新增) | no | 将命令的stdin设置为指定的值 |
# 查看node3上root下的文件
[root@manage01 ~]# ansible -m command 192.168.221.40 -a "ls /root"
# 在test文件里输入hello123,command模块执行不成功
[root@manage01 ~]# ansible -m command 192.168.221.40 -a "echo 'hello 123' > /root/test"
192.168.221.40 | CHANGED | rc=0 >>
hello 123 > /root/test
# 查看结果——————未改变
[root@manage01 ~]# ansible -m command 192.168.221.40 -a "cat /root/test"
192.168.221.40 | CHANGED | rc=0 >>
haha
15、shell模块:用于执行Linux命令
| 名称 | 必选 | 备注 |
|---|
| chdir | no | 运行command命令前先cd到这个目录 |
| creates | no | 如果这个参数对应的文件存在,就不运行command |
| executable | no | 改变用来执行命令的shell,应该是可执行文件的绝对路径。 |
| free_form | yes | 需要执行的脚本(没有真正的参数为free_form) |
| removes | no | 如果这个参数对应的文件不存在,就不运行command,与creates参数的作用相反 |
| stdin(2.4后新增) | no | 将命令的stdin设置为指定的值 |
# 查看node3上root下的文件
[root@manage01 ~]# ansible -m shell 192.168.221.40 -a "ls /root"
192.168.221.40 | CHANGED | rc=0 >>
anaconda-ks.cfg
initial-setup-ks.cfg
mysql_install.sh
mysql_install.sh.1
test
test40
# 输入hello123到test文件中
[root@manage01 ~]# ansible -m shell 192.168.221.40 -a "echo 'hello 123' > /root/test"
192.168.221.40 | CHANGED | rc=0 >>
# 查看结果——————已改变
[root@manage01 ~]# ansible -m shell 192.168.221.40 -a "cat /root/test"
192.168.221.40 | CHANGED | rc=0 >>
hello 123
# 追加hello456到test文件中
[root@manage01 ~]# ansible -m shell 192.168.221.40 -a "echo 'hello 456' >> /root/test"
192.168.221.40 | CHANGED | rc=0 >>
# 查看结果
[root@node3 ~]# cat /root/test
hello 123
hello 456
16、raw模块:最原始的方式运行命令(不依赖python,仅通过ssh实现)
| 名称 | 必选 | 备注 |
|---|
| executable | no | 改变用来执行命令的shell,应该是可执行文件的绝对路径。 |
| free_form | yes | 需要执行的脚本(没有真正的参数为free_form) |
# 清除yum缓存
[root@manage01 ~] ansible 192.168.221.40 -m raw -a "yum clean all"
17、setup模块:用于收集远程主机的基本信息(如操作系统类型,主机名,ip,cpu信息,内存信息等)
# 打印192.168.221.40机器的所有信息(一般不这么写,太多)
[root@manage01 ~]# ansible -m setup 192.168.221.40
### 使用filter过滤输出
#打印192.168.8.21机器的CPU信息
[root@manage01 ~]# ansible -m setup 192.168.221.40 -a "filter='ansible_processor'"
| 参数 | 说明 |
|---|
| ansible_processor | 打印CPU信息 |
| ansible_kernel | 打印内核信息 |
| ansible_hostname | 打印主机名 |
| ansible_ens* | 打印网卡信息 |
| ansible_all_ipv4_addresses | 显示ipv4的信息 |
| ansible_devices | 显示磁盘设备信息 |
| ansible_distribution_major_version | 显示是系统主版本 |
| ansible_distribution_version | 仅显示系统版本 |
| ansible_machine | 显示系统类型,例:32位,还是64位 |
| ansible_lvm | 显示lvm相关信息 |
| ansible_memtotal_mb | 显示系统总内存 |
| ansible_memfree_mb | 显示可用系统内存 |
| ansible_memory_mb | 详细显示内存情况 |
| ansible_swaptotal_mb | 显示总的swap内存 |
| ansible_swapfree_mb | 显示swap内存的可用内存 |
| ansible_mounts | 显示系统磁盘挂载情况 |
| ansible_processor | 显示cpu个数(具体显示每个cpu的型号) |
| ansible_processor_vcpus | 显示cpu个数(只显示总的个数) |
18、stat模块:类似linux的stat命令,用于获取文件的状态信息
# 获取root下的test文件的状态信息
[root@manage01 ~]# ansible -m stat 192.168.221.40 -a "path=/root/test"
19、blockinfile模块:用来在文件中插入文本块,插入的文本块会有一个标记,方便修改和删除
| 参数名 | 描述信息 |
|---|
| path | 操作的文件对象 |
| block | 也可用content,指定内容。 |
| marker | 使用marker参数自定义”标记”,方便我们通过对应的标记找到对应的内容 |
| state | 默认是present,state=absent,则表示从文件中删除对应标记的内容。 |
| backup | 在修改文件之前是否对文件进行备份,默认是no. |
| create | 当要操作的文件并不存在时,是否创建对应的文件。 |
# 使用marker插入文本“start httpd、stop httpd \restart httpd”,插入文本内容使用 httpd server进行标记
[root@manage01 ~]# ansible 192.168.221.40 -m blockinfile -a 'path=/root/test block="start httpd \n stop ttpd \n restart httpd " marker="#{mark} httpd server"'
[root@manage01 ~]# ansible 192.168.221.40 -m raw -a 'cat /root/test'
192.168.221.40 | CHANGED | rc=0 >>
hello 123
hello 456
#BEGIN httpd server
start httpd
stop ttpd
restart httpd
#END httpd server
Shared connection to 192.168.221.40 closed.
# 删除http server标记的文本内容
[root@manage01 ~]# ansible 192.168.221.40 -m blockinfile -a 'path=/root/test state=absent marker="#{mark} httpd server" '
[root@manage01 ~]# ansible 192.168.221.40 -m raw -a 'cat /root/test'
192.168.221.40 | CHANGED | rc=0 >>
hello 123
hello 456
Shared connection to 192.168.221.40 closed.
20、lineinfile模块:对文件行进行操作,默认添加文件是在行尾
| 参数名 | 描述信息 |
|---|
| path | 操作的文件对象 |
| line | 指定行内容。 |
| state | 默认值为present,state=absent表示删除。 |
| backup | 是否在修改文件之前对文件进行备份。 |
| insertbefore | 插在匹配值之前 |
| insertafter | 插在匹配值之后,在该该选项不写的情况下默认插在文本最后 |
| create | 当要操作的文件并不存在时,是否创建对应的文件。 |
# 添加"这是测试行"到/tmp/test文件中,如果不指定位置,默认是在末尾添加
[root@manage01 ~]# ansible 192.168.221.40 -m lineinfile -a 'path=/root/test line="这是测试行" '
[root@manage01 ~]# ansible 192.168.221.40 -m raw -a 'cat /root/test'
192.168.221.40 | CHANGED | rc=0 >>
hello 123
hello 456
这是测试行
Shared connection to 192.168.221.40 closed.
# 使用insertbefore指定行首插入到"hello 123"行之前,insertafter指定行末插入到"hello 456"之后
[root@manage01 ~]# ansible 192.168.221.40 -m lineinfile -a 'path=/root/test line="这是测试行-行首" insertbefore="hello 123" '
[root@manage01 ~]# ansible 192.168.221.40 -m lineinfile -a 'path=/root/test line="这是测试行-行末" insertafter="hello 456" '
[root@manage01 ~]# ansible 192.168.221.40 -m raw -a 'cat /root/test'
192.168.221.40 | CHANGED | rc=0 >>
这是测试行-行首
hello 123
hello 456
这是测试行-行末
这是测试行
Shared connection to 192.168.221.40 closed.
| 区别 | blockinfile模块 | lineinfile模块 |
|---|
| 功能目标 | 用于确保文件中存在特定的多行内容块,主要用于管理多行文本块 | 用于确保文件中存在特定的单行内容,主要用于管理单行文本 |
| 匹配方式 | 通过marker参数(默认是# BEGIN ANSIBLE MANAGED BLOCK和# END ANSIBLE MANAGED BLOCK)来标识和管理整个文本块 | 通过regexp参数匹配单行,默认替换匹配到的行 |
| 典型用途 | 添加 / 修改配置文件中的整个配置段、添加多行注释或代码块 | 修改配置文件中的特定参数(如max_memory = 1024) |
| 示例对比 | - blockinfile: path: /etc/nginx/nginx.conf block: | server { listen 80; server_name example.com; } insertafter: 'http {'- lineinfile: path: /etc/sysctl.conf regexp: '^net.ipv4.ip_forward' line: 'net.ipv4.ip_forward = 1' |