Ansible 不会从 .bashrc 加载环境变量

Ansible 不会从 .bashrc 加载环境变量

我想使用 Ansible 剧本从 .bashrc 文件预加载变量。

我尝试过以下方法:

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

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

其他方式:

- 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 }}"

两者都返回此结果:

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

我从这里得到了这个“解决方案”:https://stackoverflow.com/questions/60209185/ansible-environment-variables-from-env-file但它并没有像我预期的那样工作。如何从 .bashrc 文件预加载这些 shell 变量?

答案1

是的 - 使用 become_flag:“--login”

相关内容