Using getpass.getpass() instead of pre-filled password

If I have a groups.yaml file like below. is it possible to use getpass.getpass() instead of having a prefilled password in the yaml file? I would like to build something where different users use nornir but all get a prompt for their own password rather than having to edit the groups.yaml file.

username: ‘vagrant’
password: ‘vagrant’
platform: ‘ios’

1 Like

Here is a function I previously built that accomplishes that:

def nornir_set_creds(norn, username=None, password=None):
    """Handler so credentials don't need stored in clear in inventory."""
    if not username:
        username = input("Enter username: ")
    if not password:
        password = getpass()

    for host_obj in norn.inventory.hosts.values():
        host_obj.username = username
        host_obj.password = password

This function would be called after you initialize your inventory. There are several ways this problem can be solved besides this.


I use this and it works great! Thanks!

1 Like