在本地主机上运行 ansible playbook,但指的是库存的 group_var

dev*_*dom 6 ansible

我正在尝试在本地运行剧本,但我希望角色的 task/main.yml 文件中的所有变量都引用特定清单文件中的 group_var。

不幸的是,剧本无法访问 group_vars 目录,就像无法识别角色中指定的变量一样。

运行的命令如下:

/usr/local/bin/ansible-playbook --connection=local /opt/ansible/playbooks/create.yml -i ./inventory-file

但在/group_vars 目录下的清单文件的同一目录级别中找不到 group_vars

fatal: [127.0.0.1]: FAILED! => {
    "msg": "The task includes an option with an undefined variable. The error was: 'admin_user_name' is undefined\n\nThe error appears to have been in '/opt/roles/create/tasks/main.yml': line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n- name: create org\n  ^ here\n"
}
Run Code Online (Sandbox Code Playgroud)

这是我的配置:

ansible-playbook 2.7.5
config file = /etc/ansible/ansible.cfg
configured module search path = ['/opt/ansible-modules']
  ansible python module location = /usr/local/lib/python3.6/dist-packages/ansible
executable location = /usr/local/bin/ansible-playbook
python version = 3.6.3 (default, Oct  3 2017, 21:45:48) [GCC 7.2.0]
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏。

谢谢,多姆

dev*_*dom 4

因此,理论上在清单中添加 localhost 是一个很好的解决方案,但在我的特定情况下(通常对于大型部署)不是一个选择。

我还添加了--extra-vars "myvar.json"但是也没用。

事实证明(邪恶的细节......)通过命令行添加 var 文件的正确方法是: --extra-vars "@myvar.json"

将其发布在这里,希望没有其他人费尽心思找到这个解决方案。

干杯,多姆