Ansible模块介绍

Ansible中常用模块

目录

一、Ansible命令运行方式及常用参数

1、命令的格式:

2、常用的参数:

二、Ansible中常用模块

a、ping模块

b、command模块

常用参数
语法:

c、raw模块

d、shell模块

e、script模块

f、template模块

g、yum模块

h、copy模块

1、目的文件的权限
2、如果目的地文件已存在,备份目的地的原文件

i、group模块

1、在受控机上添加一个系统组,其gid为306,组名为zzz
2、删除受控机上的zzz组

j、user模块

1、在受控机上添加一个系统用户,用户名为zzz,uid为366,设置其shell为/sbin/nologin,无家目录
2、修改zzz用户的uid为306
3、删除用户时不删除家目录
4、删除用户时删除用户的家目录

k、service模块

l、file模块

1、创建一个普通文件
2、创建一个目录
3、查看
4、删除文件
5、删除目录
6、查看
7、创建一个软连接文件
8、创建一个硬连接文件
9、创建一个文件,并设置权限,属主,属组
10、修改一个文件得权限,属主,属组
11、修改一个文件得set位特殊权限
12、若想同时改变目录及其中的子目录或字文件的属性,可以用recurse=yes参数实现

m、lineinfile模块

1、当要操作的文件不存在时,是否创建对应的文件
2、将匹配到的含hello字符串的最后一行行替换为line中的字符串
3、当 backrefs=yes,同时根据匹配规则找不到相应的行时,不对文件进行任何改变
4、^正则表达式
5、在文件的开头插入line中的字符
6、在文件的结尾插入line中的字符
7、在含有hello字符的行前插入line中的字符
8、在含有hello字符的行后插入line中的字符

n、blockinfile 模块

1、添加
2、在末尾添加
3、删除:

o、cron模块

1、模块方法
2、示例:

p、unarchive 解压模块

q、archive 压缩模块

一、Ansible命令运行方式及常用参数
1、命令的格式:
ansible 清单 -m 模块 -a 模块参数

2、常用的参数:
名称 含义

  • -version 显示版本
    -m module 指定模块,默认为command模块
    -v 详细过程 -vv -vvv更详细过程
  • -list 显示主机列表,也可以用–list-hosts
    -k 提示输入ssh连接密码,默认key认证
    -C 预执行检测
    -T 执行命令的超时时间,默认10s
    -b 执行sudo切换身份操作
    -become-user=USERNAME 指定sudo的用户
    -K 提示输入sudo密码
    二、Ansible中常用模块
    a、ping模块
    语法:
    ansible hosts主机清单 -m 模块名

[root@192 ansible]# ansible 192.168.129.135 -m ping
[root@192 ansible]# ansible all -m ping //all代表所有主机清单
[root@192 ansible]# ansible web -m ping

192.168.129.135 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

b、command模块
command模块用于在远程主机上执行命令,ansible默认就是使用command模块。不能使用特殊得符号 :| > >>

官方文档:https://docs.ansible.com/ansible/latest/modules/command_module.html#command-module

常用参数
名称 含义
chdir 执行命令前先进入到指定目录
cmd 运行命令指定
creates 当指定文件存在时,后一条命令不执行 / 指定文件不存在,后一条命令执行
removes 当指定文件存在时,后一条命令执行 / 指定文件不存在,后一条命令不执行
语法:
ansible 主机清单 -m 模块名 -a ‘执行命令’

/etc文件存在,则touch命令不执行

[root@192 ansible]# ansible web -m command -a ‘chdir=/tmp creates=/etc touch file1’
192.168.129.137 | SUCCESS | rc=0 >>
skipped, since /etc exists

/etc文件存在,则touch命令执行

[root@192 ansible]# ansible web -m command -a ‘chdir=/tmp removes=/etc touch file1’
[WARNING]: Consider using the file module with state=touch rather than running ‘touch’. If you need to use command because file is
insufficient you can add ‘warn: false’ to this command task or set ‘command_warnings=False’ in ansible.cfg to get rid of this message.
192.168.129.137 | CHANGED | rc=0 >>

linux中的通配符 | 重定向在command模块中不适用

[root@192 ansible]# ansible web -m command -a ‘chdir=/tmp rm -rf *’
[WARNING]: Consider using the file module with state=absent rather than running ‘rm’. If you need to use command because file is
insufficient you can add ‘warn: false’ to this command task or set ‘command_warnings=False’ in ansible.cfg to get rid of this message.
192.168.129.137 | CHANGED | rc=0 >>

[root@shazi tmp]# ls
file1

[root@192 ansible]# ansible web -m shell -a ‘chdir=/tmp rm -rf *’
[WARNING]: Consider using the file module with state=absent rather than running ‘rm’. If you need to use command because file is
insufficient you can add ‘warn: false’ to this command task or set ‘command_warnings=False’ in ansible.cfg to get rid of this message.
192.168.129.137 | CHANGED | rc=0 >>

[root@shazi tmp]# ls
c、raw模块
raw模块支持管道符与重定向

//支持重定向
[root@192 ansible]# ansible web -m raw -a ‘echo ‘hello’ > /tmp/abc’
192.168.129.137 | CHANGED | rc=0 >>
Shared connection to 192.168.129.137 closed.

[root@192 ansible]# ansible web -m command -a ‘cat /tmp/abc’
192.168.129.137 | CHANGED | rc=0 >>
hello

//支持管道符
[root@192 ansible]# ansible web -m raw -a’cat /tmp/abc | grep -Eo hello’
Enter passphrase for key ‘/root/.ssh/id_rsa’:
192.168.129.137 | CHANGED | rc=0 >>
hello
Shared connection to 192.168.129.137 closed.
d、shell模块
shell模块用于在受控机上执行受控机上的脚本,亦可直接在受控机上执行命令。
shell模块亦支持管道与重定向。

官方文档:https://docs.ansible.com/ansible/latest/modules/shell_module.html#shell-module

常用参数
名称 含义
chdir 执行命令前先进入到指定目录
creates 如果文件存在则不执行
removes 如果文件存在将执行
[root@192 ansible]# ansible web -m shell -a ‘ls /tmp | grep file’
192.168.129.137 | CHANGED | rc=0 >>
file

[root@192 ansible]# ansible web -m shell -a ‘mkdir /root/test1’
[WARNING]: Consider using the file module with state=directory rather than running ‘mkdir’. If you need to use command because file is
insufficient you can add ‘warn: false’ to this command task or set ‘command_warnings=False’ in ansible.cfg to get rid of this message.
192.168.129.137 | CHANGED | rc=0 >>

[root@shazi ~]# ls
abc anaconda-ks.cfg test1
ansible常用模块raw、command、shell的区别:
名称 含义
shell模块 调用/bin/sh指令执行
command模块 不是调用的shell的指令,所以没有bash的环境变量
raw模块 很多地方和shell类似,更多的地方建议使用shell和command模块
e、script模块
script模块用于在受控机上执行主控机上的脚本

[root@192 ansible]# mkdir /scripts
[root@192 ~]# cd /scripts/
[root@192 scripts]# cat a.sh
#! /bin/bash
echo hello world
echo zhi shazi
echo xioii
[root@192 scripts]# chmod +x /scripts/a.sh
[root@192 scripts]# ll /scripts/a.sh
-rwxr-xr-x. 1 root root 30 Jul 18 12:31 /scripts/a.sh

[root@192 scripts]# ansible web -m script -a ‘/scripts/a.sh’
192.168.129.137 | CHANGED => {
“changed”: true,
“rc”: 0,
“stderr”: “Shared connection to 192.168.129.137 closed.\r\n”,
“stderr_lines”: [
“Shared connection to 192.168.129.137 closed.”
],
“stdout”: “hello world\r\nzhi shazi\r\nxioii\r\n”,
“stdout_lines”: [
“hello world”,
“zhi shazi”,
“xioii”
]
}

f、template模块
template模块用于生成一个模板,并可将其传输至远程主机上。

1、下载一个Centos-8的yum源文件
[root@192 yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2595 100 2595 0 0 10136 0 --:–:-- --:–:-- --:–:-- 10136

2、将Centos-8源传到受控主机
[root@192 yum.repos.d]# ansible web -m template -a ‘src=/etc/yum.repos.d/CentOS-Base.repo dest=/etc/yum.repos.d/Centos8.repo’
192.168.129.137 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: true,
“checksum”: “4966466ad015ef3d2a3cc0b8252d43efbdcf2c94”,
“dest”: “/etc/yum.repos.d/Centos8.repo”,
“gid”: 0,
“group”: “root”,
“md5sum”: “d06fb7d5709727828bcaba7457ea673e”,
“mode”: “0644”,
“owner”: “root”,
“secontext”: “system_u:object_r:system_conf_t:s0”,
“size”: 2595,
“src”: “/root/.ansible/tmp/ansible-tmp-1626586130.9004688-479453-51846552834534/source”,
“state”: “file”,
“uid”: 0
}
3、查看受控机上是否有Centos-8源
[root@shazi yum.repos.d]# ls
Centos8.repo CentOS-Base.repo redhat.repo

g、yum模块
官方文档:https://docs.ansible.com/ansible/latest/modules/yum_repository_module.html#yum-repository-module

参数 选项/默认值 释义
name(required) 指定软件名称信息
state absent/removed 将软件进行卸载(慎用)
= present/installed 将软件进行安装
latest 安装最新的软件 yum update
[root@192 etc]# ansible web -m yum -a “name=wget* state=installed”
Enter passphrase for key ‘/root/.ssh/id_rsa’:
192.168.129.137 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: true,
“msg”: “”,
“rc”: 0,
“results”: [
“Installed: wget-1.19.5-10.el8.x86_64”,
“Removed: wget-1.19.5-8.el8_1.1.x86_64”
]
}
[root@shazi ~]# rpm -qa | grep wget
wget-1.19.5-10.el8.x86_64

h、copy模块
copy模块用于复制文件至远程受控机。

官方文档:https://docs.ansible.com/ansible/latest/modules/copy_module.html#copy-module

update_cache: 更新缓存

name: 要创建的文件名字

state: 状态(present,absent,latest),表示是安装还卸载
   present:默认的,表示为安装
   lastest: 安装为最新的版本
   absent:表示删除

mode: 目标文件的权限模式,模式可以被指定为符号模式(例如,u + rwx 或 u = rw,g = r,o = r)

backup: 如果原目标文件存在,则先备份目标文件

force: 是否强制覆盖,默认为yes

owner: 目标文件属主

group: 目标文件属组

参数 选项/默认值 释义
src 指定将本地管理主机的什么数据信息进行远程复制
backup no* yes 默认数据复制到远程主机,会覆盖原有文件(yes 将源文件进行备份)
content 在文件中添加信息
dest(required) 将数据复制到远程节点的路径信息
group 文件数据复制到远程主机,设置文件属组用户信息
mode 文件数据复制到远程主机,设置数据的权限 eg 0644 0755
owner 文件数据复制到远程主机,设置文件属主用户信息
remote_src no* yes 如果设置为yes,表示将远程主机上的数据进行移动操作如果设置为no, 表示将管理主机上的数据进行分发操作
1、目的文件的权限
[root@192 ansible]# ansible web -m copy -a ‘src=/root/mode.txt dest=/tmp mode=744’
192.168.129.137 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: true,
“checksum”: “da39a3ee5e6b4b0d3255bfef95601890afd80709”,
“dest”: “/tmp/mode.txt”,
“gid”: 0,
“group”: “root”,
“mode”: “0744”,
“owner”: “root”,
“path”: “/tmp/mode.txt”,
“secontext”: “unconfined_u:object_r:admin_home_t:s0”,
“size”: 0,
“state”: “file”,
“uid”: 0
}

[root@192 ansible]# ansible web -m shell -a ‘ls -l /tmp/mode.txt’
192.168.129.137 | CHANGED | rc=0 >>
-rwxr–r–. 1 root root 0 7月 18 12:54 /tmp/mode.txt

2、如果目的地文件已存在,备份目的地的原文件
[root@192 ansible]# ansible web -m copy -a ‘src=/tmp/abc dest=/tmp/abc owner=ddd group=ddd mode=755 backup=yes’
192.168.129.137 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“backup_file”: “/tmp/abc.493505.2021-07-18@13:08:38~”,
“changed”: true,
“checksum”: “71415f2fcb9291dd06cfc730ae2838e69e45c234”,
“dest”: “/tmp/abc”,
“gid”: 1001,
“group”: “ddd”,
“md5sum”: “3c4ee07f99a204bb8a0a0f940a8e1fcd”,
“mode”: “0755”,
“owner”: “ddd”,
“secontext”: “unconfined_u:object_r:user_tmp_t:s0”,
“size”: 18,
“src”: “/root/.ansible/tmp/ansible-tmp-1626584917.9604204-442099-277721032319/source”,
“state”: “file”,
“uid”: 1001
}
[root@shazi tmp]# ll
总用量 8
-rwxr-xr-x. 1 ddd ddd 18 7月 18 13:08 abc
-rw-r–r–. 1 root root 6 7月 17 17:56 abc.493505.2021-07-18@13:08:38~
-rwxr–r–. 1 root root 0 7月 18 12:54 mode.txt
[root@shazi tmp]# cat abc
linux
linux
hello
[root@shazi tmp]# cat abc.493505.2021-07-18@13:08:38~
hello
i、group模块
group模块用于在受控机上添加或删除组

官方文档:https://docs.ansible.com/ansible/latest/modules/group_module.html#group-module

参数 选项/默认值 释义
gid 指创建的组ID信息
name 指创建组名称信息
state absent 删除指定的用户组
= present 创建指定的用户组
1、在受控机上添加一个系统组,其gid为306,组名为zzz
[root@192 ~]# ansible web -m group -a ‘name=zzz gid=306 state=present’
192.168.129.137 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: true,
“gid”: 306,
“name”: “zzz”,
“state”: “present”,
“system”: false
}
[root@192 ~]# ansible web -m shell -a ‘grep zzz /etc/group’
192.168.129.137 | CHANGED | rc=0 >>
zzz❌306:
2、删除受控机上的zzz组
[root@192 ~]# ansible web -m group -a ‘name=zzz state=absent’
192.168.129.137 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: true,
“name”: “zzz”,
“state”: “absent”
}

[root@192 ~]# ansible web -m shell -a ‘grep zzz /etc/group’
192.168.129.137 | FAILED | rc=1 >>
non-zero return code

j、user模块
user模块用于管理受控机的用户帐号

官方文档:https://docs.ansible.com/ansible/latest/modules/user_module.html#user-module

参数 选项/默认值 释义
password 请输入密码信息
name 指定用户名信息
uid 指定用户uid信息
group 指定用户主要属于哪个组
groups 指定用户属于哪个附加组信息
shell /bin/bash或/sbin/nologin 指定是否能够登录
create_home yes/no 是否创建家目录信息
home 指定家目录创建在什么路径 默认/home
1、在受控机上添加一个系统用户,用户名为zzz,uid为366,设置其shell为/sbin/nologin,无家目录
[root@192 ~]# ansible web -m user -a ‘name=zzz uid=366 system=yes create_home=no shell=/sbin/nologin state=present’
192.168.129.137 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: true,
“comment”: “”,
“create_home”: false,
“group”: 366,
“home”: “/home/zzz”,
“name”: “zzz”,
“shell”: “/sbin/nologin”,
“state”: “present”,
“system”: true,
“uid”: 366
}
[root@192 ~]# ansible web -m shell -a ‘grep zzz /etc/group’
192.168.129.137 | CHANGED | rc=0 >>
zzz❌366:
[root@192 ~]# ansible web -m shell -a ‘ls /home’
192.168.129.137 | CHANGED | rc=0 >>
ddd
hhh
hhr

2、修改zzz用户的uid为306
[root@192 ~]# ansible web -m user -a ‘name=zzz uid=306’
192.168.129.137 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“append”: false,
“changed”: true,
“comment”: “”,
“group”: 366,
“home”: “/home/zzz”,
“move_home”: false,
“name”: “zzz”,
“shell”: “/sbin/nologin”,
“state”: “present”,
“uid”: 306
}
[root@192 ~]# ansible web -m shell -a ‘grep zzz /etc/group’
192.168.129.137 | CHANGED | rc=0 >>
zzz❌366:
[root@192 ~]# ansible web -m shell -a ‘grep zzz /etc/passwd’
192.168.129.137 | CHANGED | rc=0 >>
zzz❌306:366::/home/zzz:/sbin/nologin

3、删除用户时不删除家目录
[root@192 ~]# ansible web -m user -a ‘name=zzz state=absent’
192.168.129.137 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: true,
“force”: false,
“name”: “zzz”,
“remove”: false,
“state”: “absent”
}

4、删除用户时删除用户的家目录
[root@192 ~]# ansible web -m user -a ‘name=hhh state=absent remove=yes’
Enter passphrase for key ‘/root/.ssh/id_rsa’:
192.168.129.137 | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: false,
“name”: “hhh”,
“state”: “absent”
}

k、service模块
service模块用于管理受控机上的服务

官方文档:https://docs.ansible.com/ansible/latest/modules/service_module.html#service-module

参数 选项/默认值 释义
enabled no /yes 设置服务是否开机自启动 如果参数不指定,原有服务开机自启动状态进行保留
name (required) 设置要启动/停止服务名称
state= reloaded 平滑重启
= restarted 重启
= started 启动
= stopped 停止
查看受控机上的vsftpd服务是否启动
[root@192 ~]# ansible web -m shell -a ‘systemctl is-active vsftpd’
192.168.129.137 | FAILED | rc=3 >>
inactivenon-zero return code

启动vsftpd服务
1、安装vsftpd服务
[root@192 ~]# ansible web -m yum -a ‘name=vsftpd* state=installed’
192.168.129.137 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: true,
“msg”: “”,
“rc”: 0,
“results”: [
“Installed: vsftpd-3.0.3-33.el8.x86_64”
]
}
2、启动
[root@192 ~]# ansible web -m service -a ‘name=vsftpd state=started’
192.168.129.137 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
…此处省略N行

查看受控机上的vsftpd服务是否启动
[root@192 ~]# ansible web -m shell -a ‘systemctl is-active vsftpd’
192.168.129.137 | CHANGED | rc=0 >>
active

查看受控机上的vsftpd服务是否开机自启
[root@192 ~]# ansible web -m shell -a ‘systemctl is-enabled vsftpd’
192.168.129.137 | FAILED | rc=1 >>
disablednon-zero return code

设置vsftpd服务开机自启
[root@192 ~]# ansible web -m service -a ‘name=vsftpd enabled=yes’
Enter passphrase for key ‘/root/.ssh/id_rsa’:
192.168.129.137 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
…此处省略N行

查看受控机上的vsftpd服务是否开机自启
[root@192 ~]# ansible web -m shell -a ‘systemctl is-enabled vsftpd’
192.168.129.137 | CHANGED | rc=0 >>
enabled

重启vsftpd服务
[root@192 ~]# ansible web -m service -a ‘name=vsftpd state=restarted’
Enter passphrase for key ‘/root/.ssh/id_rsa’:
192.168.129.137 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
…此处省略N行

停止受控机上的vsftpd服务
[root@192 ~]# ansible web -m service -a ‘name=vsftpd state=stopped’
192.168.129.137 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
…此处省略N行
[root@192 ~]# ansible web -m shell -a ‘systemctl is-active vsftpd’
Enter passphrase for key ‘/root/.ssh/id_rsa’:
192.168.129.137 | FAILED | rc=3 >>
inactivenon-zero return code

l、file模块
官方文档:https://docs.ansible.com/ansible/latest/modules/file_module.html#file-module

参数 选项/默认值 释义
dest/path/name(required) 将数据复制到远程节点的路径信息
group 文件数据复制到远程主机,设置文件属组用户信息
mod 文件数据复制到远程主机,设置数据的权限 eg 0644 0755
owner 文件数据复制到远程主机,设置文件属主用户信息
src 指定将本地管理主机的什么数据信息进行远程复制
state absent 将数据进行删除
= directory 创建一个空目录信息
= file 查看指定目录信息是否存在
= touch 创建一个空文件信息
= hard/link 创建链接文件
名称 含义
state=absent 删除文件或者目录
owner 属主
group 属组
set 4777 属主特殊权限
2777 属组特殊权限
1777 执行特殊权限
7777 全部特殊权限
1、创建一个普通文件
[root@192 ~]# ansible web -m file -a ‘path=/tmp/gool state=touch’

2、创建一个目录
[root@192 ~]# ansible web -m file -a ‘path=/tmp/test state=directory’

3、查看
[root@192 ~]# ansible web -m shell -a ‘ls /tmp’
192.168.129.137 | CHANGED | rc=0 >>
gool
test

4、删除文件
[root@192 ~]# ansible web -m file -a ‘state=absent path=/tmp/gool’

5、删除目录
[root@192 ~]# ansible web -m file -a ‘state=absent path=/tmp/test’

6、查看
[root@192 ~]# ansible web -m shell -a ‘ls /tmp’
192.168.129.137 | CHANGED | rc=0 >>

7、创建一个软连接文件
[root@192 ~]# ansible web -m file -a ‘state=link src=/tmp/goole path=/tmp/谷歌’

[root@shazi tmp]# ll
总用量 0
lrwxrwxrwx. 1 root root 10 7月 18 16:34 谷歌 -> /tmp/goole
-rw-r–r–. 1 root root 0 7月 18 16:33 goole

8、创建一个硬连接文件
[root@192 ~]# ansible web -m file -a ‘state=hard src=/etc/passwd path=/tmp/passwd’

[root@shazi tmp]# ll
总用量 4
-rw-r–r–. 3 root root 2655 7月 18 15:29 passwd
[root@shazi etc]# ll /passwd
-rw-r–r–. 1 root root 2655 7月 18 15:29 /passwd

9、创建一个文件,并设置权限,属主,属组
//创建zzz用户
[root@192 ~]# ansible web -m user -a ‘name=zzz uid=366 system=yes create_home=no shell=/sbin/nologin state=present’

[root@192 ~]# ansible web -m file -a ‘state=touch path=/tmp/fiel.txt mode=744 owner=zzz group=zzz’

10、修改一个文件得权限,属主,属组
[root@192 ~]# ansible web -m file -a ‘path=/tmp/goole mode=777’
[root@192 ~]# ansible web -m file -a ‘path=/tmp/fiel.txt owner=root group=root’

11、修改一个文件得set位特殊权限
[root@192 ~]# ansible web -m file -a ‘path=/tmp/goole mode=2777’
[root@192 ~]# ansible web -m shell -a ‘ls -l /tmp/goole’
192.168.129.137 | CHANGED | rc=0 >>
-rwxrwsrwx. 1 root root 0 7月 18 16:33 /tmp/goole

12、若想同时改变目录及其中的子目录或字文件的属性,可以用recurse=yes参数实现
[root@192 ~]# ansible web -m file -a ‘path=/tmp group=zzz recurse=yes’
[root@shazi tmp]# ll
总用量 4
lrwxrwxrwx. 1 root zzz 10 7月 18 16:34 谷歌 -> /tmp/goole
-rwxrwxrwx. 1 root zzz 0 7月 18 16:42 fiel.txt
-rwxrwxrwx. 1 root zzz 0 7月 18 16:33 goole

m、lineinfile模块
名称 含义
regexp=’^ $’ 正则匹配,匹配数字
path 指定要操作的文件
line 指定文本的内容
regexp 使用正则表达式匹配对应的行
当替换文本时,如果有多行文本被匹配,只替换最后匹配的那一行
当删除文本时,如果有多行文本被匹配,那么这些行都会被删除
state state的默认值为present
state=absent时,表示删除文本
backrefs 当没有根据匹配规则找到对应的行时不做任何更改,默认值为no
当 backrefs=yes,没有根据匹配规则找到对应的行,指定的文本内容添加
当backrefs=yes时,也可以向后引用regexp变量信息
insertafter 将文本插入到指定的行之后,参数的值可以设定为EOF或者正则表达式
insertbefore 将文本插入到指定的行之前,参数的值可以设定为BOF或者正则表达式
backup 是否在修改文件前备份
create 当要操作的文件不存在时,是否创建对应的文件
1、当要操作的文件不存在时,是否创建对应的文件
[root@192 ~]# ansible web -m lineinfile -a 'path=/tmp/fiel line=“hello woorld” ’
192.168.129.137 | FAILED! => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: false,
“msg”: “Destination /tmp/fiel does not exist !”,
“rc”: 257
}

//create=yes当要操作的文件不存在时,创建对应的文件
[root@192 ~]# ansible web -m lineinfile -a ‘path=/tmp/fiel line=“hello woorld” create=yes’
192.168.129.137 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“backup”: “”,
“changed”: true,
“msg”: “line added”
}

//查看
root@shazi tmp]# ll
总用量 8
-rw-r–r–. 1 root root 13 7月 18 17:26 fiel
-rw-r–r–. 1 root root 96 7月 18 17:26 fiel.txt
-rw-r–r–. 1 root root 0 7月 18 16:33 goole

2、将匹配到的含hello字符串的最后一行行替换为line中的字符串
[root@192 ~]# ansible web -m lineinfile -a ‘path=/tmp/fiel line=“Centos” regexp=‘hello’ create=yes’

[root@shazi tmp]# cat fiel
linux
hello
world hello
helll
Centos

3、当 backrefs=yes,同时根据匹配规则找不到相应的行时,不对文件进行任何改变
[root@192 ~]# ansible web -m lineinfile -a ‘path=/tmp/fiel line=“hehe” regexp=“jjyy” backrefs=yes’
1
4、^正则表达式
[root@192 ~]# ansible web -m lineinfile -a 'path=/etc/selinux/config regexp=“^SELINUX=” line=“SELINUX=disabled” ’

[root@shazi selinux]# cat config

This file controls the state of SELinux on the system.

SELINUX= can take one of these three values:

enforcing - SELinux security policy is enforced.

permissive - SELinux prints warnings instead of enforcing.

disabled - No SELinux policy is loaded.

SELINUX=disabled

SELINUXTYPE= can take one of these three values:

targeted - Targeted processes are protected,

minimum - Modification of targeted policy. Only selected processes are protected.

mls - Multi Level Security protection.

5、在文件的开头插入line中的字符
[root@192 ~]# ansible web -m lineinfile -a ‘path=/tmp/fiel line=“shazi” insertbefore=BOF’

[root@shazi tmp]# cat fiel
shazi
linux
hello

6、在文件的结尾插入line中的字符
[root@192 ~]# ansible web -m lineinfile -a ‘path=/tmp/fiel line=“xxx” insertafter=EOF’
[root@shazi tmp]# cat fiel
shazi
linux
Centos
xxx

7、在含有hello字符的行前插入line中的字符
[root@192 ~]# ansible web -m lineinfile -a 'path=/tmp/fiel line=“****” insertbefore=“hello” ’

[root@shazi tmp]# cat fiel
shazi
linux


hello
Centos
xxx

8、在含有hello字符的行后插入line中的字符
[root@192 ~]# ansible web -m insertafter -a 'path=/tmp/fiel line=“%%%%” insertbefore=“hello” ’

[root@shazi tmp]# cat fiel
shazi
linux


hello
%%%%
Centos
xxx

n、blockinfile 模块
给文件中添加一段内容,并注明标志

1、添加
block=‘’ 文档内容
marker=‘#{mark}’ 标志信息
state 将指定文本"插入"到文件中

[root@192 ~]# ansible web -m blockinfile -a ‘block=“nginx” path=/tmp/fiel marker=“#{mark} nginx”’
Enter passphrase for key ‘/root/.ssh/id_rsa’:
192.168.129.137 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: true,
“msg”: “Block inserted”
}
[root@shazi tmp]# cat fiel
#BEGIN nginx
nginx
#END nginx

2、在末尾添加
[root@192 ~]# ansible web -m blockinfile -a 'path=/tmp/fiel block="systemctl\start " ’

[root@shazi tmp]# cat fiel
shazi
linux

BEGIN ANSIBLE MANAGED BLOCK

systemctl\start

END ANSIBLE MANAGED BLOCK

3、删除:
[root@192 ~]# ansible web -m blockinfile -a ‘path=/tmp/fiel block="systemctl\start " state=absent’

[root@shazi tmp]# cat fiel
shazi
linux

o、cron模块
官方文档:https://docs.ansible.com/ansible/latest/modules/cron_module.html#cron-module

参数 选项/默认值 释义
minute/hour/day/month/weekday 和设置时间信息相关参数
job 和设置定时任务相关参数
name(required) 设置定时任务注释信息
state absent 删除指定定时任务
disabled yes 将指定定时任务进行注释
= no 取消注释
1、模块方法

  1. backup:对远程主机上的原任务计划内容修改之前做备份
  2. cron_file:如果指定该选项,则用该文件替换远程主机上的cron.d目录下的用户的任务计划
  3. day:日(1-31,/2,……)
  4. hour:小时(0-23,/2,……)
  5. minute:分钟(0-59,/2,……)
  6. month:月(1-12,/2,……)
  7. weekday:周(0-7,*,……)
  8. job:要执行的任务,依赖于state=present
  9. name:该任务的描述
  10. special_time:指定什么时候执行,参数:reboot,yearly,annually,monthly,weekly,daily,hourly
  11. state:确认该任务计划是创建还是删除
  12. user:以哪个用户的身份执行

2、示例:
添加定时任务
[root@192 ~]# ansible web -m cron -a ‘name=“test crontab” minute=5 hour=1 job=“echo test”’
192.168.129.137 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: true,
“envs”: [],
“jobs”: [
“test crontab”
]
}
[root@shazi tmp]# crontab -l
#Ansible: test crontab
5 1 * * * echo test

移除定时任务
[root@192 ~]# ansible web -m cron -a ‘name=“test crontab” minute=5 hour=1 job=“echo test” state=“absent”’
192.168.129.137 | CHANGED => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/libexec/platform-python”
},
“changed”: true,
“envs”: [],
“jobs”: []
}

p、unarchive 解压模块

  • name: Unarchive a file that is already on the remote machine
    unarchive:
    src: /tmp/foo.zip #要解压的包
    dest: /usr/local/bin #解压到目标位置
    remote_src:
    yes #要解压的包在受控端
    no #要解压的包在控制端

示例:

#1.解压控制端的包到受控端
[root@m01 /package]# ansible web -m unarchive -a ‘src=/package/php.tar.gz dest=/tmp/’

#2.解压受控端的包到受控端
[root@m01 /package]# ansible web -m unarchive -a ‘src=/package/php.tar.gz dest=/tmp/ remote_src=yes’

q、archive 压缩模块
EXAMPLES:

  • name: Compress directory /path/to/foo/ into /path/to/foo.tgz
    archive:
    path: /path/to/foo #要压缩的文件或目录
    dest: /path/to/foo.tgz #压缩后的文件
    format:bz2, gz, tar, xz, zip #指定打包的类型

示例:

#1.打包站点目录
[root@m01 /package]# ansible web01 -m archive -a ‘path=/code dest=/tmp/code.tar.gz’

原文链接:https://blog.csdn.net/qq_48289488/article/details/118856969

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值