How do I pass username and password while using Ansible Git module?

后端 未结 4 511
一个人的身影
一个人的身影 2020-12-13 02:14

While doing clone, push or pull of a private git repository hosted internally (e.g. on a GitLab instance) with Ansible\'s Git module, how do I specify username and password

4条回答
  •  难免孤独
    2020-12-13 02:28

    Improving on Arbab Nazar's answer, you can avoid exposing your password in the terminal by prompting for the credentials.

    playbook.yml

    --- 
    - name: ANSIBLE - Shop Installation 
      hosts: '{{ target }}' 
    
      vars_prompt: 
        - name: "githubuser" 
          prompt: "Enter your github username" 
          private: no 
        - name: "githubpassword" 
          prompt: "Enter your github password" 
          private: yes 
    
      [...] 
    

    And in the task reference the variables.

    task.yml

    - name: Get updated files from git repository 
      git:
        repo=https://{{ githubuser | urlencode }}:{{ githubpassword | urlencode }}@github.com/privrepo.git
        dest=/tmp
    

    This will save the password as clear text in .git/config as url of remote "origin". The following task can be used to remove it.

    - name: Ensure remote URL does not contain credentials
      git_config:
        name: remote.origin.url
        value: https://github.com/privrepo.git
        scope: local
        repo: /tmp
    

    Taken from: Clone a private git repository with Ansible (using password prompt)

提交回复
热议问题