Pyinfra: Dynamic config constants (Global to auto fill sudo password for every pyinfra call.)

Created on 3 Feb 2021  路  4Comments  路  Source: Fizzadar/pyinfra

Is your feature request related to a problem? Please describe.
I am frustrated when I have to explicitly pass use_sudo_password= with every pyinfra call for scripts I do not want to run as root.

Describe the solution you'd like
Add another global to do this for me or allow USE_SUDO_PASSWORD to be set to the password for the current session which would emulate passing use_sudo_password= for all pyinfra calls.

Thanks.

CLI Spec

Most helpful comment

Ah, I see. I love the idea of the local config.py or a config override object!

All 4 comments

Hi @gnat! I believe this should work using the USE_SUDO_PASSWORD variable passing the password directly; ie:

from pyinfra.operations import server

USE_SUDO_PASSWORD = 'top-secret-password'
SUDO = True

server.shell('echo hi')

Unfortunately found a bug, not sure how to fix it. Yours works. All of the following do not work.

from pyinfra.operations import server

USE_SUDO_PASSWORD = f"hello"
SUDO = True

server.shell('echo hi')
from pyinfra.operations import server

top_secret_password = "hello"
USE_SUDO_PASSWORD = f"{top_secret_password}"
SUDO = True

server.shell('echo hi')
from pyinfra.operations import server

top_secret_password = "hello"
USE_SUDO_PASSWORD = top_secret_password
SUDO = True

server.shell('echo hi')

Thanks @Fizzadar

So this is due to the config variables only accepting constants - they're parsed out of the config/deploy file via ast without actually loading (and evaluating) the file. This means any values that aren't booleans, simple (non-formatted) strings or numbers won't be accepted.

I'm keen on getting this working though as it could be useful (particularly getting password via user input/similar). Need to think about sensible ways to achieve this. One idea would be to have a pyinfra.config object that is available within operation files specifically to enable overriding for just that file, ie:

from pyinfra import config

config.SUDO = True
...

This would have the additional benefit of making config.py more explicit as "the default config for operations in this directory".

Ah, I see. I love the idea of the local config.py or a config override object!

Was this page helpful?
0 / 5 - 0 ratings