How to Get Environment Variables of Remote Host

How to get environment variables of remote host

The behavior of the lookup function is documented explicitly:

plugins allow access of data in Ansible from outside sources. These plugins are evaluated on the Ansible control machine...

There is a FAQ regarding access to remote environment variables:

Ansible 1.4 will also make remote environment variables available via facts in the ‘ansible_env’ variable:

{{ ansible_env.SOME_VARIABLE }}

Note that remote facts (like ansible_env) are only available if fact gathering is enabled (which is the default behavior of ansible, but can be disabled in the config file or in your playbooks).

If you want to modify the environment of the remote host, you again look to the documentation which describes the environment directive:

Ansible makes it easy for you to configure your environment by using the ‘environment’ keyword. Here is an example:

- hosts: all
remote_user: root

tasks:

- apt: name=cobbler state=installed
environment:
http_proxy: http://proxy.example.com:8080

These sets an environment variable for this specific task. It is not a persistent modification.

How do I get environment variable value from remote powershell C# code?

If you need a full list of environment variables on target machine or just one of it use following code:

using (var psShell = PowerShell.Create())
{
// Set up remote connection code

// Empty means that you will get all environment variables on target machine.
// You can use specific environment variable name to get only one.
var environmentVariableName = string.Empty;

psShell.AddCommand("Get-ChildItem")
.AddParameter("Path", $"Env:\\{environmentVariableName}");

var environmentVariablesDictionary = psShell.Invoke<DictionaryEntry>(); // Here will be dictionary of environment variables.
}

Get remote servers environment variables

Have not tried it, but you can check this out. the examples is vbscript, change strComputer to the IP address of your remote and see how it goes. For many remote hosts, use a for loop.

Reading an environment variable from the remote host

It is possible the env variable (SSH_AUTH_SOCK) is not in the remote's env, so it is returning nothing. One way to rule this out is to get something that is always available, eg: USER or SSH_CLIENT. If you can get that value, then you can safely assume SSH_AUTH_SOCK is not set in remote's env.

  - debug: msg={{ ansible_env.USER }}

The reason you see SSH_AUTH_SOCK is set when you ssh into the machine could be: Your login profile or bash script is starting ssh-agent which sets SSH_AUTH_SOCK variable with the unix socket so that ssh-add works correctly.

Set global environment variables in VS Code with remote-ssh

I think I found the solution in this issue comment and the follow-up response:

  • When vscode-server initially starts, it uses a login shell, sourcing .profile in your home directory.
  • However, any following interactive shells started through VS Code are non-login shells and thus only source .bashrc
  • A complication in fiddling with this is that vscode-server apparently caches the environment during its lifetimes, so changes to these dotfiles don't become visible until the server is restarted.


Related Topics



Leave a reply



Submit