2
votes

I am trying to run a playbook which execute against variables stored in the host_vars. This is the solution layout:

  • /host_vars/f5-test
  • /inventory/hosts
  • /playbook.yml

The content of the files are as follows:

/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

However when we execute the following command:

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

I get the following error:

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"
}

Any idea why it is not being picked up? I set up my project in the same structure found in the F5 ansible example, which can be found here.

Thanks!

3
Please read your question and fix obvious inconsistencies. It's hard to tell which mistakes you did when writing the question and which when setting up the files.techraf

3 Answers

6
votes

In your example hosts file f5-test is a group, not a host.

So put your variables into a group_vars file: ./group_vars/f5-test.yml:

f5_user: user
f5_password: password
f5_server: server
0
votes

The host_vars directory needs to be in the same folder as your inventory. So move /host_vars/f5-test to /inventory/host_vars/f5-test. Take a look at the best practices page for more info.

0
votes

In your example hosts file f5-test is a group

/inventory/host:

[f5-test] ---this is considered as a group

localhost --- this is considered as a host

so now create a host_vars/localhost

Then it will work