首页 > 解决方案 > 仅在 apt 任务第一次安装软件包时复制文件

问题描述

我想知道如何最好地在 Ansible 中完成这个(希望如此)简单的任务:

在 playbook 中,当第一次安装 dnsmasq apt 包时,将 /etc/dnsmasq.conf 备份到 /etc/dnsmasq.conf.orig,然后再继续执行任何其他任务。

类似于下面的代码?

- name: 'Ensure dnsmasq package is installed'
  apt:
    name: dnsmasq
    state: present
  notify: Backup original /etc/dnsmasq.conf file

- name: Backup original /etc/dnsmasq.conf file
  copy:
    remote_src: yes
    src: /etc/dnsmasq.conf
    dest: /etc/dnsmasq.conf.orig
  when: ONLY IF WE JUST INSTALLED THE PACKAGE ABOVE

标签: ansible

解决方案


force: no复制模块中的选项将仅在文件不存在时复制文件(即您从未安装和复制原始文件)。– Zeitounator 昨天

使用“力”就可以了,而且是最简单的方法,谢谢大家!

(将其创建为答案,因为我无法将评论标记为答案)。


推荐阅读