Ansible 使用用户的环境变量以用户身份运行任务

问题描述

我需要从另一个用户运行剧本,但要使用该用户的环境变量。 我怀疑是简单的 becomebecome_user,所以我决定检查一下。 我尝试添加标志来模仿 sudo su -l myuser - 它没有帮助。

Playbooks 由 Ansible Tower 推出,项目 scm 是 git(组织本地 bitbucket)。

---
- name: env checker
  gather_facts: false
  hosts: localhost
  tasks:

    - name: Testing output
      debug:
        msg: "'{{ lookup('env','JAVA_HOME') }}' is the JAVA_HOME environment variable."
      become: yes
      become_user: myuser
      become_flags: "-l"

有没有办法正确地做到这一点?本质上,我需要运行一堆依赖于一堆环境变量的 shell 命令,这就是我要检查这个的原因。

非常感谢。

解决方法

您可以使用以下命令来执行驻留在 Ansible Tower 中的 playbook,并可以通过该命令传递相关的环境变量。

curl -f -k -H 'Content-Type: application/json' -POST --user username:password https://<ip-address-of-ansible-server>/api/v2/job_templates/<number-of-the-job-template>/launch/ --insecure  -d '{"extra_vars": "{\"variable1\": \"value-of-variable1\"}"}'

Add your Variables here