Fetching file from remote server to github repository using Ansible script

Fetching file from remote server to github repository using Ansible script

Server A → remote server like httpd
Server B → gihub repository

By the (limited) amount of information it looks like you want to
commit something.
The git module (https://docs.ansible.com/ansible/latest/modules/git_module.html)
is targeted toward checking out repos, not committing to them.
So I assume you'd have to structure it something like this:

1. use the git module to checkout your repo
2. use the get_url module to retrieve your file and save it in your repo
3. use the command or shell module to add the file and commit

Hi Dick,

Thanks for your reply.

Actually I want to copy the httpd.conf file from httpd server and store into Github repository. When we change something into httpd.conf file then automatically this file will save into github repository (abc/backup folder).

I am facing the issue to push the file from httpd server to github repo. I got the example from internet to checkout / download files from github repo to local machine , but didn’t find example to copy the file from remote server into github repository directly.

Please assist me.

With best regards,
Pradeep Kumar

Hi Dick,

Thanks for your reply.

Actually I want to copy the httpd.conf file from httpd server and store into
Github repository. When we change something into httpd.conf file then
automatically this file will save into github repository (abc/backup
folder).

It seems you've somehow reversed the workflow.
The idea is that git is used to keep track of config file changes.
And form there you configure your web server.
So the git repository is "leading" - not the web server.

I am facing the issue to push the file from httpd server to github repo. I
got the example from internet to checkout / download files from github repo
to local machine , but didn't find example to copy the file from remote
server into github repository directly.

That is probably because no one else has such a workflow.
This is consistent with the observation above.

If you still want to "use" git this way, then the 3 step pseudo
playbook from my previous message would work (although I don't
recommend doing it).

There is no such thing as "copy the httpd.conf file from httpd server
and store into Github repository".
You'd have to clone the repo on the web server, commit the files, and
then push it.

When I find myself having to jump through many hoops do something
seemingly simple, it's usually an indication that I'm looking at the
wrong solution...

Regards

Dick

Dick

Pradeep,
It seem to me that you’ve got a horse / cart inversion problem. Ansible can make backup copies of the files it modifies, but backing up configuration to durable storage (off of the system being managed) is the job of something else. If you want a rapid / atomic way to revert changes use something like etckeeper (which can then be used to push a copy of what it stores elsewhere, but read the fine print very closely first).

Aside from that very minor addition, Dick’s advice is sound.

Hi,

Thanks for your email.

  1. I installed the git in my ansible server.

  2. Then I use template module and downloaded the httpd.conf file with backup from httpd server into my ansible server.

  • template:
    src: /etc/httpd/conf/httpd.conf
    dest: /home/test/
    backup: yes
    owner: test
  1. I can check out the file from github repo to my ansible Linux server machine but I am unable to commit the file from my ansible Linux server machine to github repository (www.github.com/PD/testrepo/backup) folder.
    can we use “mode: push” in yml file.

Normally we use the following steps to push a file into github repo from local machine.
a. git pull b. git add . c. git commit -am “message” d. git push origin master . how to follow these steps in ansible yml file.

I don’t want to retrieve the file from github repo to local machine, I want to push the httpd.conf file from remote server to github repository backup folder.

Regards,
Pradeep

gentle reminder …

Hi

Hi,

Thanks for your email.

1. I installed the git in my ansible server.

2. Then I use template module and downloaded the httpd.conf file with backup from httpd server into my ansible server.
- template:
      src: /etc/httpd/conf/httpd.conf
      dest: /home/test/
      backup: yes
      owner: test

This is also the opposite of what 'template' is meant for.
And why do you need to backup if you're going to use git for "versioning"?

3. I can check out the file from github repo to my ansible Linux server machine but I am unable to commit the file from my ansible Linux server machine to github repository (www.github.com/PD/testrepo/backup) folder.
can we use "mode: push" in yml file.

It seems you're mixing up ansible pull/push mode with git pull/push?

Normally we use the following steps to push a file into github repo from local machine.
a. git pull b. git add . c. git commit -am "message" d. git push origin master . how to follow these steps in ansible yml file.

- name: check out a git repository
    local_action:
      module: git
      repo: https://github.com/PD/testrepo.git
      dest: /home/test/git
      update: yes
      version: master

  I don't want to retrieve the file from github repo to local machine, I want to push the httpd.conf file from remote server to github repository backup folder.

As explained twice before, your workflow isn't particularly useful.
I don't think it serves any purpose helping someone to force a round
peg in a square hole.
You are best off by changing your workflow to something more standard
and supportable.

Regards

Dick