Ansible 剧本不会获取主机变量

Ansible playbook doesn't pickup host variables

我正在尝试 运行 一个针对存储在 host_vars 中的变量执行的剧本。这是解决方案布局:

文件内容如下:

/inventory/host:

[f5-test]
localhost

/host_vars/hosts

f5_user:user
f5_password:password
f5_server:server

/playbook.yml

- name: Playbook
  hosts: f5-test
  gather_facts: no
  gather_subset: no

  tasks:
    - name: Taks_01
      tags: "bigip-node"
      bigip_node:
        server: "{{f5_server}}"
        user: "{{f5_user}}"
        password: "{{f5_password}}"
        state: "present"
        partition: "Common"
        host: "10.20.30.40"
        name: "F5_Node"
        session_state: "enabled"
        description: "description"
      delegate_to: localhost

但是当我们执行以下命令时:

sudo ansible-playbook playbook.yml -i inventory/hosts -vvvv

我收到以下错误:

task path: /home/dev/ansible/playbook.yml:9
fatal: [localhost]: FAILED! => {
    "failed": true,
    "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'f5_server' is undefined\n\nThe error appears to have been in '/home/dev/ansible/playbook.yml': line 9, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  tasks:\n    - name: Task_01\n      ^ here\n"
}

知道为什么它没有被提取吗?我在 F5 ansible 示例中找到的相同结构中设置了我的项目,可以在 here.

中找到

谢谢!

host_vars 目录需要与您的清单位于同一文件夹中。所以将 /host_vars/f5-test 移动到 /inventory/host_vars/f5-test。查看 best practices page 了解更多信息。

在您的示例主机文件中 f5-test 是一个组,而不是主机。

所以将变量放入 group_vars 文件中:./group_vars/f5-test.yml:

f5_user: user
f5_password: password
f5_server: server
#

在您的示例主机文件中,f5-test 是一个组

/inventory/host:

[f5-test] ---这被认为是一组

localhost --- 这被视为主机

所以现在创建一个 host_vars/localhost

然后就可以了