如何在 Ansible 剧本中创建一个本地事实,以跳过之前运行中已经完成的步骤?

如何在 Ansible 剧本中创建一个本地事实,以跳过之前运行中已经完成的步骤?

我编写了一个剧本,从 git 三个 repo 克隆并从源代码编译它们。

每当我配置使用该剧本的机器时,无论存储库的本地副本是否已更新,都会克隆存储库。

我希望 git clone 仅在第一次发生,而在运行剧本的其余时间,我希望 Ansible 跳过这一步。

我已经在 Ansible 文档中阅读了有关“本地事实”的内容,但我很难理解应该如何实现它。

这是我写的剧本:

---
  - name: Install required packages
    apt:  name={{item}} state=installed
    with_items:
         - "librdkafka-dev"
         - "libyajl-dev"
         - "librdkafka1"
         - "cmake"
    sudo: yes
    tags: kafkacat

  - name: Git clone kafkacat
    git:  repo=git://github.com/company/kafkacat.git
          dest={{ kafkacat_installdir }} accept_hostkey=yes force=yes
    tags: kafkacat

  - name: Git clone librdkafka
    git:  repo=git://github.com/company/librdkafka.git
          dest={{ kafkacat_installdir }}/librdkafka force=yes version={{ librdkafka_git_version }}
    tags: kafkacat

  - name: Git clone yajl
    git:  repo=git://github.com/company/yajl
          dest={{ kafkacat_installdir }}/yajl force=yes version={{ yajl_git_version }}
    tags: kafkacat

  - name: librdkafka compilation (configure)
    command: chdir={{ kafkacat_installdir }}/librdkafka {{ kafkacat_installdir }}/librdkafka/configure 
    tags: kafkacat

  - name: librdkafka compilation (make)
    command: chdir={{ kafkacat_installdir }}/librdkafka make
    tags: kafkacat

  - name: librdkafka compilation (make install)
    command: chdir={{ kafkacat_installdir }}/librdkafka make DESTDIR={{ kafkacat_installdir }}/tmp-bootstrap install
    tags: kafkacat

  - name: yajl compilation (configure)
    command: chdir={{ kafkacat_installdir }}/yajl {{ kafkacat_installdir }}/yajl/configure 
    tags: kafkacat

  - name: yajl compilation (make)
    command: chdir={{ kafkacat_installdir }}/yajl make
    tags: kafkacat

  - name: yajl compilation (make install)
    command: chdir={{ kafkacat_installdir }}/yajl make DESTDIR={{ kafkacat_installdir }}/tmp-bootstrap install
    tags: kafkacat

  - name: Set vagrant user & group as the owner of the folder
    file: path={{ kafkacat_installdir }} owner={{ kafkacat_owner }} group={{ kafkacat_group }} state=directory recurse=yes
    sudo: yes
    tags: kafkacat

  - name: kafkacat compilation (configure)
    command: chdir={{ kafkacat_installdir }} {{ kafkacat_installdir }}/configure --enable-json --enable-static
    environment: env
    tags: kafkacat
    #- debug: var=env

  - name: kafkacat compilation (make)
    command: chdir={{ kafkacat_installdir }} make
    environment: env
    tags: kafkacat
    #- debug: var=env

  - name: kafkacat compilation (make install)
    command: chdir={{ kafkacat_installdir }} make install
    sudo: yes
    tags: kafkacat

非常感谢您的帮助。

答案1

您可以使用统计模块

你可能想做这样的事情:

- name: Check if repository is checked out.
  stat: path={{ kafkacat_installdir }}/kafkacat
  register: git_dir

- name: Git clone kafkacat
  git:  repo=git://github.com/company/kafkacat.git
        dest={{ kafkacat_installdir }} accept_hostkey=yes force=yes
  tags: kafkacat
  when: git_dir.stat is defined and git_dir.stat.isdir

相关内容