1

I am trying to copy a few files from a git repository to a remote server. since the changes will be made often to these files, the files are placed in git, and from git, I am trying to copy it to the remote servers.

I am trying something like this, but the git repo I am accessing is a protected one and I have the creds. I would like to know how do I pass on the username and password here. How do I achieve it?

    - name: Clone git to a local server
        git:
          repo: https://github.com/blah/blah.git
          dest: /my/laptop/var/tmp/

Once I have those files in the controller server, from there I can copy files to all the remote hosts with the copy module.

kornel
  • 49
  • 6
Stack Learner
  • 99
  • 2
  • 10
  • 2
    I think your question about username and password has been replied [here](https://stackoverflow.com/questions/37841914/how-do-i-pass-username-and-password-while-using-ansible-git-module) – rcastellcastell Aug 14 '20 at 09:49
  • 2
    Yo do not have to copy from controller into targets. You should run the playbook using the target machines from your inventory and then the task will run on all the machines registered in your inventory. – rcastellcastell Aug 14 '20 at 09:50
  • Hi, but how do i pass the username and password – Stack Learner Aug 14 '20 at 12:47
  • 1
    Putting a password in a script or playbook is generally a bad idea due to security reasons. I would suggest, you add a ssh-key to your github account, clone/pull using ssh (not https) and use ssh agent forwarding on the machine you are running the playbook on. – toydarian Aug 14 '20 at 12:55
  • Hi @toydarian, okay. Does git should be installed in the server ? because i dont have the git installed in the destiantion servers where i am running the playbooks. – Stack Learner Aug 14 '20 at 13:35
  • `git` needs to be installed on the server, where you want to clone/pull the repository. – toydarian Aug 14 '20 at 13:48
  • @toydarian, is there a way to copy the files without git ? using any of the ansible modules ? – Stack Learner Aug 14 '20 at 14:02
  • 1
    You could download them from github using the [get_url](https://docs.ansible.com/ansible/latest/modules/get_url_module.html) module. – toydarian Aug 14 '20 at 14:04
  • you could do this `repo: https://username:password@github.com/blah/blah.git` – JBone Aug 14 '20 at 14:33
  • @Jbobe, now how do i store these username and password in vault and use it in my playbook – Stack Learner Aug 14 '20 at 17:53
  • please look `ansible-vault` examples. create a file in vault, that pops up a window, so you can put your username: user1. password: mypassword. for example. so Ansible decodes those values and applies here before calling the gitrepo. But the catch here is to decode those values, you need to provide a password that you created a file in `ansible-vault`. you can put this password in a file, and have that file information in your ansible.cfg file `vault_password_file = path/to/your/vaultpwd` – JBone Aug 14 '20 at 21:33

1 Answers1

0

One easy (but not secure) possibility is:

git:
      repo: https://username:password@github.com/blah/blah.git
      dest: /my/laptop/var/tmp/
wumpz
  • 8,257
  • 3
  • 30
  • 25