read contents from a file on git using ansible

31 views Asked by At

i have been using below task to read contents of a file "file.py" from github repo "GITHUB_XXXX" but i am unable to see the actual content of the file. what is the best way to read the contents of the file from github by using git token and ansible?. verified all settings on repo to grant access to read the file contents but still not working

---
- name: Read file from GitHub
  hosts: localhost
  gather_facts: false
  vars:
    github_user: "soonxxx"
    repository: "GITHUB_xxxxx"
    file_path: "file.py"
    github_token: "ghp_dXG"

  tasks:
    - name: Fetch file from GitHub
      uri:
        url: "https://raw.githubusercontent.com/soonXXXX/GITHUB_XXXXX/main/file.py"
        method: GET
        headers:
          Authorization: "token {{ github_token }}"
        return_content: true
        force_basic_auth: true
        status_code: 200
      register: file_contents

    - name: Store file content to variable
      set_fact:
        my_variable: "{{ file_contents.content | b64decode }}"

error:

{"accept_ranges": "bytes", "access_control_allow_origin": "*", "changed": false, "connection": "close", "content": "404: Not Found", "content_length": "14", "content_security_policy": "default-src 'none'; style-src 'unsafe-inline'; sandbox", "content_type": "text/plain; charset=utf-8", "cross_origin_resource_policy": "cross-origin", "date": "Thu, 28 Mar 2024 21:45:32 GMT", "elapsed": 0, "expires": "Thu, 28 Mar 2024 21:50:32 GMT", "msg": "Status code was 404 and not [200]: HTTP Error 404: Not Found", "redirected": false, "source_age": "0", "status": 404, "strict_transport_security": "max-age=31536000", "url": "https://raw.githubusercontent.com/soonxxxx/GITHUB_xxxxx/main/file.py", "vary": "Authorization,Accept-Encoding,Origin", "via": "1.1 varnish", "x_cache": "MISS", "x_cache_hits": "0", "x_content_type_options": "nosniff", "x_fastly_request_id": "13ad8711f515500b0be10b9b8c427f2f60384a42", "x_frame_options": "deny", "x_github_request_id": "9488:123E83:7DB4F:8DDBA:6605E4FC", "x_served_by": "cache-ewr18143-EWR", "x_timer": "S1711662332.486123,VS0,VE28", "x_xss_protection": "1; mode=block"}

expecting to see the actual contents(data) of file.py from gitrepo "GITHUB_XXXXX"
1

There are 1 answers

0
Sravan Nekkanti On

the issue seems to be the token, generated new token and it worked. just make sure the token has all the access to read the contents from the target repo