cancel
Showing results for 
Search instead for 
Did you mean: 
tambakhk
Mission Specialist
Mission Specialist
  • 16.5K 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. 

9 Replies
bonnevil
Starfighter Starfighter
Starfighter
  • 16.5K 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
  • 16.5K 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.

  • 16K 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
  • 12.5K 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
  • 12.5K Views

Did you find a solution? 

I am also looking for a similar approach. 

Chetan_Tiwary_
Community Manager
Community Manager
  • 12.5K Views

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

  • 10.3K 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

kvegh
Cadet
Cadet
  • 2,162 Views

Have you folks tried the ansible.scm collection? It comes with git_retrieve and git_publish modules. 

bonnevil
Starfighter Starfighter
Starfighter
  • 2,111 Views

Interesting!  Yeah, that collection didn't exist in January 2023 when the question was first asked.  (Well, it might have existed but it wasn't even version 0.1 yet!)  It looks like 1.0.6 was the first version added to automation hub and Galaxy, around April 2023.

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