Skip to content

Commit cae4dbc

Browse files
committed
Add idempotency to the ssh.keyscan operation.
1 parent f573fb5 commit cae4dbc

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

pyinfra/operations/ssh.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,30 @@ def keyscan(hostname, force=False, port=22, state=None, host=None):
4747
pattern=hostname,
4848
)
4949

50+
did_keyscan = False
5051
keyscan_command = 'ssh-keyscan -p {0} {1} >> {2}/.ssh/known_hosts'.format(
5152
port, hostname, homedir,
5253
)
5354

5455
if not hostname_present:
5556
yield keyscan_command
57+
did_keyscan = True
5658

5759
elif force:
5860
yield 'ssh-keygen -R {0}'.format(hostname)
5961
yield keyscan_command
62+
did_keyscan = True
6063

6164
else:
6265
host.noop('host key for {0} already exists'.format(hostname))
6366

67+
if did_keyscan:
68+
host.create_fact(
69+
FindInFile,
70+
kwargs={'path': '{0}/.ssh/known_hosts'.format(homedir), 'pattern': hostname},
71+
data=['{0} unknown unknown'.format(hostname)],
72+
)
73+
6474

6575
def _user_or_ssh_user(user, ssh_user):
6676
if ssh_user:

0 commit comments

Comments
 (0)