05ansible file copy fetch lineinfile replace模块使用案例
标签(空格分隔): ansible后
文章目录

file模块的用法
创建文件.修改权限等
命令行帮助


三条命令分别执行
ansible proxy -m file -a “path=/tmp/file.txt state=touch”
ansible proxy -m file -a “path=/tmp/mydir state=directory”
ansible proxy -m file -a “path=/tmp/file.txt owner=sshd group=adm mode=0777”



受控主机proxy查看结果


三条命令执行
ansible proxy -m file -a “path=/tmp/mydir state=absent”
ansible proxy -m file -a “path=/tmp/file.txt state=absent”
ansible proxy -m file -a “src=/etc/hosts path=/tmp/hosts.txt state=link”

受控主机proxy查看执行结果 文件已被删除 同时也创建了目标链接

copy模块的用法
拷贝文件

ansible proxy -m copy -a “src=~/ceshi.txt dest=/root/”
ansible proxy -m copy -a “src=~/ceshi.txt dest=/root/xiugai.txt”
ansible proxy -m copy -a “content=‘hello world\n’ dest=/root/hello.txt”
执行命令测试结果 有文件复制过去

受控端执行结果

复制并修改名称

幂等性 绿色 执行结果并无区别

本地无文件,直接使用content定义文件内容进行操作

fetch模块的用法
与copy相反
拷贝受控端文件到控制端

可用于批量收集受控端的日志文件等
ansible proxy -m fetch -a “src=/etc/hostname dest=~/”

lineinfile|replace模块的用法
编辑文件

ansible proxy -m lineinfile -a “path=/etc/issue line=‘hello world’”
ansible proxy -m lineinfile -a “path=/etc/issue line=‘insert’ insertafter=‘Kernel’”







本文介绍了Ansible中file、copy、fetch和lineinfile|replace模块的使用案例。file模块用于创建、修改文件及权限;copy模块实现文件拷贝;fetch模块将受控端文件复制到控制端;lineinfile|replace模块则用于编辑文件内容。通过这些模块,可以轻松实现远程系统的文件管理与配置更新。
933

被折叠的 条评论
为什么被折叠?



