ansiblenetconf

Subscribe to NETCONF notifications using Ansible


Currently working on software update feature on embedded platform using NETCONF and I'd like to be able to use Ansible.

I was able to communicate with NETCONF server using Ansible to get configuration and to fire RPC but I didn't find anything about subscribing to notifications.

To better understand my issue here, each RPC (download, install and activate) works in two steps. First I call the RPC, starting the procedure which could take time and when it's over, I receive a notification telling me it's done and I can move on to the next step.

So, is it possible to subscribe to NETCONF notification and wait for it using Ansible ?


Solution

  • It appears there is no builtin Ansible module capable of subscribing to NETCONF notification but it can be done using a python script executed by the playbook.

    Here is the python code subscribing to a NETCONF notification :

    from ncclient import manager
    from ncclient.xml_ import to_ele
    import sys
    
    def main():
        # Variables
        hostname = sys.argv[1]
        port =  sys.argv[2]
        username = sys.argv[3]
        password = sys.argv[4]
        
        # Connection to NETCONF
        with manager.connect(host=hostname, port=port, username=username, password=password, hostkey_verify=False, allow_agent=False, look_for_keys=False) as nchandle:
            # Subscribe to software-download notifications
            sub_rpc = """
            <create-subscription xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">
                <filter type="subtree">
                    <download-event xmlns="urn:o-ran:software-management:1.0" />
                </filter>
            </create-subscription>
            """
            response = nchandle.dispatch(to_ele(sub_rpc))
            
            # Wait for notification
            while True:
                notif = nchandle.take_notification(timeout=30)
                if notif:
                    print(notif)
                    break
            
    main()
    

    Ansible task executing python script :

      - name: Wait for download notification
        ansible.builtin.script:
          cmd: ../python/software-download-notif.py {{ansible_host}} {{ansible_port}} {{ansible_user}} {{ansible_password}}
          executable: /usr/bin/python3
        delegate_to: 127.0.0.1