1

I want to preload variables from .bashrc file with an Ansible playbook.

I tried these ways:

- hosts: my_host
  tasks: 
    - name: Display environment variables
      shell: |
        . ./.env_file_name && env

    - name: Do another action
      shell: |
        . ./.env_file_name && do_something_else

Another way:

- hosts: "{{ host }}" 
  tasks:
    - name: source bashrc file
      shell: . /home/user/.bashrc && env
      register: env_file_result

    - name: Show
      debug:
        msg: "{{ env_file_result.stdout_lines }}"

Both returns this:

TASK [source bashrc file] ************************************************************************************************************************************************************************************
task path: /home/srvadm/playbooks/hello.yml:3
Using module file /usr/lib/python3.6/site-packages/ansible/modules/commands/command.py
Pipelining is enabled.
<XX.XX.XX.XX> ESTABLISH SSH CONNECTION FOR USER: user
<XX.XX.XX.XX> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="user"' -o ConnectTimeout=10 -o ControlPath=/home/srvadm/.ansible/cp/d9553c19b6 XX.XX.XX.XX '/bin/sh -c '"'"'/usr/bin/python && sleep 0'"'"''
<XX.XX.XX.XX> (0, b'\n{"changed": true, "end": "2021-03-12 11:56:15.596390", "stdout": "MAIL=/var/mail/user\\nSSH_CLIENT=XX.XX.XX.XX 41318 22\\nUSER=user\\nSHLVL=1\\nHOME=/home/user\\nLC_CTYPE=C.UTF-8\\nLOGNAME=user\\n_=/bin/sh\\nXDG_SESSION_ID=35493\\nPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games\\nXDG_RUNTIME_DIR=/run/user/1000\\nLANG=en_US.UTF-8\\nSHELL=/bin/bash\\nPWD=/home/user\\nSSH_CONNECTION=XX.XX.XX.XX 41318 XX.XX.XX.XX 22", "cmd": ". /home/user/.bashrc && env", "rc": 0, "start": "2021-03-12 11:56:15.593574", "stderr": "", "delta": "0:00:00.002816", "invocation": {"module_args": {"creates": null, "executable": null, "_uses_shell": true, "strip_empty_ends": true, "_raw_params": ". /home/user/.bashrc && env", "removes": null, "argv": null, "warn": true, "chdir": null, "stdin_add_newline": true, "stdin": null}}}\n', b'')
changed: [XX.XX.XX.XX] => {
    "changed": true,
    "cmd": ". /home/user/.bashrc && env",
    "delta": "0:00:00.002816",
    "end": "2021-03-12 11:56:15.596390",
    "invocation": {
        "module_args": {
            "_raw_params": ". /home/user/.bashrc && env",
            "_uses_shell": true,
            "argv": null,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "stdin_add_newline": true,
            "strip_empty_ends": true,
            "warn": true
        }
    },
    "rc": 0,
    "start": "2021-03-12 11:56:15.593574",
    "stderr": "",
    "stderr_lines": [],
    "stdout": "MAIL=/var/mail/user\nSSH_CLIENT=XX.XX.XX.XX 41318 22\nUSER=user\nSHLVL=1\nHOME=/home/user\nLC_CTYPE=C.UTF-8\nLOGNAME=user\n_=/bin/sh\nXDG_SESSION_ID=35493\nPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games\nXDG_RUNTIME_DIR=/run/user/1000\nLANG=en_US.UTF-8\nSHELL=/bin/bash\nPWD=/home/user\nSSH_CONNECTION=XX.XX.XX.XX 41318 XX.XX.XX.XX 22",
    "stdout_lines": [
        "MAIL=/var/mail/user",
        "SSH_CLIENT=XX.XX.XX.XX 41318 22",
        "USER=user",
        "SHLVL=1",
        "HOME=/home/user",
        "LC_CTYPE=C.UTF-8",
        "LOGNAME=user",
        "_=/bin/sh",
        "XDG_SESSION_ID=35493",
        "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games",
        "XDG_RUNTIME_DIR=/run/user/1000",
        "LANG=en_US.UTF-8",
        "SHELL=/bin/bash",
        "PWD=/home/user",
        "SSH_CONNECTION=XX.XX.XX.XX 41318 XX.XX.XX.XX 22"
    ]
}

TASK [Show] **************************************************************************************************************************************************************************************************
task path: /home/srvadm/playbooks/hello.yml:7
ok: [XX.XX.XX.XX] => {
    "msg": [
        "MAIL=/var/mail/user",
        "SSH_CLIENT=XX.XX.XX.XX YYYY 22",
        "USER=user",
        "SHLVL=1",
        "HOME=/home/user",
        "LC_CTYPE=C.UTF-8",
        "LOGNAME=user",
        "_=/bin/sh",
        "XDG_SESSION_ID=35493",
        "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games",
        "XDG_RUNTIME_DIR=/run/user/1000",
        "LANG=en_US.UTF-8",
        "SHELL=/bin/bash",
        "PWD=/home/user",
        "SSH_CONNECTION=XX.XX.XX.XX YYYY XX.XX.XX.XX 22"
    ]
}
META: ran handlers
META: ran handlers

I got this "solution" from here: https://stackoverflow.com/questions/60209185/ansible-environment-variables-from-env-file but It does not work as I expected. How I can preload these shell variables from .bashrc file?

1
  • 1
    The debug output appears to show that it worked correctly. What exactly is the problem? Mar 12, 2021 at 20:21

1 Answer 1

0

Yeah - use a become_flag: "--login"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.