Skip to content
This repository was archived by the owner on Jan 30, 2021. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 64 additions & 4 deletions library/ssh_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
`~/.ssh/config`.
default: root
choices: []
group:
description:
- Which group this configuration file belongs to.
default: root
choices: []
host:
description:
- The endpoint this configuration is valid for. Can be an actual
Expand Down Expand Up @@ -58,6 +63,9 @@
description:
- Sets the ProxyCommand option.
required: false
extra:
description:
- Provides any extra configuration directives
'''

EXAMPLES = '''
Expand Down Expand Up @@ -679,6 +687,8 @@ def change_host(options, **kwargs):
options = copy.deepcopy(options)
changed = False
for k, v in kwargs.items():
k = k.lower()

if '_' in k:
k = k.replace('_', '')

Expand All @@ -696,24 +706,51 @@ def change_host(options, **kwargs):
def main():
module = AnsibleModule(
argument_spec=dict(
state=dict(default='present', choices=['present', 'absent']),
host=dict(required=True, type='str'),
state=dict(
default='present',
choices=[
'present',
'absent'
]
),
host=dict(
required=True,
type='str'
),
hostname=dict(type='str'),
port=dict(type='str'),
remote_user=dict(type='str'),
identity_file=dict(type='str'),
user=dict(default=None, type='str'),
group=dict(default=None, type='str'),
user_known_hosts_file=dict(default=None, type='str'),
proxycommand=dict(default=None, type='str'),
strict_host_key_checking=dict(
default=None,
choices=['yes', 'no', 'ask']
),
extra=dict(
type='dict',
elements='dict'
"""
options=dict(
ip_protocol=dict(
required=True,
type='str'
),
ports=dict(
type='list',
elements='str'
)
)
"""
),
),
supports_check_mode=True
)

user = module.params.get('user')
group = module.params.get('group')
host = module.params.get('host')
args = dict(
hostname=module.params.get('hostname'),
Expand All @@ -738,6 +775,9 @@ def main():
os.path.expanduser('~{0}'.format(user)), '.ssh', 'config'
)

if group is None:
group = 'root'

# See if the identity file exists or not, relative to the config file
if os.path.exists(config_file) and args['identity_file']:
dirname = os.path.dirname(config_file)
Expand All @@ -752,12 +792,31 @@ def main():
msg='IdentityFile "{0}" does not exist'.format(identity_file)
)

extra = module.params.get('extra')

if extra:
for key in extra:
if key not in SSH_KEYWORDS:
module.fail_json(
msg='Unrecognized SSH keyword "{0}"'.format(key)
)
else:
args[key] = extra[key]

config = ConfigParser(config_file)
config.load()

found = False

results = config.search_host(host)
if results:

for h in results:
if h['host'] != host:
continue

found = True

# Anything to remove?
if state == 'absent':
config_changed = True
Expand All @@ -776,8 +835,9 @@ def main():
'new': options,
}
})

# Anything to add?
elif state == 'present':
if not found and state == 'present':
changed, options = change_host(dict(), **args)

if changed:
Expand All @@ -789,7 +849,7 @@ def main():
config.write_to_ssh_config()
# MAKE sure the file is owned by the right user
module.set_owner_if_different(config_file, user, False)
module.set_group_if_different(config_file, user, False)
module.set_group_if_different(config_file, group, False)
module.set_mode_if_different(config_file, '0600', False)

module.exit_json(changed=config_changed,
Expand Down