How to use file contents from Git?

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.

---
- 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:


Thank you for your input again!

1 Like

Got it again after around 4-5 times aking chatgpt:
instead of using get_url:

ansible.builtin.get_url:

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:

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

But weir is when i use:

---
- 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:

---
- 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?

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… you pull the website you would see in your browser.

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

2 Likes

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.
:laughing:

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
1 Like

Very nice Thank you!

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:


  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:

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.

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

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
The Playbook:

---
- 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

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