【问题标题】:difference in two file using ansible module使用ansible模块的两个文件的区别
【发布时间】:2018-12-17 16:52:41
【问题描述】:

我想看看本地和远程主机上的文件是否有任何更改。如果有任何差异,它应该在屏幕上可见 使用 Ansible 的最佳方法应该是什么

例如:

src : /tmp/abc.txt
dest : hostname:/tmp/cde.txt

【问题讨论】:

    标签: ansible


    【解决方案1】:

    您还可以使用check_mode: yesdiff: yes 任务选项来显示差异:

    ---
    - hosts: localhost
      gather_facts: no
    
      tasks:
        - name: "Only show diff between test1.txt & test2.txt" 
          copy:
            src: /tmp/test2.txt
            dest: /tmp/test1.txt
          check_mode: yes
          diff: yes
    

    例子:

    # cat /tmp/test1.txt
    test1
    
    # cat /tmp/test2.txt
    test1
    test2
    
    # ansible-playbook diff.yaml
    PLAY [localhost] ***********************************************************************************************************************************
    
    TASK [Only show diff between test1.txt & test2.txt] ************************************************************************************************
    --- before: /tmp/test1.txt
    +++ after: /tmp/test2.txt
    @@ -1 +1,2 @@
     test1
    +test2
    
    changed: [localhost]
    
    PLAY RECAP *****************************************************************************************************************************************
    localhost                  : ok=1    changed=1    unreachable=0    failed=0
    

    更多关于check_modediffhere的信息。

    【讨论】:

    • 谢谢.. 关键是其中一个文件存在于 localhost 和其他远程主机上
    • copy 从 ansible 机器复制文件到远程机器
    【解决方案2】:

    这听起来像是您可以使用debug 模块完成的任务。使用diff 获取两个文件的差异。注册输出并使用调试显示:

    - name: Generate diff
      command: diff  /tmp/abc.txt  /tmp/def.txt
      register: diff_result
    
    - name: Show diff result
      debug:
        var: diff_result
    

    【讨论】:

      【解决方案3】:

      从命令行,

      ansible <host-pattern> -m copy -CD -a "src=<your local file> dest=<remote file or location>"
      

      -m copy 选项使 Ansible 调用 copy 模块

      -C 选项使 Ansible 检查是否会发生更改,而不是执行复制

      -D 选项使 Ansible 报告如果要进行复制会发生哪些更改

      输出类似于 UNIX diff 命令产生的内容,只是它报告本地文件和远程副本之间的差异。

      【讨论】:

        【解决方案4】:
        ---
        - hosts: localhost
          gather_facts: yes
          become: yes
          become_method: sudo
          tasks:
           - name: Get ansible date/time facts
             setup:
               filter: "ansible_date_time"
               gather_subset: "!all"
        
           - name: Store DTG as fact
             set_fact:
               DTG: "{{ ansible_date_time.date }}"
        
           - name: Changes of the USCORE_Switch
             copy:
               src: /home/thilan_widearea_cloud/network-programmability/curentconfig/backups/{{hostvars.localhost.DTG}}/USNYCL3SW-{{hostvars.localhost.DTG}}-config.txt
               dest: /home/thilan_widearea_cloud/network-programmability/curentconfig/USBaseconfig.txt
             check_mode: yes
             diff: yes
             register: output
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-01-23
          • 1970-01-01
          • 1970-01-01
          • 2018-12-29
          • 2019-02-01
          • 2012-02-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多