Ansible Git Module with Username Password-DecodingDevOps

Ansible Git Module with Username Password

in this blog post i am going to explain how to checkout the code from github or bitbucket, using ansible git module with username password. 

root@ip-172-31-37-35:~# cat git.yml


---
- hosts: localhost
  gather_facts: no
  vars:
    username: decodingdevops
    password: sjjfddf7ee@3kj!8
    repo_name: devops

  tasks:
  - name: Ansible Git Module with Username Password.
    git:
     repo: 'https://{{ username | urlencode }}:{{ password | urlencode }}@github.com/{{ username }}/{{ repo_name }}.git'
     dest: /root/devops

Here i am defining three variables username, pasowrd, repo_name and i am using these three variables in ansible git module.

username is my github/bitbucker account username.

Password is Github/Bitbucket login paswsword.

repo_name is my repository name in github/bitbucket

In ansible git module i used two arguments one is repo and another one is dest. Repo represents the github/bitbucket url. Dest represnets the destination path, on this path only path only your downloaded/chekout code will be saved.

Output:

root@ip-172-31-37-35:~# ansible-playbook abc.yml

PLAY [localhost] ***********************************************************************************************************************************************************

TASK [nsible Git Module with Username Password.] ********************************************************************************************************************************************
changed: [localhost]

PLAY RECAP *****************************************************************************************************************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

root@ip-172-31-37-35:~# ls
devops  git.yml

root@ip-172-31-37-35:~# pwd
/root

If there is any special characters in your username and password, you must and should use urlencode method as shown in above. otherwise you will get error like Could not resolve the host.

Error:

fatal: [localhost]: FAILED! => {“changed”: false, “cmd”: [“/usr/bin/git”, “fetch”, “–tags”, “origin”], “msg”: “Failed to download remote objects and refs: fatal: unable to access ‘https://decodingdevops:ds45n44cci!@github.com/sfdfgfv45d443t/devops.git/’: Could not resolve host: @github.com\n”}

 

Leave a Reply

Your email address will not be published. Required fields are marked *