# How to use file contents from Git?

**URL:** <https://forum.ansible.com/t/how-to-use-file-contents-from-git/4215>\
**Category:** Get Help\
**Tags:** awx\
**Created:** [March 5, 2024, 9:53am UTC](https://forum.ansible.com/t/how-to-use-file-contents-from-git/4215 "2024-03-05T09:53:24Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [March 5, 2024, 9:53am UTC](https://forum.ansible.com/t/how-to-use-file-contents-from-git/4215/1 "2024-03-05T09:53:25Z")

</div>

Hello!  
I want to use the content of a config file at GitHub to replace a config file at a host.  
When i dowload the file its content rather looks like a siterip of the used link but not the actual content.

```auto
---
- name: testconfigfile_dl
  hosts: all
  become: yes 

  tasks:
    - name: dl conf
      get_url:
        url: https://github.com/gsg-git/awx_pub/blob/main/linux_playbooks/fusion_inventory/agent.cfg
        dest: "/tmp/agent.cfg"
        mode: '0644'

```

the content of the agent.cfg is like this:

 ![grafik](https://us1.discourse-cdn.com/ansible/original/2X/a/a8650a635dd93a26ab506522bdd438a76eaac43e.png)  
Thank you for your input again!

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [March 5, 2024, 10:01am UTC](https://forum.ansible.com/t/how-to-use-file-contents-from-git/4215/2 "2024-03-05T10:01:27Z")

</div>

Got it again after around 4-5 times aking chatgpt:  
instead of using get\_url:

```auto
ansible.builtin.get_url:

```

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [March 5, 2024, 1:12pm UTC](https://forum.ansible.com/t/how-to-use-file-contents-from-git/4215/3 "2024-03-05T13:12:38Z")

</div>

Please don’t post chat gpt answers if you insist on using it yourself. This is a bad solution because it’s unrelated to the issue.

If you’re using `ansible-core` out of the box (no custom legacy content), and not using the `collections` keyword in the playbook/none of collections contains a module called `get_url`, then `get_url` is the exact same as `ansible.builtin.get_url`.

Try downloading the raw file instead of the web page:

```auto
   - get_url:
       url: https://raw.githubusercontent.com/gsg-git/awx_pub/main/linux_playbooks/fusion_inventory/agent.cfg
       dest: /tmp/agent.cfg
       mode: '0644'

```

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [March 5, 2024, 2:23pm UTC](https://forum.ansible.com/t/how-to-use-file-contents-from-git/4215/4 "2024-03-05T14:23:58Z")

</div>

But weir is when i use:

```auto
---
- name: testdl
  hosts: all
  become: yes

  tasks:
    - name: dlfile
      ansible.builtin.get_url:
        url: "https://raw.githubusercontent.com/gsg-git/awx_pub/main/linux_playbooks/fusion_inventory/agent.cfg"
        dest: "/tmp/agent.cfg"
        owner: root
        group: root
        mode: '0644'

```

The contenst of the file is correct, but when i use:

```auto
---
- name: testconfigfile_dl
  hosts: all
  become: yes 

  tasks:
    - name: dl conf
      get_url:
        url: https://github.com/gsg-git/awx_pub/blob/main/linux_playbooks/fusion_inventory/agent.cfg
        dest: "/tmp/agent.cfg"
        mode: '0644'

```

not.  
how could the added permissions lead to copy the whoe Git like an htl homepage code, but nothing of the content of the file?

---

<div class="post-metadata">

**Author:** ![Dustin](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dustin/32/2573_2.png) [@Dustin](https://forum.ansible.com/u/Dustin)\
**Post date:** [March 5, 2024, 3:07pm UTC](https://forum.ansible.com/t/how-to-use-file-contents-from-git/4215/5 "2024-03-05T15:07:46Z")

</div>

The actual difference in the playbooks that is helping you is the URL that you are pulling from.

When you pull from [https://github.com](https://github.com)… you pull the website you would see in your browser.

When you pull from [https://raw.github.com](https://raw.github.com) you pull just the target file you are interested in.

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [March 6, 2024, 8:21am UTC](https://forum.ansible.com/t/how-to-use-file-contents-from-git/4215/6 "2024-03-06T08:21:15Z")

</div>

Thats even better THANK YOU AGAIN!  
Is there something comparable when using GitLab?  
We have an on-prem gitlab for our devs and my test-data will be migrated there.  
And i see now that i got the raw. in my last code but not in the first so thats the reason in the beginning.  
Even chatgpt stated it in the explanation…  
Do you too have those moments when you start your day and see the mistake you got stuck the day before and think: why didnt i got it yesterday - it seems so obious.  
😆

---

<div class="post-metadata">

**Author:** ![Dustin](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dustin/32/2573_2.png) [@Dustin](https://forum.ansible.com/u/Dustin)\
**Post date:** [March 6, 2024, 2:19pm UTC](https://forum.ansible.com/t/how-to-use-file-contents-from-git/4215/7 "2024-03-06T14:19:18Z")

</div>

A link to a GitLab file when opened in the browser looks like this:  
`https://my.gitlab.com/team/group/project/-/blob/branch/file.txt?ref_type=heads`

If you want the raw version of the file, it would look like this:  
`https://my.gitlab.com/team/group/project/-/raw/branch/file.txt?ref_type=heads&inline=false`

The changes are:

- Replace `/blob/` with `raw` in the URL
- Add `&inline=yes` to the end of the URL

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [March 7, 2024, 8:03am UTC](https://forum.ansible.com/t/how-to-use-file-contents-from-git/4215/8 "2024-03-07T08:03:02Z")

</div>

Very nice Thank you!

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [March 8, 2024, 9:11am UTC](https://forum.ansible.com/t/how-to-use-file-contents-from-git/4215/9 "2024-03-08T09:11:55Z")

</div>

Im now at the Gitlab and have to use a litle diffrent playbook, because the GitLab Git isnt public.  
Can i use the same credentials i used for the project?

I the bot suggested something like this:

```auto

  tasks:
    - name: clone from repo to tmp
      ansible.builtin.git:
        repo: 'https://git.gruen.net/awx_gruen/playbooks.git'
        dest: '/tmp/repo'
        version: 'dev'
        force: yes
        accept_hostkey: yes
      environment:
        GIT_ASKPASS: "/bin/echo"
        GIT_USERNAME: "{{ lookup('env','GITLAB_USER') }}"
        GIT_PASSWORD: "{{ lookup('env','GITLAB_PASSWORD') }}"

```

But i dont know here the eviroment variables are even possible like this and how i add it correct.

I Added a new credential type:

 ![grafik](https://us1.discourse-cdn.com/ansible/original/2X/a/a9c1e1327904708ec619c8cb6cfad5beb41f211d.png)

```auto
fields:
  - id: gitlab_user
    type: string
    label: GitLab User
  - id: gitlab_password
    type: string
    label: GitLab Password
    secret: true
required:
  - gitlab_user
  - gitlab_password

```

and added the user i created for Gitlab in AD and its pw and still get a bad credetial error:  
`remote: HTTP Basic: Access denied. The provided password or token is incorrect or your account has 2FA enabled and you must use a personal access token instead of a password.`

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [March 11, 2024, 8:25am UTC](https://forum.ansible.com/t/how-to-use-file-contents-from-git/4215/10 "2024-03-11T08:25:54Z")

</div>

I know now that the GitLap currentls doesnt support auth via http

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [March 14, 2024, 11:16am UTC](https://forum.ansible.com/t/how-to-use-file-contents-from-git/4215/11 "2024-03-14T11:16:25Z")

</div>

Hello!  
I tried today to use your hint with the Url of the gitlab, but i got like in the beginning with github the content of the website, not the specific file:

 ![grafik](https://us1.discourse-cdn.com/ansible/original/2X/5/50f83e1019be4f794e95359d6f41162991839717.png)  
The Playbook:

```auto
---
- name: Download specific file from GitLab with authentication
  hosts: all
  become: true

  vars:
    file_url: "https://git.gruen.net/awx_gruen/playbooks/-/raw/dev/linux_playbooks/fusion_inventory/agent.cfg"

  tasks:
    - name: Download the specific file with HTTP Basic Auth
      ansible.builtin.uri:
        url: "{{ file_url }}"
        dest: "/tmp/agent.cfg"
        method: GET
        validate_certs: yes
        url_username: "{{ GITLAB_USER }}"
        url_password: "{{ GITLAB_TOKEN }}"
        force_basic_auth: yes

```

when i add the ?ref\_type=heads&inline=false nothing gets downloaded

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 13, 2024, 11:16am UTC](https://forum.ansible.com/t/how-to-use-file-contents-from-git/4215/12 "2024-04-13T11:16:44Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
