Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Ansible
Manage
Activity
Members
Labels
Plan
Issues
3
Issue boards
Milestones
Code
Merge requests
2
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nounous
Ansible
Commits
f4dd6fe2
Commit
f4dd6fe2
authored
4 years ago
by
Benjamin Graillot
Committed by
Benjamin Graillot
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
[ssh_known_hosts] Use LDAP to deploy ssh_known_hosts
parent
009e7b42
No related branches found
No related tags found
1 merge request
!217
[ssh_known_hosts] Use LDAP to deploy ssh_known_hosts
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lookup_plugins/ldap.py
+19
-0
19 additions, 0 deletions
lookup_plugins/ldap.py
roles/ssh_known_hosts/templates/ssh/ssh_known_hosts.j2
+7
-5
7 additions, 5 deletions
roles/ssh_known_hosts/templates/ssh/ssh_known_hosts.j2
with
26 additions
and
5 deletions
lookup_plugins/ldap.py
+
19
−
0
View file @
f4dd6fe2
...
...
@@ -104,6 +104,23 @@ class LookupModule(LookupBase):
result
.
append
(
cn
.
decode
(
'
utf-8
'
))
return
result
def
ssh_keys
(
self
,
host
):
"""
Retrieve SSH keys of a host
query(
'
ldap
'
,
'
ssh_keys
'
, HOST)
"""
host_query_id
=
self
.
base
.
search
(
f
"
cn=
{
host
}
,ou=hosts,
{
self
.
base_dn
}
"
,
ldap
.
SCOPE_BASE
)
host_result
=
self
.
base
.
result
(
host_query_id
)[
1
][
0
][
1
]
result
=
[]
if
'
description
'
not
in
host_result
:
return
result
for
description
in
host_result
[
'
description
'
]:
description
=
description
.
decode
(
'
utf-8
'
)
key
,
value
=
description
.
split
(
'
:
'
,
1
)
if
key
in
{
'
ecdsa-sha2-nistp256
'
,
'
ssh-ed25519
'
,
'
ssh-dss
'
,
'
ssh-rsa
'
}:
result
.
append
(
f
'
{
key
}
{
value
}
'
)
return
result
def
subnet_ipv4
(
self
,
subnet
):
"""
Retrieve used IP addresses on a subnet
...
...
@@ -132,6 +149,8 @@ class LookupModule(LookupBase):
result
=
self
.
all_cn
(
*
terms
[
1
:])
elif
terms
[
0
]
==
'
subnet_ipv4
'
:
result
=
self
.
subnet_ipv4
(
*
terms
[
1
:])
elif
terms
[
0
]
==
'
ssh_keys
'
:
result
=
self
.
ssh_keys
(
*
terms
[
1
:])
elif
terms
[
0
]
==
'
group
'
:
query_id
=
self
.
base
.
search
(
f
"
ou=group,
{
self
.
base_dn
}
"
,
ldap
.
SCOPE_SUBTREE
,
"
objectClass=posixGroup
"
)
result
=
self
.
base
.
result
(
query_id
)
...
...
This diff is collapsed.
Click to expand it.
roles/ssh_known_hosts/templates/ssh/ssh_known_hosts.j2
+
7
−
5
View file @
f4dd6fe2
{% for host in groups["server"] | sort %}
{% for keytype in ['ecdsa', 'rsa', 'ed25519'] %}
{% if 'ssh_host_key_{}_public'.format(keytype) in hostvars[host]['ansible_facts'].keys() %}
{{ query('ldap', 'all_cn', hostvars[host]['ansible_facts']['hostname']) | join(',') }},{{ query('ldap', 'all_ip', hostvars[host]['ansible_facts']['hostname']) | join(',') }} ssh-{{ keytype }} {{ hostvars[host]['ansible_facts']['ssh_host_key_{}_public'.format(keytype)] }} root@{{ hostvars[host]['ansible_facts']['hostname'] }}
{% endif %}
{{ ansible_header | comment }}
{% set hosts = query('ldap', 'query', 'ou=hosts,dc=crans,dc=org', 'one', 'objectClass=device') %}
{% for host, device in hosts.items() | sort(attribute='0') %}
{% set cns = query('ldap', 'all_cn', hosts[host].cn[0]) | sort %}
{% set ips = query('ldap', 'all_ip', hosts[host].cn[0]) | sort %}
{% for key in query('ldap', 'ssh_keys', hosts[host].cn[0]) | sort %}
{{ cns | join(',') }},{{ ips | join(',') }} {{ key }} root@{{ hosts[host].cn[0] }}
{% endfor %}
{% endfor %}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment