我在/etc/profile.d/文件夹中定义了一个envs.sh脚本.
执行ansible-playbook时,我试图获取此env var的值,但它却抛出了一个错误:
Ansible测试:
debug: msg="{{ ansible_env.NGN_VAL }} is an environment variable"
错误:
fatal: [xxx.yyy.zzz.kkk] => One or more undefined variables: 'dict object' has no attribute 'NGN_VAL'
FATAL: all hosts have already failed -- aborting
为什么它不执行该文件夹中的脚本?当我通过ssh连接时,我回显它并显示它的值.如何在ansible执行期间设置远程环境变量并获取它们?
谢谢
最佳答案 如果要捕获环境变量,请使用此方法进行测试.
[jenkins@scsblnx-828575 jenkins]$cat mypass
export MYPASSWD=3455637
[jenkins@scsblnx-828575 jenkins]$cat test.yml
- hosts: all
user: jenkins
tasks:
- name: Test variables.
shell: source /apps/opt/jenkins/mypass && echo $MYPASSWD
register: myenvpass
- debug: var=myenvpass.stdout
[jenkins@scsblnx-828575 jenkins]$ansible-playbook -i hosts test.yml
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [127.0.0.1]
TASK [Test variables.] *********************************************************
changed: [127.0.0.1]
TASK [debug] *******************************************************************
ok: [127.0.0.1] => {
"myenvpass.stdout": "3455637"
}
PLAY RECAP *********************************************************************
127.0.0.1 : ok=3 changed=1 unreachable=0 failed=0