0

I wrote a playbook which clones from git three repo's and compiling them from source.

Whenever I provision a machine which uses this playbook, the repos are cloned, no matter if the local copy of the repo is already updated.

I'd like the git clone to happen only the first time and in the rest of the times when I run the playbook, I want Ansible to skip this step.

I've read about "local facts" on the Ansible docs but I'm having a hard time to understand how it should be implemented.

This is the playbook I wrote:

---
  - 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

Your help is much appreciated.

Itai Ganot
  • 10,644
  • 29
  • 93
  • 146
  • if you want idempotence, why are you using the `force` param? `force=no state=no` should do it, and there are similar idempotentcy tricks for the next commands too. – tedder42 Apr 26 '16 at 16:19

1 Answers1

1

You can create custom checks and skips with the stat module

You might want to do something like this:

- 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
Henrik Pingel
  • 9,380
  • 2
  • 28
  • 39
  • Thanks for your answer. I'd like to set it on most of the tasks in the playbook and not just the git task... Does that require me to create stat tasks for each one of the tasks I intend to use it on? – Itai Ganot Apr 26 '16 at 15:45
  • that depends. The value of the `git_dir` variable will stay over the play. You might want to check the [blocks](http://docs.ansible.com/ansible/playbooks_blocks.html) feature. Another option would be to use an `include` statement and create a when clause for that. – Henrik Pingel May 02 '16 at 16:46