重庆思庄Oracle、Redhat认证学习论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2392|回复: 0
打印 上一主题 下一主题

[基础命令] ansible 和文件复制相关的几个模块

[复制链接]
跳转到指定楼层
楼主
发表于 2019-12-18 22:49:59 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 郑全 于 2019-12-18 23:09 编辑

ansible 和文件复制相关的几个模块

ansible 复制远程主机的文件到本地主机——fetch

例子1:
[root@centos_7 ansible]# cat fetched.yml
----
hosts: centos7  
remote_user: root  tasks:   
- name : fetched file from centos7 into local      
  fetch:         
     src: /tmp/fstab_from_centos6        
     dest: /tmp/centos

说明:src  - 是远程主机的文件,这个参数只能使用文件,不能使用目录
          dest - 是本地主机,用来保存文件的目录。它会将这里的参数看成是目录。
          注意,在执行ansible 成功后,dest 的路径会自动将远程文件保存到 /tmp/centos/remote_host/tmp/fstab_from_centos6

上面例子的结果:
[root@centos_7 ansible]# ls  /tmp/centos/example.fetch.host/tmp
fstab_from_centos6
example.fetch.host 是在 /etc/ansible/hosts 主机列表里 centos7 对应的名称

例子2:保存到本地时,直接使用远程的文件名,不需要remote_host 创建新的目录

[root@centos_7 ansible]# cat fetched.yml

----
hosts: centos7  
remote_user: root  
tasks:   
   - name : fetched file from centos7 into local      
      fetch:         
         src: /tmp/fstab_from_centos6        
         dest: /tmp/        
         flat: yes


增加 flat 参数,当flat 为 yes 时,有两种情况:
(1)dest 是以"/" 结尾,文件直接保存在 dest 的目录下,所以现在的结果是:fstab_from_centos6 保存在本地主机的 /tmp/fstab_from_centos6
(2) dest 不是以 "/" 结尾,则dest 被看做文件,相当于 fstab_from_centos6 会重命名再保存。


例如:[root@centos_7 ansible]# cat fetched.yml


----
hosts: centos7  
remote_user: root  
tasks:   
  - name : fetched file from centos7 into local      
    fetch:         
        src: /tmp/fstab_from_centos6        
        dest: /tmp/centos        
        flat: yes


fstab_from_centos6 被保存为 /tmp/cnetos

复制本地(或远程)文件 到远程主机
# Example from Ansible Playbooks
- copy:   
      src: /srv/myfiles/foo.conf   
      dest: /etc/foo.conf   
      owner: foo   
      group: foo   
      mode: 0644
# The same example as above, but using a symbolic mode equivalent to 0644


- copy:   
      src: /srv/myfiles/foo.conf   
      dest: /etc/foo.conf   
      owner: foo   
      group: foo   
      mode: u=rw,g=r,o=rfile

模块file 模块用来修改文件、目录和符号文件的属性权限等。利用它的 state 参数,有更多丰富的用法。


修改权限属性:
[root@centos_7 ansible]# ansible centos6 -m command -a "chdir=/tmp/new/ ls 1.txt -l"

192.168.188.109 | SUCCESS | rc=0 >>-rw-r--r-- 1 root root 0 Jan 16 12:22 1.txt

[root@centos_7 ansible]# ansible centos6 -m file -a "path=/tmp/new/1.txt mode=0777"
192.168.188.109 | SUCCESS =>
{    "changed": true,     "gid": 0,     "group": "root",     "mode": "0777",     "owner": "root",    
      "path": "/tmp/new/1.txt",     "size": 0,     "state": "file",     "uid": 0}

[root@centos_7 ansible]# ansible centos6 -m command -a "chdir=/tmp/new/ ls 1.txt -l"
192.168.188.109 | SUCCESS | rc=0 >>-rwxrwxrwx 1 root root 0 Jan 16 12:22 1.txt

说明: 将远程主机 /tmp/new/1.txt 的文件修改权限属性为0777;
ansible 命令中,path 相当于 dest,指明了目标文件。如果想修改所属主和所属组,
可以:ansible  centos6  -m  file  -a  "path=/tmp/new/1.txt   mode=0755 ower=apple group=apple"
命令执行结果:
[root@centos_7 ansible]# ansible centos6 -m file -a "path=/tmp/new/1.txt mode=0755 owner=apple group=apple"

192.168.188.109 | SUCCESS => {    "changed": true,     "gid": 508,     "group": "apple",     "mode": "0755",     "owner": "apple",     "path": "/tmp/new/1.txt",     "size": 0,     "state": "file",     "uid": 508}

[root@centos_7 ansible]# ansible centos6 -m command -a "chdir=/tmp/new/ ls 1.txt -l"

192.168.188.109 | SUCCESS | rc=0 >>-rwxr-xr-x 1 apple apple 0 Jan 16 12:22 1.txt

说明: 只要增加 owner 和 group 参数就可以了。

file 的 state 参数
熟悉 state 的几个参数,基本就满足我们平时对文件、目录和符号链接文件的操作了。先来看看 state 有几个值?

state 的值和我们要进行的操作,其实是一一对应的。

1. state=directory  --  如果想创建一个目录

[root@centos_7 ansible]# ansible test -m file -a "path=/tmp/testdir state=directory"

192.168.188.109 | SUCCESS => {    "changed": true,     "gid": 0,     "group": "root",     "mode": "0755",     "owner": "root",     "path": "/tmp/testdir",     "size": 4096,     "state": "directory",     "uid": 0}

可以看到,在远程主机已经创建了 /tmp/testdir 目录:

[root@centos_7 ansible]# ansible test -m command  -a "chdir=/tmp ls -lthr"

192.168.188.109 | SUCCESS | rc=0 >>
total 16K
-rw-r--r-- 1 root root  484 Jan 12 15:08 fstab
drwxr-xr-x 2 root root 4.0K Jan 16 12:22 new
drwxr-xr-x 2 root root 4.0K Jan 16 14:50 testdir
drwx------ 2 root root 4.0K Jan 16 14:51 ansible_IOZLxg

2. state=touch --  创建文件

[root@centos_7 ansible]# ansible test -m file -a "path=/tmp/new/1.txt state=touch"

[root@centos_7 ansible]# ansible test -m command -a "chdir=/tmp/new ls -lthr"

192.168.188.109 | SUCCESS | rc=0 >>
total 0-rw-r--r-- 1 root root 0 Jan 16 15:00 1.txt

注意: 使用 state=file ,当 path 指定的文件不存在的时候,并不能新创建文件。
重复对相同的文件使用 touch 只能改变文件的元数据属性,例如访问时间等等 stat 查看到的数据。

3. state=link  -- 创建符号链接        

当stat=link 的时候,就需要使用另一个 src 的参数一起搭配使用了。

[root@centos_7 ansible]# ansible test -m file -a "path=/tmp/new/1_link.txt src=/tmp/new/1.txt state=link"
结果:

[root@centos_7 ansible]# ansible test -m command -a "ls /tmp/new -l"

192.168.188.109 | SUCCESS | rc=0 >>
total 0
lrwxrwxrwx 1 root root 14 Jan 16 15:25 1_link.txt -> /tmp/new/1.txt
-rw-r--r-- 1 root root  0 Jan 16 15:02 1.txt

说明: path 指定了生成的符号链接文件的路径, src 指定了 被链接的文件是什么。

4. state=hard  --  创建硬链接

[root@centos_7 ansible]# ansible test -m file -a "path=/tmp/new/1_hard.txt src=/tmp/new/1.txt state=hard"

[root@centos_7 ansible]# ansible test -m command -a "ls -l /tmp/new "

192.168.188.109 | SUCCESS | rc=0 >>
total 0
-rw-r--r-- 2 root root  0 Jan 16 15:02 1_hard.txt
lrwxrwxrwx 1 root root 14 Jan 16 15:25 1_link.txt -> /tmp/new/1.txt
-rw-r--r-- 2 root root  0 Jan 16 15:02 1.txt

可以看到,1.txt 和 1_hard.txt  文件的属性都是一样的。

5.  state=absent  -- 删除目录、文件和符号链接

[root@centos_7 ansible]# ansible test -m file -a "path=/tmp/new/1_link.txt state=absent"

这样就删除了一个符号链接文件。
template 模块

template 模块官网说明:template使用了Jinja2格式作为文件模版,进行文档内变量的替换的模块。

它的每次使用都会被ansible标记为”changed”状态。通俗来讲,就是在本地 file.j2 文件里的变量状态。

例如在file.j2 文件里有变量: username="{{ admin_username }}" password="{{ admin_password }}"。

那么,file.j2 文件会一直保持变量状态,直到 file.j2 文件被 ansible 的 template 模块执行后,文件里的变量就会被具体的值代替,
并且传送到远程主机。例如,在roles 里面调用了 template 模块,那么ansible 会在 roles 同级目录 global_vars 里找到存储变量的文件,
或者有时会在 roles 目录里的 vars 目录定义变量的文件,然后,找到对应变量的值,再将变量传递给 file.j2 文件。


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 支持支持 反对反对
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|重庆思庄Oracle、Redhat认证学习论坛 ( 渝ICP备12004239号-4 )

GMT+8, 2024-5-19 15:25 , Processed in 0.118318 second(s), 20 queries .

重庆思庄学习中心论坛-重庆思庄科技有限公司论坛

© 2001-2020

快速回复 返回顶部 返回列表