file模块
主要用于远程主机上的文件操作
常用参数
owner:定义文件/目录的所属主
group:定义文件/目录的所属组
mode:定义文件/目录的权限
path:必选项,定义文件/目录的路径
recurse :递归设置文件的属性,只对目录有效果
src:链接源文件的路径,只应用于state=link的情况
dest 被链接的路径,只应用于state=link的情况
state= (directory 如果目录不存在,创建目录
file 文件不存在,则不会被创建,存在则返回文件的信息,常用于检查文件是否存在
link:创建软链接
hard:创建硬链接
touch:如果文件不存在,则会创建一个新的文件,如果文件或者目录已存在,则更新其最后的修改时间
absent 删除目录,文件或者取消链接文件)
- EXAMPLE
创建一个文件
[root@localhost opt]# ansible dbserver -i hosts -m file -a "path=/tmp/xjm.conf state=touch"
192.168.88.129 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp/xjm.conf",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 0,
"state": "file",
"uid": 0
如果被管理节点上面,没有该文件,就会创建文件,
2.改变文件所有者及权限
> [root@localhost opt]# ansible dbserver -i hosts -m file
-a "path=/tmp/xjm.conf owner=nobody group=nobody mode=0644"
> 192.168.88.129 | CHANGED => {
> "ansible_facts": {
> "discovered_interpreter_python": "/usr/bin/python"
> },
> "changed": true,
> "gid": 99,
> "group": "nobody",
> "mode": "0644",
> "owner": "nobody",
> "path": "/tmp/xjm.conf",
> "secontext": "unconfined_u:object_r:user_tmp_t:s0",
> "size": 0,
> "state": "file",
> "uid": 99 }
3.创建一个目录
> [root@localhost opt]# ansible dbserver -i hosts -m file -a
"path=/tmp/xjm/ state=directory"
> 192.168.88.129 | CHANGED => {
> "ansible_facts": {
> "discovered_interpreter_python": "/usr/bin/python"
> },
> "changed": true,
> "gid": 0,
> "group": "root",
> "mode": "0755",
> "owner": "root",
> "path": "/tmp/xjm/",
> "secontext": "unconfined_u:object_r:user_tmp_t:s0",
> "size": 6,
> "state": "directory",
> "uid": 0 }
在被管理节点上面,目录就会被创建好
4.删除一个文件
> [root@localhost opt]# ansible dbserver -i hosts -m file -a
"path=/tmp/xjm.conf state=absent"
> 192.168.88.129 | CHANGED => {
> "ansible_facts": {
> "discovered_interpreter_python": "/usr/bin/python"
> },
> "changed": true,
> "path": "/tmp/xjm.conf",
> "state": "absent" }
cron模块
管理远程节点的cron服务,等同于linux中的计划任务,
注意:使用ansible创建的计划任务不能使用本地的crontab -e编辑 否则,ansible无法再次操作
常用参数:
name:名字
minute :分钟 (0-59,* ,* /2等)格式,默认是 * ,就是每分钟
hour:小时 可以设置(0-23,* ,* /2等)格式,默认是 * 也就是每小时
day :天 ,可以设置成(1-31,* ,* /2等)格式,默认是 * 也就是每一天
month 指定月份,可以设置成为(1-12,* ,*/ 2等)格式
weekday 指定星期,可以设置成为(0-6)等默认是 * ,也就是每星期
job 指定要执行的内容,统称可以写个脚本,或者一段内容,
state 指定这个job的状态,可以是新增(present)或者删除(absent)默认是为新增
- example
新建一个cron job任务
> [root@localhost opt]# ansible dbserver -i hosts -m cron -a
"name='create new job' minute='0' job='ls -alh > /dev/null'"
> 192.168.88.129 | CHANGED => {
> "ansible_facts": {
> "discovered_interpreter_python": "/usr/bin/python"
> },
> "changed": true,
> "envs": [],
> "jobs": [
> "create new job"
> ] }
在被管理节点上面查看
> [root@localhost tmp]# crontab -l
> #Ansible: create new job 0 * * * * ls -alh > /dev/null
debug模块
debug模块主要用于调试的使用,通常的作用是将一个变量的结果打印出来
常用参数:
var:直接打印一个指定的变量值
msg:打印一段可以格式化的字符串
-e传变量
example
给role赋值
[root@localhost opt]# ansible dbserver -i hosts -m debug -a "var=role" -e "role=web"
192.168.88.129 | SUCCESS => {
"role": "web"
}
> [root@localhost opt]# ansible all -i hosts -m debug -a "msg='role is {{role}}'" -e "role=web"
template模块
传递给ansible的值替换到模板里面
template模块使用了jinjia2格式作为文件模板,可以进行文档内变量的替换,文件名以.j2结尾
常用参数:
src:指定ansible控制端的文件路径
dest:指定ansible被控端的文件路径
owner:指定文件所属主
group:指定文件所属组
mode:指定文件的权限
backup 创建一个包含时间戳信息的备份文件,这样如果您以某种方式错误地破坏了原始文件,就可以将其恢复原状,yes 、no
- example
> [root@localhost opt]# ansible dbserver -i hosts -m template -a
"src=hello_world.j2 dest=/tmp/hello_world.world" -e "var=world"
> 192.168.88.129 | CHANGED => {
> "ansible_facts": {
> "discovered_interpreter_python": "/usr/bin/python"
> },
> "changed": true,
> "checksum": "22596363b3de40b06f981fb85d82312e8c0ed511",
> "dest": "/tmp/hello_world.world",
> "gid": 0,
> "group": "root",
> "md5sum": "6f5902ac237024bdd0c176cb93063dc4",
> "mode": "0644",
> "owner": "root",
> "secontext": "unconfined_u:object_r:admin_home_t:s0",
> "size": 12,
> "src": "/root/.ansible/tmp/ansible-tmp-1639157948.97-2532-263085633128524/source",
>
> "state": "file",
> "uid": 0 }
在被管理节点上面就能看到
lineinfile模块
在被管理节点上,用正则匹配的方式对目标文件的一行内容进行修改的操作
常用参数:
path 被管理节点目标文件路径
state 可选值absent删除,present替换
regexp 在文件的每一行中查找的正则表达式
line要在文件中插入/替换的行,需要state=present,
create 文件不存在时,是否要创建文件并添加内容个,yes 、no example
把被控节点上面 /etc/sudoers 中的以%wheel开头的内容被删除
> [root@localhost opt]# ansible dbserver -i hosts -m lineinfile -a
"path=/etc/sudoers regexp='^%wheel' state=absent"
> 192.168.88.129 | CHANGED => {
> "ansible_facts": {
> "discovered_interpreter_python": "/usr/bin/python"
> },
> "backup": "",
> "changed": true,
> "found": 1,
> "msg": "1 line(s) removed" }
替换某一行
line后面要加state= present
> [root@localhost opt]# ansible dbserver -i hosts -m lineinfile -a
"path=/etc/selinux/config regexp='^SELINUX=' line='SELINUX=disabled'
state=present"
> 192.168.88.129 | CHANGED => {
> "ansible_facts": {
> "discovered_interpreter_python": "/usr/bin/python"
> },
> "backup": "",
> "changed": true,
> "msg": "line replaced" }
blockinifle模块
对一个文件进行一次性修改或者更新多行内容,可以使用blockinfile模块
在/etc/ssh/sshd_config 中添加两行内容 \n就是换行
> [root@localhost opt]# ansible dbserver -i hosts -m blockinfile -a
"path=/etc/ssh/sshd_config block='welcome to china \n hahahahahaha'"
> 192.168.88.129 | CHANGED => {
> "ansible_facts": {
> "discovered_interpreter_python": "/usr/bin/python"
> },
> "changed": true,
> "msg": "Block inserted" }
在被管理节点上面就能看到了