GIT add and push via ansible

I am trying to use GIT as the single source of truth for all artifacts - inputs, inventory and output generated by ansible runs. 

Ansible git module does not have add, commit and push options, so I am currently using below method to achieve this: 



  - name: Clone GIT Repository
      repo: "{{ repo_url }}"
      dest: "{{ tmp_repo_directory }}"
      clone: yes
      update: yes
  - name: copy folders
      dest: "{{ tmp_repo_directory }}"
      src: "{{ item }}"
      force: true
      - "{{ playbook_dir }}/outputs"
      - "{{ playbook_dir }}/inventory"
  - name: "Add output directory to git"
    ansible.builtin.shell: |
      git config --global user.name "rhaap"
      git config --global user.email "rhaap@rhaap.com"
      git add -A && git commit -m "RHAAP Job ID {{ tower_job_id }}: Automated Commit by Ansible  " && git push
      chdir: "{{ tmp_repo_directory }}"
    ignore_errors: true
    register: output
    failed_when: >
      output.msg != "" and
      ("error" in output.msg or
      "conflict" in output.msg or
      "Errno" in output.msg or
      "fatal" in output.msg or
      (output.stdout != "" and
      "nothing to commit, working tree clean" not in output.stdout) or
      (output.stderr != ""))




Is there a plan to add these options to the git module ? 

Also, is there a better way to handle these operations. 

PS- we don't expect to see parallel commits to GIT repo to see conflicts. 

0 Kudos
3 Replies
Starfighter Starfighter

My guess is that you've already touched on why the add/commit/push options aren't supported by the git (ansible.builtin.git) module: if there's a conflict, normally we assume that resolving it is an operation that requires human intervention.  Dealing with commit conflicts is not something most folks automate.  However, cloning/pulling a git repository is something that is much more common for an automated process to do.

That having been said, I don't have any special insight into what plans that upstream Ansible has for the ansible.builtin.git module.  That Ansible Content Collection is included with Ansible Core, so its community upstream is https://github.com/ansible/ansible (and you can see the Python code for those modules in lib/ansible/modules in the source tree).  If you're a Red Hat customer, you could also follow up through your support contacts.

0 Kudos

I understand that conflicts will be difficult to handle via automation. But, having these options provides option to user to use it in certain scenarios where conflicts can be avoided. Also, user can use a separate branch(that has only read access to other users) to push the artifacts. 

I will try to follow it up with support contacts.

0 Kudos

Hmmm... not being able to do a git push from a native Ansible module seems like a massive gap!  Are things so restricted that you do not have access to Shell module?  This can be acheived quite easily as illustrated here:


As an aside, it sounds like you are trying to manage state which TBH is something Ansible still struggles to get right... if only we could all be like one of the cool kids & work over at HashiCorp:



0 Kudos
Join the discussion
You must log in to join this conversation.