mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Fixes #4108 Add sshopts and keyfile parameters to the git module
This commit is contained in:
parent
f2b2354332
commit
18050d50ec
1 changed files with 72 additions and 1 deletions
|
@ -49,6 +49,21 @@ options:
|
||||||
version_added: "1.5"
|
version_added: "1.5"
|
||||||
description:
|
description:
|
||||||
- Add the hostkey for the repo url if not already added.
|
- Add the hostkey for the repo url if not already added.
|
||||||
|
sshopts:
|
||||||
|
required: false
|
||||||
|
default: None
|
||||||
|
version_added: "1.5"
|
||||||
|
description:
|
||||||
|
- Creates a wrapper script and exports the path as GIT_SSH
|
||||||
|
which git then automatically uses to override ssh arguments.
|
||||||
|
An example value could be "-o StrictHostKeyChecking=no"
|
||||||
|
keyfile:
|
||||||
|
requird: false
|
||||||
|
default: None
|
||||||
|
version_added: "1.5"
|
||||||
|
description:
|
||||||
|
- Uses the same wrapper method as sshopts to pass
|
||||||
|
"-i <keyfile>" to the ssh arguments used by git
|
||||||
reference:
|
reference:
|
||||||
required: false
|
required: false
|
||||||
default: null
|
default: null
|
||||||
|
@ -124,6 +139,45 @@ EXAMPLES = '''
|
||||||
import re
|
import re
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
|
def write_ssh_wrapper():
|
||||||
|
fh = tempfile.NamedTemporaryFile(delete=False)
|
||||||
|
wrapper_path = fh.name
|
||||||
|
template = """#!/bin/sh
|
||||||
|
if [ -z "$GIT_SSH_OPTS" ]; then
|
||||||
|
BASEOPTS=""
|
||||||
|
else
|
||||||
|
BASEOPTS=$GIT_SSH_OPTS
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z "$GIT_KEY" ]; then
|
||||||
|
ssh $BASEOPTS "$@"
|
||||||
|
else
|
||||||
|
ssh -i "$GIT_KEY" $BASEOPTS "$@"
|
||||||
|
fi
|
||||||
|
"""
|
||||||
|
fh.write(template)
|
||||||
|
fh.close()
|
||||||
|
st = os.stat(wrapper_path)
|
||||||
|
os.chmod(wrapper_path, st.st_mode | stat.S_IEXEC)
|
||||||
|
return wrapper_path
|
||||||
|
|
||||||
|
def set_git_ssh(ssh_wrapper, key_file, ssh_opts):
|
||||||
|
|
||||||
|
if os.environ.get("GIT_SSH"):
|
||||||
|
del os.environ["GIT_SSH"]
|
||||||
|
os.environ["GIT_SSH"] = ssh_wrapper
|
||||||
|
|
||||||
|
if os.environ.get("GIT_KEY"):
|
||||||
|
del os.environ["GIT_KEY"]
|
||||||
|
|
||||||
|
if key_file:
|
||||||
|
os.environ["GIT_KEY"] = key_file
|
||||||
|
|
||||||
|
if os.environ.get("GIT_SSH_OPTS"):
|
||||||
|
del os.environ["GIT_SSH_OPTS"]
|
||||||
|
|
||||||
|
if ssh_opts:
|
||||||
|
os.environ["GIT_SSH_OPTS"] = ssh_opts
|
||||||
|
|
||||||
def get_version(git_path, dest, ref="HEAD"):
|
def get_version(git_path, dest, ref="HEAD"):
|
||||||
''' samples the version of the git repo '''
|
''' samples the version of the git repo '''
|
||||||
|
@ -360,6 +414,8 @@ def main():
|
||||||
depth=dict(default=None, type='int'),
|
depth=dict(default=None, type='int'),
|
||||||
update=dict(default='yes', type='bool'),
|
update=dict(default='yes', type='bool'),
|
||||||
accept_hostkey=dict(default='no', type='bool'),
|
accept_hostkey=dict(default='no', type='bool'),
|
||||||
|
keyfile=dict(default=None, required=False),
|
||||||
|
sshopts=dict(default=None, required=False),
|
||||||
executable=dict(default=None),
|
executable=dict(default=None),
|
||||||
bare=dict(default='no', type='bool'),
|
bare=dict(default='no', type='bool'),
|
||||||
),
|
),
|
||||||
|
@ -377,6 +433,17 @@ def main():
|
||||||
reference = module.params['reference']
|
reference = module.params['reference']
|
||||||
git_path = module.params['executable'] or module.get_bin_path('git', True)
|
git_path = module.params['executable'] or module.get_bin_path('git', True)
|
||||||
|
|
||||||
|
key_file = module.params['keyfile']
|
||||||
|
ssh_opts = module.params['sshopts']
|
||||||
|
|
||||||
|
# create a wrapper script and export
|
||||||
|
# GIT_SSH=<path> as an environment variable
|
||||||
|
# for git to use the wrapper script
|
||||||
|
ssh_wrapper = None
|
||||||
|
if key_file or ssh_opts:
|
||||||
|
ssh_wrapper = write_ssh_wrapper()
|
||||||
|
set_git_ssh(ssh_wrapper, key_file, ssh_opts)
|
||||||
|
|
||||||
# add the git repo's hostkey
|
# add the git repo's hostkey
|
||||||
#if module.params['accept_hostkey']:
|
#if module.params['accept_hostkey']:
|
||||||
add_git_host_key(module, repo, accept_hostkey=module.params['accept_hostkey'])
|
add_git_host_key(module, repo, accept_hostkey=module.params['accept_hostkey'])
|
||||||
|
@ -438,6 +505,10 @@ def main():
|
||||||
if before != after or local_mods:
|
if before != after or local_mods:
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
|
# cleanup the wrapper script
|
||||||
|
if ssh_wrapper:
|
||||||
|
os.remove(ssh_wrapper)
|
||||||
|
|
||||||
module.exit_json(changed=changed, before=before, after=after)
|
module.exit_json(changed=changed, before=before, after=after)
|
||||||
|
|
||||||
# import module snippets
|
# import module snippets
|
||||||
|
|
Loading…
Reference in a new issue