AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1048430
Accepted
Petr
Petr
Asked: 2021-01-04 13:11:44 +0800 CST2021-01-04 13:11:44 +0800 CST 2021-01-04 13:11:44 +0800 CST

如何管理文件的修改副本?

  • 772

按照这个答案,我想复制 OpenSSL 的配置,并进行一组特定的更改。原始文件不在我的控制范围内,所以我无法将其设为模板。

目前我有:

  - name: Make a copy
    copy:
      src: original.cnf
      dest: copy.cnf
      force: no
  - name: Modify
    ini_file:
      path: copy.cnf
      section: ...
      option: ...
      value: ...

此更改序列是幂等的,但如果原始文件发生更改,则更改不会传播到副本。如果我将其更改为force: yes,则将传播原始更改,但每次运行剧本时都会执行更改。这是有问题的,因为我需要在发生变化的情况下重新启动依赖服务,但显然这不能每次都发生。

有没有办法以这样一种方式维护副本,即当且仅在需要时修改目标文件?

files copy ansible ansible-playbook
  • 2 2 个回答
  • 259 Views

2 个回答

  • Voted
  1. John Mahowald
    2021-01-04T16:13:14+08:002021-01-04T16:13:14+08:00
    - block:
      name: These two are changed every time as modifications are not in original.cnf
      - name: Make a temporary copy
        copy:
          src: original.cnf
          dest: temp.cnf
          force: yes
      - name: Modify temporary copy
        ini_file:
          path: temp.cnf
          section: ...
          option: ...
          value: ...
    
    - block:
      name: With same original.cnf and same modifications, the result will be already installed
      - name: Idempotent copy into place
        register: openssl_config_install
        copy:
          src: temp.cnf
          dest: copy.cnf
          force: yes
    
    
    - assert:
        that:
          - openssl_config_install is not changed
    
    • 2
  2. Best Answer
    Petr
    2021-01-06T13:22:29+08:002021-01-06T13:22:29+08:00

    根据约翰的回答,我最终得到了以下剧本片段。重要的部分是changed_when: False,它确保只有修改目标配置文件副本的步骤才算作更改。

    - name: Create OpenSSL config copy
      block:
      - name: Create temporary file for the config's copy
        tempfile:
        register: tempfile
        changed_when: False
      - name: Copy openssl.cnf to the temporary file
        copy:
          src: "{{ openssl_cnf_source }}"
          dest: "{{ tempfile.path }}"
          mode: 0644  # Without this the next `copy` task can have issues reading the file.
        changed_when: False
      - name: Modify openssl.cnf in the temporary file
        ini_file:
          path: "{{ tempfile.path }}"
          section: ...
          option: ...
          value: ...
        changed_when: False
      - name: Copy the temporary file to the target OpenSSL config
        copy:
          src: "{{ tempfile.path }}"
          dest: "{{ openssl_cnf_copy }}"
          mode: 0644
          owner: ...
        notify:
          - ...
      - name: Delete the temporary file
        file:
          path: "{{ tempfile.path }}"
          state: absent
        changed_when: False
    
    • 0

相关问题

  • 在本地复制大型目录树?cp还是rsync?

  • > 4GB文件的rsync

  • 操作系统文件

  • 比较(diff)完整目录结构的最佳方法?

  • 将 .mbx 文件转换为 .pst 文件

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve