如何使用ansible备份文件和文件夹?

时间:2016-10-13 07:13:21

标签: ansible ansible-playbook ansible-2.x

我有一个文件和子文件夹的文件夹,其中包含一些需要在远程计算机中部署的模板文件,但在部署这些文件之前和文件夹我需要在控制系统中备份这些文件夹(ansible正在运行)。需要部署的文件数量不固定,可能会更改。是否有可能在ansible中做到这一点?

下面是我的ansible配置和全局变量文件,它一直工作到提取步骤。

---
#Global vars
aws_access_key: xxx
aws_secret_key: xxx
aws_bucket: s3-artifacts
aws_bucket_download: /deployment/trial/deployment.tar.gz
local_download: deployment.tar.gz
current_dir: /root/tools/deployment/trial/
local_extract_dir: extracted

deployment_usr: root
deployment_grp: root
mode: 775
...




---
- hosts: web
  tasks:
   - name: Download from S3
     s3: aws_access_key="{{ aws_access_key }}" aws_secret_key="{{ aws_secret_key }}" bucket="{{ aws_bucket }}" object="{{ aws_bucket_download }}" dest="{{ current_dir }}/{{ local_download }}" mode=get overwrite=true
   #Change permissions
   - name: Change permissions
     file: path="{{ current_dir }}/{{ local_download }}" owner="{{ deployment_usr }}" group="{{ deployment_grp }}" mode="{{ mode }}"
   #Extract
   - name: mkdir local_extract_dir
     file: path="{{ current_dir }}/{{ local_extract_dir }}" state=directory mode="{{ mode }}"
   - name: Extracting deployment folder 
     unarchive: src="{{ current_dir }}/{{ local_download }}" dest="{{ current_dir }}/{{ local_extract_dir }}"
   - name: Give permissions
     file: path="{{ current_dir }}/{{ local_extract_dir }}" owner="{{ deployment_usr }}" group="{{ deployment_grp }}" mode="{{ mode }}"

   #Backup
   #- name: Synchronize
     #synchronize: src="{{ current_dir }}/{{ local_extract_dir }}"

...

我还尝试运行(cd extracted; find .) | cut -c 3-命令,该命令提供了解压缩文件和文件夹的列表,但需要帮助从远程计算机备份这些文件。

2 个答案:

答案 0 :(得分:1)

如果我正确理解了您的问题,则说明您正在寻找copy模块:

- name: Copy some directory
  copy:
    src: "{{ current_dir }}/{{ local_extract_dir }}/"
    dest: "{{ backup_dir }}/"
  delegate_to: localhost

如果您更喜欢基于rsync的解决方案,还可以看看synchronize module

答案 1 :(得分:0)

退房

https://docs.ansible.com/ansible/latest/modules/fetch_module.html


- name: Specifying a path directly
  fetch:
    src: /tmp/somefile
    dest: /tmp/prefix-{{ inventory_hostname }}
    flat: yes