如何在安装远程软件时使Ansible覆盖现有文件?

时间:2018-06-06 11:00:51

标签: ansible

我使用Ansible将存储库克隆到远程主机并在那里安装软件:

---
- hosts: ubuntu-16.04
  become: yes

  tasks:
     - name: Install aptitude
       apt:   
         name: aptitude
         state: latest
         update_cache: yes

     - name: Update apt cache and upgrade packages
       apt:
         update_cache: yes
         upgrade: full
         autoclean: yes
         autoremove: yes

     - name: Clone <name of repository> repository
       git:
         repo: <link to repository>
         dest: /home/test/Downloads/<new directory>
         clone: yes

     - name: Install <name of repository>
       command: chdir=/home/test/Downloads/<new directory> {{ item }}
       with_items:
         - make
         - make install
       become_user: root
       become_method: sudo

     - name: Remove <name of repository> directory
       file:
         path: /home/test/Downloads/<new directory>
         state: absent

如果系统上尚未安装该软件,则此工作正常。但是,如果它是,我再次运行剧本,那么它会停留在&#34;安装...&#34;任务,说它无法创建符号链接,因为文件已经存在。

第一个问题是在此之后的几个任务(此处未显示)由于其中的游戏而未执行。我可以通过简单地重新安排我的任务来解决这个问题,但我真的想知道,请:

  • 如何使Ansible覆盖现有文件?
  • 你是否会以这种方式解决这个问题,或者你会说这个行为很好,因为软件已经安装好了?但是,如果存储库在第二次克隆时包含更新的代码呢?

1 个答案:

答案 0 :(得分:2)

我不认为问题与Ansible有关,而与Makefile有关。不过,您可以在已安装软件时跳过安装步骤。

首先,您可以检查二进制文件是否已存在,并根据输出跳过安装:

name: Check if software exists
stat:
  path: <path to binary>
register: stat_result

name: Install <name of repository>
  command: chdir=/home/test/Downloads/<new directory> {{ item }}
  with_items:
    - make
    - make install
  become_user: root
  become_method: sudo
when: stat_result.stat.exists == False