aristanetworks / ansible-cvp

Ansible modules for Arista CloudVision
http://cvp.avd.sh
Apache License 2.0
66 stars 61 forks source link

How to get specific information on a cv_facts #309

Closed batchenr closed 3 years ago

batchenr commented 3 years ago

Hey I have been trying to run a playbook that shows specific information on a device, but none of the examples work for me and it ends up giving me all the information from all devices.

Containers by order of connection: Tenant -> HQ -> HQ-Users -> HQ-Users-Floors -> the device dw-k3-01 the specific device i want to test is : dw-k3-01 Device configlets name: dw-k3-01 (device and configrlet has the same name.

This is the playbooks i ran:

---
- name: Playbook to demonstrate cv_container module.
  hosts: cvp_servers
  connection: local
  gather_facts: no
  collections:
    - arista.cvp
  vars:
    containers_provision:
        Tenant:
          parent_container: Tenant
        HQ:
          parent_container: Tenant
        HQ-Users:
          parent_container: HQ
        HQ-Users-Floors:
          parent_container: HQ-Users
          configlets:
              - dw-k3-01
          devices:
              - dw-k3-01
  vars_files:
            - vars.yml
  tasks:
    - name: "Gather CVP facts from {{inventory_hostname}}"
      arista.cvp.cv_facts:
      register: facts

also this playbook :

---
- name: Playbook to demonstrate cv_container module.
  hosts: cvp_servers
  connection: local
  gather_facts: no
  collections:
    - arista.cvp
  vars:
  vars_files:
            - vars.yml
  tasks:
    - name: "collecting facts from CVP {{inventory_hostname}}"
      arista.cvp.cv_facts:
        facts:
          devices
    - name: "Print out facts from CVP"
      debug:
        msg: "{{item.name}}"
      with_items: "{{devices}}"

This will sort the information much better but still i will have to parse ansible output, if there is a ways without filtering ansible output i would rather do that.

Packages:

ansible==3.1.0
ansible-base==2.10.7
cvprac==1.0.5
httplib2==0.19.0
Jinja2==2.11.3
netaddr==0.8.0
requests==2.25.1
treelib==1.6.1

How to get the specific device facts only ?

titom73 commented 3 years ago

Hello @batchenr

To get device information using cv_facts, you should use this type of playbook: here we just print out FQDN field but you can adapt.

---
- name: cv_configlet unit testing
  hosts: cv_server
  connection: local
  gather_facts: false
  tasks:
    - name: Get CV Facts
      arista.cvp.cv_facts:
        facts:
          devices
      register: cv_facts

    - name: "Print out facts from CVP"
      debug:
        msg: "{{item.fqdn}}"
      when:
        - item.fqdn == 'DC1-L2LEAF1A.eve.emea.lab'
      loop: "{{cv_facts.ansible_facts.devices}}"

Note that cv_fact get information from Cloudvision and does not provide more information than what is provided by inventory/devices API call.

Besides that, if you want to configure configlets and container even for a specific device, you need to collect all facts to let ansible find mandatory information to send correct information to CV.