tambakhk
Cadet
Cadet
  • 165 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. 

0 Kudos
2 Replies
bonnevil
Starfighter Starfighter
Starfighter
  • 140 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.

0 Kudos
tambakhk
Cadet
Cadet
  • 122 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.

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