Ansible 无法从vars_files加载变量



我使用这个版本的Ansible:

ansible 2.9.6
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/ychtourou/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.8.10 (default, Jun  2 2021, 10:49:15) [GCC 9.4.0]

我有这个小剧本:

- name: play create users from var file
hosts: all
become: yes
vars_files: "vars/users.yaml"
tasks:
- name: group exists
group:
name: "{‌{ grp }}"
state: present
- name: user exists
user:
name: "{‌{ user }}"
uid: "{‌{ id }}"
state: present
groups: "{‌{ grp }}"
append: yes

用户。yaml文件

user: qmerlin
id: 1000
grp: ops

剧本和用户的位置。html文件:

.
├── inventory
├── playbook1.yml
└── vars
└── users.yaml

当使用这个命令ansible-playbook -i inventory playbook1.yml --ask-become-pass执行时,我得到这个错误:

BECOME password: 
PLAY [play create users from var file] ***************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************
ok: [server1]
TASK [group exists] **********************************************************************************************************************************
fatal: [server1]: FAILED! => {"changed": false, "msg": "groupadd: '{‌{ grp }}' is not a valid group namen", "name": "{‌{ grp }}"}
PLAY RECAP *******************************************************************************************************************************************
server1                    : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

看起来Ansible无法从文件

中获取值如何解决这个问题?

你在剧本中引入了角色ZERO WIDTH NON-JOINER。如果你用vim检查剧本,你会看到:

- name: group exists
group:
name: "{<200c>{ grp }}"
state: present
- name: user exists

(其中<200c>表示所讨论的字符)

这个额外的字符意味着你没有Jinja{{模板标记,这样您就不会得到任何变量替换。

如果我从剧本中删除错误字符,它将运行为预期。

相关内容

  • 没有找到相关文章

最新更新