ansible playbook 恢复备份文件

有的时候我们用copy模块备份的文件想要回滚

ansible -i pbs_hosts all -m copy -a "src=file/hosts dest=/etc/ backup=yes"

那我们就可以用下面这种方式进行回滚

cat restore.hosts.yml

--- #固定格式
#- hosts: "{
  { ansiblegroupnames }}" #需要操作的组或者服务器,可以使用-e传入
- hosts: "saas_hosts"
  gather_facts: false
  
  vars:
    file_name: /etc/hosts #需要恢复的远程文件位置,可以使用-e传入,当前位置为测试用文件
    ansiblegroupnames: 127.0.0.1
  
  tasks:
    - name: "查找指定文件:{
  { file_name }}"
      find:
        recurse: no
        paths:
          - "{
  { file_name | dirname }}"
          #- "/etc/"
        patterns:
          - '{
  { file_name | basename }}\..*~'  #这里必须使用单引号
        use_regex: true
      register: backup_file #注册成变量
   # - name:
   #   debug: 
   #     msg: "{
  { backup_file }}"
  
    - name: "查找最后一个备份文件按mtime时间排序"
      set_fact:
        latest_backup: "{
  { (backup_file.files | sort(attribute='mtime') | last).path }}"
  
    - name: "还原备份文件:{
  { file_name }}"
      copy:
        src: "{
  { latest_backup }}"
        remote_src: true
        dest: "{
  { file_name }}"

cat saas_hosts_all

[saas_hosts]
192.168.1.1

ansible-playbook -i saas_hosts_all  restore.hosts.yml -C

相关推荐

  1. ansible playbook 恢复备份文件

    2024-01-20 20:32:03       60 阅读
  2. 数据库备份恢复

    2024-01-20 20:32:03       50 阅读
  3. 数据备份恢复

    2024-01-20 20:32:03       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-20 20:32:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-20 20:32:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-20 20:32:03       82 阅读
  4. Python语言-面向对象

    2024-01-20 20:32:03       91 阅读

热门阅读

  1. ansible

    ansible

    2024-01-20 20:32:03      61 阅读
  2. cout << “if (customers > 0)

    2024-01-20 20:32:03       54 阅读
  3. Linux 修改文件名称

    2024-01-20 20:32:03       68 阅读
  4. SpringMVC 参数传递见解5

    2024-01-20 20:32:03       61 阅读
  5. 设计模式——备忘录模式

    2024-01-20 20:32:03       55 阅读
  6. 【算法详解】力扣169.多数元素

    2024-01-20 20:32:03       68 阅读
  7. c语言-位段

    2024-01-20 20:32:03       61 阅读