cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2207
Views
0
Helpful
12
Replies

Delete device from group via jsonrpc / Ansible

mboehm001
Level 1
Level 1

Hi,

 

how can I delete a device from a group using the jsonrpc or the Ansible-module "nso_config" to be exact?

 

I was able to delete other configuration, like devices using the "__state: absent"-statement but this is not working for devices in groups.

 

In other words im looking for an API-equivalent for:

 

delete devices device-group somegroup device-name somedevice

How would the API-Call / yaml for nso_config look like?

 

We are using NSO 5.3.

 

Thanks a lot.

12 Replies 12

mboehm001
Level 1
Level 1

Bump as I stil have no solution.

I will admit that I am not at all an expert on this, but it ought to work, what error do you get? 

Hi,

 

thanks for your reply. I have the following group in NSO:

 

user@ncs% show devices device-group somegroup 
device-name [ test-mb ];

I want to remove "test-mb" from the group via REST-API / jsonrpc using the Ansible-module nso_config.

 

I'll post my approaches with error messages later on.

Minimum example:

 

This works (deleting a whole group):

 

---
- hosts: all
  connection: local
  gather_facts: no
  environment:
    http_proxy: ''
    https_proxy: ''
  vars_files:
    - ../roles/nso_import/vars/main.yml 
  tasks:
    - name: Add devices to Cisco NSO
      nso_config:
        url: "{{ nso_config.protocol }}://{{ ansible_host }}:{{ nso_config.api_port }}/{{ nso_config.endpoint }}"
        username: "{{ nso_config.username }}"
        password: "{{ nso_config.password }}"
        validate_certs: "{{ nso_config.validate }}"
        timeout: "{{ nso_config.timeout }}"
        data:
          tailf-ncs:devices:
            device-group:
              - name: somegroup2
                __state: absent

This does not work (deleting a single element of the group):

 

---
- hosts: all
  connection: local
  gather_facts: no
  environment:
    http_proxy: ''
    https_proxy: ''
  vars_files:
    - ../roles/nso_import/vars/main.yml 
  tasks:
    - name: Add devices to Cisco NSO
      nso_config:
        url: "{{ nso_config.protocol }}://{{ ansible_host }}:{{ nso_config.api_port }}/{{ nso_config.endpoint }}"
        username: "{{ nso_config.username }}"
        password: "{{ nso_config.password }}"
        validate_certs: "{{ nso_config.validate }}"
        timeout: "{{ nso_config.timeout }}"
        data:
          tailf-ncs:devices:
            device-group:
              - name: somegroup
                test-mb:
                  __state: absent


####################################################

fatal: [stnnso1]: FAILED! => {"changed": false, "msg": "no child in /ncs:devices/device-group{somegroup} with name test-mb. children ncs:name,ncs:location,ncs:device-name,ncs:device-group,ncs:member,ncs:ned-id,al:alarm-summary,ncs:connect,ncs:sync-to,ncs:sync-from,ncs:check-sync,ncs:check-yang-modules,ncs:fetch-ssh-host-keys,ncs:apply-template"}

Thanks for your help!

Minimal example:

 

---
- hosts: all
  connection: local
  gather_facts: no
  environment:
    http_proxy: ''
    https_proxy: ''
  vars_files:
    - ../roles/nso_import/vars/main.yml 
  tasks:
    - name: Delete from a group
      nso_config:
        url: "{{ nso_config.protocol }}://{{ ansible_host }}:{{ nso_config.api_port }}/{{ nso_config.endpoint }}"
        username: "{{ nso_config.username }}"
        password: "{{ nso_config.password }}"
        validate_certs: "{{ nso_config.validate }}"
        timeout: "{{ nso_config.timeout }}"
        data:
          tailf-ncs:devices:
            device-group:
              - name: somegroup
                test-mb:
                  __state: absent

Error:

fatal: [nso]: FAILED! => {"changed": false, "msg": "no child in /ncs:devices/device-group{somegroup} with name test-mb. children ncs:name,ncs:location,ncs:device-name,ncs:device-group,ncs:member,ncs:ned-id,al:alarm-summary,ncs:connect,ncs:sync-to,ncs:sync-from,ncs:check-sync,ncs:check-yang-modules,ncs:fetch-ssh-host-keys,ncs:apply-template"}

Any idea what I'm doing wrong. Thanks!

 

Haven't tested it myself, but according to the error it's expecting one more child.  Try "device-name"

 

data:
  tailf-ncs:devices:
    device-group:
    - name: somegroup
      device-name:
      - test-mb
__state:absent

Hi, thanks for your answer.

I don't get an error with your syntax but unfortunately the host is not deleted from the group. Ansible detects no change, "test-mb" remains in "somegroup" in NSO.

 

ansible-playbook -i stnnso1.nso.telekom.de, test.yml 

PLAY [all] *******************************************************************************************************************************************************************************************************************************************

TASK [Delete from a group] ***************************************************************************************************************************************************************************************************************************
ok: [nso]

PLAY RECAP *******************************************************************************************************************************************************************************************************************************************
stnnso1.nso.telekom.de     : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Any other hints what I could try out?

Just had a chance to try it out.  I have two XE netsims and two XR netsims.  I added XE0 to the device group.  If I try to remove it, nothing happens.  If I modify the group and add XE1 with a "__state: absent" in there, then it removes XE0.  If I go back and replace it with XE1, it removes XE0.  So basically what seems to be happening is that whatever is in the list is left. And if it's not in the list it's removed.

 

State of device group:

 

admin@ncs# show running-config devices device-group somegroup
devices device-group somegroup
 device-name [ xe0 xe1 xr0 xr1 ]
!

Play:

 

 

- name: Delete a device from a group
  nso_config:
    url: "{{ api.url }}"
    username: "{{ api.username }}"
    password: "{{ api.password }}"
    data:
      tailf-ncs:devices:
        device-group:
        - name: somegroup
          device-name:
          - xe0
          __state: absent

Result:

 

admin@ncs# show running-config devices device-group somegroup
devices device-group somegroup
 device-name [ xe0 ]
!

 

So it looks like whatever is in the leaf-list "device-name" is left, and anything that is absent from the list is removed.  Seems to me this is the opposite behavior then expected.  Bug??

 

More data:

 

Before:

admin@ncs# show running-config devices device-group somegroup
devices device-group somegroup
 device-name [ xe0 xe1 xr0 xr1 ]
!

Play:

- name: Delete a device from a group
  nso_config:
    url: "{{ api.url }}"
    username: "{{ api.username }}"
    password: "{{ api.password }}"
    data:
      tailf-ncs:devices:
        device-group:
        - name: somegroup
          device-name:
          - xr0
          - xr1
          __state: absent


        

Result:

admin@ncs# show running-config devices device-group somegroup
devices device-group somegroup
 device-name [ xr0 xr1 ]
!

 

Thank you for your effort and validating my results!

 

I know that the nso_* Ansible-modules were contributed by Cisco. Are they still maintained? Who could look into it and possible provide a patch that could be included in one of the next Ansible-versions?

rogaglia
Cisco Employee
Cisco Employee

I believe the issue is that you are dealing with a leaf-list and not a list.

Can you check that your ansible version is larger than 2.6 per this commit?:

https://github.com/ansible/ansible/pull/36583

 

Hi,

 

Ansible is brand-new:

 

# ansible --version
ansible 2.9.10
  config file = /var/www/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.7/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.7.3 (default, Dec 20 2019, 18:57:59) [GCC 8.3.0]

Is @cnasten still maintaining the module?

 

__state must be indented under the key of the list item you wish to remove.
Ansible reports "ok" with nothing changed:

          - xr1
          __state: absent

Ansible reports "modified" and item is deleted:

          - xr1
            __state: absent