ansible 和文件复制相关的几个模块
2018-01-16 18:21
387 查看
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=r
file 模块
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 文件。
相关文章推荐
- ansible几个常用模块及参数的简单演示(2)
- Python模块整理(一):文件系统相关模块os
- ansible文件模块使用
- Drupal cache相关的几个模块
- VC中几个小问题总结---文件复制,重命名,结构体初始化
- 与PP模块相关的几个话题[转摘]
- ansible文件模块使用
- Ubuntu 与用户帐号、用户组相关的几个重要文件
- 以下列出几个和模块开发紧密相关的类: DNN控件开发
- 【Ansible学习】- 常用文件操作模块之copy模块
- 几个相关文件系统概念:geometry、sector 、Block
- mysql 主从复制相关的日志文件
- Python的shutil模块中文件的复制操作函数详解
- PHPCMS模块开发相关文件
- C语言中几个与临时文件操作相关的函数
- 重新想象 Windows 8 Store Apps (70) - 其它: 文件压缩和解压缩, 与 Windows 商店相关的操作, app 与 web, 几个 Core 的应用, 页面的生命周期和程序的生命周期
- lucene 文件存储相关的几个类
- 泡菜模块和几个对文件处理的 第三方模块
- ubuntu useradd 相关的几个重要文件
- Ansible中几个常用模块的使用