question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

server.shell does not support env?

See original GitHub issue

I have this code:

env = dict(EMAIL=host.data.email)
cmd = f"docker stack deploy --compose-file docker-compose.yml {app}"
server.shell(name=f'Deploying stack {app}',
             commands=cmd,
             chdir=app_dir,
             env=env,
             )

but this gives a confusing error:

--> Starting operation: tasks/setup_traefik.py | Deploying stack traefik 
    [testbox.tothemax.nl] env: ‘cd’: No such file or directory
    [testbox.tothemax.nl] Error
--> pyinfra error: No hosts remaining!

Removing env=env makes the error go away (but obviously does not transfer the env vars.)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mjmarecommented, Oct 18, 2020

You are right (of course). Not trusting bash, here is a python equivalent:

from pyinfra.operations import server

server.shell(commands= '''python3 -c "import os; print('The env var is='+os.environ['hello'])"''',
             env={'hello':'world'})

Thanks.

0reactions
Fizzadarcommented, Oct 18, 2020

@mjmare this is due to the way bash/shell parses the command; the envvar is being set correctly but the $hello is evaluated beforehand; if you run printenv you’ll see the envvar exists:

from pyinfra.operations import server

server.shell(commands='printenv',
             env={'hello':'world'})

To echo the envvar you need to wrap the command w/sh:

from pyinfra.operations import server

server.shell(commands="sh -c 'echo $hello'",
             env={'hello':'world'})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Shell variable support in server.env · Issue #14901 - GitHub
Sourcing the server.env would actually break support for having an odd number of single quotes in a value. The solution must not require...
Read more >
How To Read and Set Environmental and Shell Variables on ...
In this guide, we will discuss how to interact with the environment and read or set environmental and shell variables interactively and through ......
Read more >
How to Set, List and Manage Linux Environment Variables
Shell variables are not the same as environment variables. Shell variables only apply to the current shell, not any child processes.
Read more >
162018 – [ssh][shells] environment getting and setting are not ...
I try to use a remoteshell to execute a command where I set a specific environment. The environment I specify does not get...
Read more >
Why does an SSH remote command get fewer environment ...
Great answer, this was exactly the problem, the needed environment variables resided in /etc/bashrc, which is not sourced in non interactive mode. Moving...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found