cancel
Showing results for 
Search instead for 
Did you mean: 
tambakhk
Mission Specialist
Mission Specialist
  • 10K Views

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
    ansible.builtin.git:
      repo: "{{ repo_url }}"
      dest: "{{ tmp_repo_directory }}"
      clone: yes
      update: yes
  - name: copy folders
    copy:
      dest: "{{ tmp_repo_directory }}"
      src: "{{ item }}"
      force: true
    loop:
      - "{{ 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
    args:
      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. 

7 Replies
bonnevil
Starfighter Starfighter
Starfighter
  • 9,988 Views

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.

tambakhk
Mission Specialist
Mission Specialist
  • 9,970 Views

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.

  • 9,446 Views

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:

https://github.com/willtome/ansible-git/blob/master/tasks/push.yml#L43

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:

https://developer.hashicorp.com/terraform/tutorials/automation/github-actions#review-and-merge-pull-...

 

DOY
Cadet
Cadet
  • 5,980 Views

I also need git push functionality as a common repository. 

I am using AWX-operator and need a repository to store fetched files. 

I think git is a simple repository. 

 

DOY
Cadet
Cadet
  • 5,979 Views

Did you find a solution? 

I am also looking for a similar approach. 

Chetan_Tiwary_
Moderator
Moderator
  • 5,961 Views

Refer ( not sure if this is all helpful )  : https://github.com/lvrfrc87/git-acp-ansible 

  • 3,742 Views

There was a attempt to add these features to the community.general ansible collection, but sadly it was not finished.
Here is the initial PR: https://github.com/ansible-collections/community.general/pull/57
Which was then closed in favor of this: https://github.com/ansible-collections/community.general/pull/168

But as you can read from that thread the author of those PR's currently maintains this module: https://github.com/lvrfrc87/git-acp-ansible

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