gpt4 book ai didi

Ansible group_vars 环境变量替换

转载 作者:行者123 更新时间:2023-12-04 14:24:16 30 4
gpt4 key购买 nike

将其放入group_vars/all:

default_environment:
HOME2: "{{ ansible_env.HOME }}"

在任务中使用时定义环境:

environment:
"{{ default_environment }}"

我明白了:

The field 'environment' has an invalid value, which includes an undefined variable. The error was: {u'HOME2': u'{{ ansible_env.HOME }}'}: 'ansible_env' is undefined
exception type: <class 'ansible.errors.AnsibleUndefinedVariable'>

有没有办法根据当前的远程环境变量定义一个组变量,在这种情况下是$HOME

编辑:根据请求,可重现的案例:

测试.yml

- name: Test
hosts: hosts1
become: true
become_user: user1

environment:
"{{ default_environment }}"

tasks:
- debug:
msg: "{{ ansible_env }}"

group_vars/all

default_environment:
HOME2: "{{ ansible_env.HOME }}"
使用 ansible-plyabook test.yml

运行时的

输出

PLAY [Test] ********************************************************************************************************************************************************************************************************************************


TASK [Gathering Facts] *********************************************************************************************************************************************************************************************************************

fatal: [host1]: FAILED! => {}

MSG:

The field 'environment' has an invalid value, which includes an undefined variable. The error was: {u'HOME2': u'{{ ansible_env.HOME }}'}: 'ansible_env' is undefined
exception type: <class 'ansible.errors.AnsibleUndefinedVariable'>
exception: {u'HOME2': u'{{ ansible_env.HOME }}'}: 'ansible_env' is undefined

to retry, use: --limit @/work/ansible/test.retry

PLAY RECAP *********************************************************************************************************************************************************************************************************************************

host1 : ok=0 changed=0 unreachable=0 failed=1

最佳答案

错误信息是:

'ansible_env' is undefined

你没有收集事实。


在您尝试仅收集事实的游戏之前运行一个游戏,而不声明环境:

- name: Test
hosts: hosts1
become: true
become_user: user1

- name: Test
hosts: hosts1
become: true
become_user: user1
gather_facts: false

environment:
"{{ default_environment }}"

tasks:
- debug:
msg: "{{ ansible_env }}"

关于Ansible group_vars 环境变量替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48892166/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com