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.


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

https://nornir.readthedocs.io/en/latest/howto/transforming_inventory_data.html?highlight=transform#Setting-a-default-password

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.

3 Likes

I use this and it works great! Thanks!

1 Like