2019-04-13 Ansible 文件拷贝/同步playbook脚本

文件管理的模块非常多,可以参考https://docs.ansible.com/ansible/2.3/list_of_files_modules.html, 包括acl, archive, copy, fetch, file, find, patch, replace, stat, synchronize, unarchive等等,比较常用的有copy, fetch, synchronize这些。

拷贝一个目录或文件到目标主机,脚本如下:

---

# synchronize

 - hosts: axtestubuntu
   tasks:
     - name: copy local folder to remote host
       copy: src=/home/axing/ansible/axtest/ dest=/home/axing/axtest/

运行一下,可以看到,.69的主机先没有axtest目录,运行剧本后就多了这个目录:


axing@ax:~/ansible$ ansible axtestubuntu -m command -a "ls /home/axing/"
xx.xxx.xxx.69 | CHANGED | rc=0 >>
sources.list

axing@ax:~/ansible$ ansible-playbook synch.yml

PLAY [axtestubuntu] **********************************************************************************

TASK [Gathering Facts] *******************************************************************************
ok: [xx.xxx.xxx.69]

TASK [copy local folder to remote host] **************************************************************
changed: [xx.xxx.xxx.69]

PLAY RECAP *******************************************************************************************
xx.xxx.xxx.69              : ok=2    changed=1    unreachable=0    failed=0

axing@ax:~/ansible$ ansible axtestubuntu -m command -a "ls /home/axing/"
xx.xxx.xxx.69 | CHANGED | rc=0 >>
axtest
sources.list

现在从.69的主机取得source.list文件,同步到.99的主机上,脚本如下:

---
 - hosts: axtestubuntu
   tasks:
     - name: Fetch the file from the server69
       run_once: yes
       fetch: src=/home/axing/sources.list dest=/home/axing/ansible/axtest/ flat=yes

 - hosts: axtestubuntu
   tasks:
     - name: Copy the file from master to axtest99
       copy: src=/home/axing/ansible/axtest/sources.list dest=/home/axing/

运行之,可以看到:

axing@ax:~/ansible$ ansible-playbook syn2.yml

PLAY [axtestubuntu] **********************************************************************************

TASK [Gathering Facts] *******************************************************************************
ok: [xx.xxx.xxx.69]

TASK [Fetch the file from the server69] **************************************************************
changed: [xx.xxx.xxx.69]

PLAY [axtest99] **************************************************************************************

TASK [Gathering Facts] *******************************************************************************
ok: [xx.xxx.xxx.99]

TASK [Copy the file from master to axtest99] *********************************************************
changed: [xx.xxx.xxx.99]

PLAY RECAP *******************************************************************************************
xx.xxx.xxx.69              : ok=2    changed=1    unreachable=0    failed=0
xx.xxx.xxx.99              : ok=2    changed=1    unreachable=0    failed=0

axing@ax:~/ansible$ ansible axtest99 -m command -a "ls /home/axing/"
xx.xxx.xxx.99 | CHANGED | rc=0 >>
sources.list

也可以直接使用命令行:
1、从目标主机拷贝目录到本机(会连目录一起拷过来,就是会生成目录:/tmp/xx.xxx.xxx.69/home/axing),如果不想创建目录结构的话加参数flat=yes

ansible axtestubuntu -m fetch -a "src=/home/axing/sources.list dest=/tmp/ mode=0600 owner=axing group=axing flat=yes"

2、从本地拷贝目录到目标主机

ansible axtest99 -m copy -a "src=/home/axing/ansible/axtest/sources.list dest=/tmp/ mode=0600 owner=axing group=axing "

3、目标主机文件移动(remote_src=true参数)

ansible axtestubuntu -m copy -a "remote_src=true src=/home/axing/sources.list dest=/tmp/ mode=0600 owner=axing group=axing "

使用 synchronize模块,ansible和目标主机必须都安装运行rsync软件包,剧本如下, 其中mode的push和pull分别代表上传和拷贝

---
# synchronize 
- name: sync local folder to remote host
  hosts: axtestubuntu
  tasks:
    - name: push local folder to remote host
      synchronize:
        mode: push
        src: /home/axing/ansible/axtest/
        dest: /home/axing/axtest/

写成命令行是:

ansible antest99 -m synchronize -a 'src=sources.list dest=/tmp/'

你可能感兴趣的:(2019-04-13 Ansible 文件拷贝/同步playbook脚本)