Forum Discussion

Thornid's avatar
Thornid
Icon for Nimbostratus rankNimbostratus
Jul 24, 2020

Basic Question on Ansible - Creating Nodes

Hello all

I know this is basic but I'm starting off learning about Ansible and F5s. I'm following this lab guide:

https://github.com/ansible/workshops/tree/devel/exercises/ansible_f5/1.2-add-node

I have configured a basic inventory as such:

[lb]
f5 ansible_host=X.X.X.X ansible_user=admin private_ip=X.X.X.X ansible_ssh_pass=XXXXXXXX

[webservers]
host1 ansible_host=X.X.X.X
host2 ansible_host=X.X.X.X

I then have my YAML file as follows:

---
- name: BIG-IP SETUP
 hosts: lb
 connection: local
 gather_facts: false

 tasks:

 - name: CREATE NODES
   bigip_node:
     provider:
       server: X.X.X.X
       user: XXXX
       password: XXXXXXX
       server_port: 443
       validate_certs: false
     host: "{{hostvars[webservers].ansible_host}}"
     name: "{{hostvars[webservers].inventory_hostname}}"
   loop: "{{ groups[webservers] }}"

When I try to run this playbook I get the following error:

devlin@ubuntu-v20:~/ansible/f5/lab2$ ansible-playbook -i inventory bigip-node.yaml

PLAY [BIG-IP SETUP] *****************************************************************************************************************

TASK [CREATE NODES] *****************************************************************************************************************
fatal: [f5]: FAILED! => {"msg": "'webservers' is undefined"}

PLAY RECAP **************************************************************************************************************************
f5                        : ok=0   changed=0   unreachable=0   failed=1   skipped=0   rescued=0   ignored=0

What does it mean "webservers is undefined"?

Thank you?

  • Thanks ArvinF

    Even if I explicitly tell the playbook which inventory file I get the same thing:

    devlin@ubuntu-v20:~/ansible/f5/lab2$ ansible-playbook -i ~/ansible/f5/lab2/inventory bigip-node-3.yaml -vvv
    ansible-playbook 2.9.6
      config file = /etc/ansible/ansible.cfg
      configured module search path = ['/home/devlin/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
      ansible python module location = /usr/lib/python3/dist-packages/ansible
      executable location = /usr/bin/ansible-playbook
      python version = 3.8.2 (default, Jul 16 2020, 14:00:26) [GCC 9.3.0]
    Using /etc/ansible/ansible.cfg as config file
    host_list declined parsing /home/devlin/ansible/f5/lab2/inventory as it did not pass its verify_file() method
    script declined parsing /home/devlin/ansible/f5/lab2/inventory as it did not pass its verify_file() method
    auto declined parsing /home/devlin/ansible/f5/lab2/inventory as it did not pass its verify_file() method
    Parsed /home/devlin/ansible/f5/lab2/inventory inventory source with ini plugin
     
    PLAYBOOK: bigip-node-3.yaml ************************************************************************************************************************************************************************************************
    1 plays in bigip-node-3.yaml
     
    PLAY [BIG-IP SETUP] ********************************************************************************************************************************************************************************************************
    META: ran handlers
     
    TASK [CREATE NODES] ********************************************************************************************************************************************************************************************************
    task path: /home/devlin/ansible/f5/lab2/bigip-node-3.yaml:9
    fatal: [f5]: FAILED! => {
        "msg": "The task includes an option with an undefined variable. The error was: \"hostvars['webservers']\" is undefined\n\nThe error appears to be in '/home/devlin/ansible/f5/lab2/bigip-node-3.yaml': line 9, column 5, 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 NODES\n    ^ here\n"
    }
     
    PLAY RECAP *****************************************************************************************************************************************************************************************************************
    f5                         : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
    • cjunior's avatar
      cjunior
      Icon for Nacreous rankNacreous

      Hello,

      I think you must loop on "webservers" group and interact on each "item":

      ---
      - name: BIG-IP SETUP
       hosts: lb
       connection: local
       gather_facts: false
       
       tasks:
       
       - name: CREATE NODES
         bigip_node:
           provider:
             server: X.X.X.X
             user: XXXX
             password: XXXXXXX
             server_port: 443
             validate_certs: false
           host: "{{hostvars[item].ansible_host}}"
           name: "{{hostvars[item].inventory_hostname}}"
         loop: "{{ groups[webservers] }}"

      Regards.

  • Hi cjunior

     

    You were spot on, thank you very much. Reading the various Ansible documentation and blog posts I convinced myself the hostvars must refer to an actual name in the inventory. But looking at it like this, it makes much more sense now.