Role play games with Ansible for Nokia (Alcatel-Lucent) SR OS and Cisco IOS XR

Anton Karneliuk. IT & Network Expert / Architect / Manager; Project Manager; Entrepreneur; 2x CCIE (RS/SP) #49412; MSc Telecommunications.

Leave a Reply

Comment as a guest.

  1. Hello and thank you for the amazing automation articles.

    I’m trying to implement it in my lab and later in production.
    I’m using Juniper vRR and want to do deploy and Zero Touch Provisioning using Ansible.
    But i’m stuck with configuring management interface via ansible telnet module:
    “`
    – name: Configure management interface via telnet
    telnet:
    port: 3517
    user: root
    pause: 1
    login_prompt: ‘login: ‘
    prompts:
    – “[>|#]”
    command:
    – cli
    – edit
    – set system services ssh
    – set system root-authentication plain-text-password
    – Juniper
    – Juniper
    – set system services netconf rfc-compliant ssh port 830
    – set intermface em0 unit 0 familt inet address 192.168.0.111/24
    – commit
    “`

    I’m getting this error
    “`
    The full traceback is:
    Traceback (most recent call last):
    File “/usr/local/lib/python3.6/dist-packages/ansible/executor/task_executor.py”, line 138, in run
    res = self._execute()
    File “/usr/local/lib/python3.6/dist-packages/ansible/executor/task_executor.py”, line 576, in _execute
    result = self._handler.run(task_vars=variables)
    File “/usr/local/lib/python3.6/dist-packages/ansible/plugins/action/telnet.py”, line 58, in run
    tn.read_until(login_prompt)
    File “/usr/lib/python3.6/telnetlib.py”, line 302, in read_until
    i = self.cookedq.find(match)
    TypeError: a bytes-like object is required, not ‘AnsibleUnicode’

    fatal: [192.168.0.95]: FAILED! => {
    “msg”: “Unexpected failure during module execution.”,
    “stdout”: “”
    }
    “`

    And can’t figure out how to fix it.

    Ansible 2.6.0
    Python 3.6.5
    Ubuntu 18.04

    1. Hi Kirill,

      thanks for kind feedback. Have you used also magic_wand.py in advance? As I’ve written in article, when you connect to emulation of serial part “0.0.0.0:3517”, the device expects you press enter, before it shows you login prompt.

      BR,
      Anton

      1. Yes, I did use magic_wand.py but for some reason the script module didn’t work for me. It just tried to find magic_wand.py in ansible tmp directory which doesn’t exist. So I used a combination of copy and command modules.
        Telnet module seems not working with Python3 so for now i removed play with this module and rewrite magic_wand.py:
        “`
        #!/bin/env python3

        import telnetlib
        from time import sleep

        host = ‘0.0.0.0’
        port = 3517
        timeout = 10

        tn = telnetlib.Telnet(host, port, timeout)

        try:
        tn.write(‘\r\n’.encode(‘ascii’))
        sleep(0.5)
        tn.write(‘root\n’.encode(‘ascii’))
        sleep(0.5)
        tn.write(‘cli\n’.encode(‘ascii’))
        sleep(0.5)
        tn.write(‘edit\n’.encode(‘ascii’))
        sleep(0.5)
        tn.write(‘set system services ssh\n’.encode(‘ascii’))
        sleep(0.5)
        tn.write(‘set system root-authentication plain-text-password\n’.encode(‘ascii’))
        sleep(0.5)
        tn.write(‘Juniper\n’.encode(‘ascii’))
        sleep(0.5)
        tn.write(‘Juniper\n’.encode(‘ascii’))
        sleep(0.5)
        tn.write(‘set system services netconf rfc-compliant ssh port 830\n’.encode(‘ascii’))
        sleep(0.5)
        tn.write(‘set interface em0 unit 0 family inet address 192.168.0.111/24\n’.encode(‘ascii’))
        sleep(0.5)
        tn.write(‘commit\n’.encode(‘ascii’))
        sleep(0.5)
        print(tn.read_very_eager().decode(‘ascii’))
        except EOFError:
        pass
        “`

        This works but not so conveniently as your examples.
        If you tried this with new ansible which works with Python3 please let me know how did you make it work?

        1. Hi Kirill,

          Nope, I have used telnet module with Python 2.7. Have you tried to find something on github on telnet module problem with Python 3?
          Well, what you are doing is directly putting commands in Python, what definitely works 🙂

          BR,
          Anton

  2. With Ansible 2.5.0 and Python 2.7 combo include_vars and telnet seems working now but I run into an issue I can’t fix.

    VM is deployed perfectly fine and then the problem part of playbook is:
    “`
    – name: Copy magic_wand script to host
    copy:
    src: magic_wand.py
    dest: “/home/{{ lookup(‘env’, ‘USER’) }}/”
    become: no

    – name: Making router reachable via CLI
    command: python “/home/{{ lookup(‘env’, ‘USER’) }}/magic_wand.py”

    – name: Configure management interface via telnet
    telnet:
    port: 3517
    user: root
    pause: 0.5
    login_prompt: ‘login: ‘
    prompts:
    – “[>|#]”
    command:
    – cli
    – edit
    – set system services ssh
    – set system root-authentication plain-text-password
    – Juniper
    – Juniper
    – set system services netconf rfc-compliant ssh port 830
    – set interface em0 unit 0 family inet address 192.168.0.111/24
    – commit
    “`

    magic_wand.py is copied and executed but telnet can’t do it’s work. It just stuck (very long output):
    “`
    PLAYBOOK: playbook1_deploy.yml *****************************************************************************************************************************
    2 plays in playbook1_deploy.yml

    PLAY [192.168.0.95] ****************************************************************************************************************************************
    META: ran handlers

    TASK [kvm : Include vars] **********************************************************************************************************************************
    task path: /home/horseinthesky/netprog/Ansible/05_deploy_JunOS_vRR/roles/kvm/tasks/main.yml:1
    ok: [192.168.0.95] => {
    “ansible_facts”: {
    “kvm”: {
    “console”: {
    “host”: “0.0.0.0”,
    “port”: 3517,
    “protocol”: “telnet”
    },
    “cpu”: 4,
    “interfaces”: {
    “em0”: {
    “bridge”: “vrr-ext”,
    “type”: “virtio”
    }
    },
    “memory”: 8388608,
    “name”: “ansible_vrr”,
    “product”: “VRR”,
    “vendor”: “Juniper”,
    “version”: 16.2
    }
    },
    “ansible_included_var_files”: [
    “/home/horseinthesky/netprog/Ansible/05_deploy_JunOS_vRR/roles/kvm/vars/kvm.yml”
    ],
    “changed”: false
    }

    TASK [kvm : PERFORMING ONBOARDING] *************************************************************************************************************************
    task path: /home/horseinthesky/netprog/Ansible/05_deploy_JunOS_vRR/roles/kvm/tasks/main.yml:5
    included: /home/horseinthesky/netprog/Ansible/05_deploy_JunOS_vRR/roles/kvm/tasks/on.yml for 192.168.0.95

    TASK [kvm : Copy magic_wand script to host] ****************************************************************************************************************
    task path: /home/horseinthesky/netprog/Ansible/05_deploy_JunOS_vRR/roles/kvm/tasks/on.yml:26
    ESTABLISH SSH CONNECTION FOR USER: horseinthesky
    SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=horseinthesky -o ConnectTimeout=10 -o ControlPath=/home/horseinthesky/.ansible/cp/1b79b08566 192.168.0.95 ‘/bin/sh -c ‘”‘”‘echo ~horseinthesky && sleep 0′”‘””
    (0, ‘/home/horseinthesky\n’, ”)
    ESTABLISH SSH CONNECTION FOR USER: horseinthesky
    SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=horseinthesky -o ConnectTimeout=10 -o ControlPath=/home/horseinthesky/.ansible/cp/1b79b08566 192.168.0.95 ‘/bin/sh -c ‘”‘”‘( umask 77 && mkdir -p “` echo /home/horseinthesky/.ansible/tmp/ansible-tmp-1532667583.34-205278060753962 `” && echo ansible-tmp-1532667583.34-205278060753962=”` echo /home/horseinthesky/.ansible/tmp/ansible-tmp-1532667583.34-205278060753962 `” ) && sleep 0′”‘””
    (0, ‘ansible-tmp-1532667583.34-205278060753962=/home/horseinthesky/.ansible/tmp/ansible-tmp-1532667583.34-205278060753962\n’, ”)
    Using module file /home/horseinthesky/venv/py2/local/lib/python2.7/site-packages/ansible/modules/files/stat.py
    PUT /home/horseinthesky/.ansible/tmp/ansible-local-2810rliHQ2/tmp6uflqo TO /home/horseinthesky/.ansible/tmp/ansible-tmp-1532667583.34-205278060753962/stat.py
    SSH: EXEC sshpass -d10 sftp -o BatchMode=no -b – -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=horseinthesky -o ConnectTimeout=10 -o ControlPath=/home/horseinthesky/.ansible/cp/1b79b08566 ‘[192.168.0.95]’
    (0, ‘sftp> put /home/horseinthesky/.ansible/tmp/ansible-local-2810rliHQ2/tmp6uflqo /home/horseinthesky/.ansible/tmp/ansible-tmp-1532667583.34-205278060753962/stat.py\n’, ”)
    ESTABLISH SSH CONNECTION FOR USER: horseinthesky
    SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=horseinthesky -o ConnectTimeout=10 -o ControlPath=/home/horseinthesky/.ansible/cp/1b79b08566 192.168.0.95 ‘/bin/sh -c ‘”‘”‘chmod u+x /home/horseinthesky/.ansible/tmp/ansible-tmp-1532667583.34-205278060753962/ /home/horseinthesky/.ansible/tmp/ansible-tmp-1532667583.34-205278060753962/stat.py && sleep 0′”‘””
    (0, ”, ”)
    ESTABLISH SSH CONNECTION FOR USER: horseinthesky
    SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=horseinthesky -o ConnectTimeout=10 -o ControlPath=/home/horseinthesky/.ansible/cp/1b79b08566 -tt 192.168.0.95 ‘/bin/sh -c ‘”‘”‘/usr/bin/python /home/horseinthesky/.ansible/tmp/ansible-tmp-1532667583.34-205278060753962/stat.py && sleep 0′”‘””
    (0, ‘\r\n{“invocation”: {“module_args”: {“checksum_algorithm”: “sha1”, “get_checksum”: true, “follow”: false, “checksum_algo”: “sha1”, “path”: “/home/horseinthesky/magic_wand.py”, “get_md5”: null, “get_mime”: true, “get_attributes”: true}}, “stat”: {“charset”: “us-ascii”, “uid”: 1000, “exists”: true, “attr_flags”: “e”, “woth”: false, “isreg”: true, “device_type”: 0, “mtime”: 1532667203.275627, “block_size”: 4096, “inode”: 132506, “isgid”: false, “size”: 176, “executable”: false, “isuid”: false, “readable”: true, “version”: “18446744072840967349”, “pw_name”: “horseinthesky”, “gid”: 1000, “ischr”: false, “wusr”: true, “writeable”: true, “mimetype”: “text/plain”, “blocks”: 8, “xoth”: false, “islnk”: false, “nlink”: 1, “issock”: false, “rgrp”: true, “gr_name”: “horseinthesky”, “path”: “/home/horseinthesky/magic_wand.py”, “xusr”: false, “atime”: 1532667203.9316387, “isdir”: false, “ctime”: 1532667203.5236313, “isblk”: false, “wgrp”: true, “checksum”: “b0efca8ecc30ce9f3bbcb486f854d888efefd8f8”, “dev”: 64768, “roth”: true, “isfifo”: false, “mode”: “0664”, “xgrp”: false, “rusr”: true, “attributes”: [“extents”]}, “changed”: false}\r\n’, ‘Shared connection to 192.168.0.95 closed.\r\n’)
    Using module file /home/horseinthesky/venv/py2/local/lib/python2.7/site-packages/ansible/modules/files/file.py
    PUT /home/horseinthesky/.ansible/tmp/ansible-local-2810rliHQ2/tmprxNSSr TO /home/horseinthesky/.ansible/tmp/ansible-tmp-1532667583.34-205278060753962/file.py
    SSH: EXEC sshpass -d10 sftp -o BatchMode=no -b – -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=horseinthesky -o ConnectTimeout=10 -o ControlPath=/home/horseinthesky/.ansible/cp/1b79b08566 ‘[192.168.0.95]’
    (0, ‘sftp> put /home/horseinthesky/.ansible/tmp/ansible-local-2810rliHQ2/tmprxNSSr /home/horseinthesky/.ansible/tmp/ansible-tmp-1532667583.34-205278060753962/file.py\n’, ”)
    ESTABLISH SSH CONNECTION FOR USER: horseinthesky
    SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=horseinthesky -o ConnectTimeout=10 -o ControlPath=/home/horseinthesky/.ansible/cp/1b79b08566 192.168.0.95 ‘/bin/sh -c ‘”‘”‘chmod u+x /home/horseinthesky/.ansible/tmp/ansible-tmp-1532667583.34-205278060753962/ /home/horseinthesky/.ansible/tmp/ansible-tmp-1532667583.34-205278060753962/file.py && sleep 0′”‘””
    (0, ”, ”)
    ESTABLISH SSH CONNECTION FOR USER: horseinthesky
    SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=horseinthesky -o ConnectTimeout=10 -o ControlPath=/home/horseinthesky/.ansible/cp/1b79b08566 -tt 192.168.0.95 ‘/bin/sh -c ‘”‘”‘/usr/bin/python /home/horseinthesky/.ansible/tmp/ansible-tmp-1532667583.34-205278060753962/file.py && sleep 0′”‘””
    (0, ‘\r\n{“group”: “horseinthesky”, “uid”: 1000, “changed”: false, “owner”: “horseinthesky”, “state”: “file”, “gid”: 1000, “mode”: “0664”, “path”: “/home/horseinthesky/magic_wand.py”, “invocation”: {“module_args”: {“directory_mode”: null, “force”: false, “remote_src”: null, “_original_basename”: “magic_wand.py”, “path”: “/home/horseinthesky/magic_wand.py”, “owner”: null, “follow”: true, “group”: null, “unsafe_writes”: null, “state”: “file”, “content”: null, “serole”: null, “setype”: null, “dest”: “/home/horseinthesky/”, “selevel”: null, “regexp”: null, “src”: null, “seuser”: null, “recurse”: false, “_diff_peek”: null, “delimiter”: null, “mode”: null, “attributes”: null, “backup”: null}}, “diff”: {“after”: {“path”: “/home/horseinthesky/magic_wand.py”}, “before”: {“path”: “/home/horseinthesky/magic_wand.py”}}, “size”: 176}\r\n’, ‘Shared connection to 192.168.0.95 closed.\r\n’)
    ESTABLISH SSH CONNECTION FOR USER: horseinthesky
    SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=horseinthesky -o ConnectTimeout=10 -o ControlPath=/home/horseinthesky/.ansible/cp/1b79b08566 192.168.0.95 ‘/bin/sh -c ‘”‘”‘rm -f -r /home/horseinthesky/.ansible/tmp/ansible-tmp-1532667583.34-205278060753962/ > /dev/null 2>&1 && sleep 0′”‘””
    (0, ”, ”)
    ok: [192.168.0.95] => {
    “changed”: false,
    “checksum”: “b0efca8ecc30ce9f3bbcb486f854d888efefd8f8”,
    “dest”: “/home/horseinthesky/magic_wand.py”,
    “diff”: {
    “after”: {
    “path”: “/home/horseinthesky/magic_wand.py”
    },
    “before”: {
    “path”: “/home/horseinthesky/magic_wand.py”
    }
    },
    “gid”: 1000,
    “group”: “horseinthesky”,
    “invocation”: {
    “module_args”: {
    “_diff_peek”: null,
    “_original_basename”: “magic_wand.py”,
    “attributes”: null,
    “backup”: null,
    “content”: null,
    “delimiter”: null,
    “dest”: “/home/horseinthesky/”,
    “directory_mode”: null,
    “follow”: true,
    “force”: false,
    “group”: null,
    “mode”: null,
    “owner”: null,
    “path”: “/home/horseinthesky/magic_wand.py”,
    “recurse”: false,
    “regexp”: null,
    “remote_src”: null,
    “selevel”: null,
    “serole”: null,
    “setype”: null,
    “seuser”: null,
    “src”: null,
    “state”: “file”,
    “unsafe_writes”: null
    }
    },
    “mode”: “0664”,
    “owner”: “horseinthesky”,
    “path”: “/home/horseinthesky/magic_wand.py”,
    “size”: 176,
    “state”: “file”,
    “uid”: 1000
    }

    TASK [kvm : Making router reachable via CLI] ***************************************************************************************************************
    task path: /home/horseinthesky/netprog/Ansible/05_deploy_JunOS_vRR/roles/kvm/tasks/on.yml:32
    ESTABLISH SSH CONNECTION FOR USER: horseinthesky
    SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=horseinthesky -o ConnectTimeout=10 -o ControlPath=/home/horseinthesky/.ansible/cp/1b79b08566 192.168.0.95 ‘/bin/sh -c ‘”‘”‘echo ~horseinthesky && sleep 0′”‘””
    (0, ‘/home/horseinthesky\n’, ”)
    ESTABLISH SSH CONNECTION FOR USER: horseinthesky
    SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=horseinthesky -o ConnectTimeout=10 -o ControlPath=/home/horseinthesky/.ansible/cp/1b79b08566 192.168.0.95 ‘/bin/sh -c ‘”‘”‘( umask 77 && mkdir -p “` echo /home/horseinthesky/.ansible/tmp/ansible-tmp-1532667584.21-187334804190948 `” && echo ansible-tmp-1532667584.21-187334804190948=”` echo /home/horseinthesky/.ansible/tmp/ansible-tmp-1532667584.21-187334804190948 `” ) && sleep 0′”‘””
    (0, ‘ansible-tmp-1532667584.21-187334804190948=/home/horseinthesky/.ansible/tmp/ansible-tmp-1532667584.21-187334804190948\n’, ”)
    Using module file /home/horseinthesky/venv/py2/local/lib/python2.7/site-packages/ansible/modules/commands/command.py
    PUT /home/horseinthesky/.ansible/tmp/ansible-local-2810rliHQ2/tmpqTH0wT TO /home/horseinthesky/.ansible/tmp/ansible-tmp-1532667584.21-187334804190948/command.py
    SSH: EXEC sshpass -d10 sftp -o BatchMode=no -b – -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=horseinthesky -o ConnectTimeout=10 -o ControlPath=/home/horseinthesky/.ansible/cp/1b79b08566 ‘[192.168.0.95]’
    (0, ‘sftp> put /home/horseinthesky/.ansible/tmp/ansible-local-2810rliHQ2/tmpqTH0wT /home/horseinthesky/.ansible/tmp/ansible-tmp-1532667584.21-187334804190948/command.py\n’, ”)
    ESTABLISH SSH CONNECTION FOR USER: horseinthesky
    SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=horseinthesky -o ConnectTimeout=10 -o ControlPath=/home/horseinthesky/.ansible/cp/1b79b08566 192.168.0.95 ‘/bin/sh -c ‘”‘”‘chmod u+x /home/horseinthesky/.ansible/tmp/ansible-tmp-1532667584.21-187334804190948/ /home/horseinthesky/.ansible/tmp/ansible-tmp-1532667584.21-187334804190948/command.py && sleep 0′”‘””
    (0, ”, ”)
    ESTABLISH SSH CONNECTION FOR USER: horseinthesky
    SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=horseinthesky -o ConnectTimeout=10 -o ControlPath=/home/horseinthesky/.ansible/cp/1b79b08566 -tt 192.168.0.95 ‘/bin/sh -c ‘”‘”‘sudo -H -S -p “[sudo via ansible, key=jvttixlvptnqhlfrcusmiwxhmevshnww] password: ” -u root /bin/sh -c ‘”‘”‘”‘”‘”‘”‘”‘”‘echo BECOME-SUCCESS-jvttixlvptnqhlfrcusmiwxhmevshnww; /usr/bin/python /home/horseinthesky/.ansible/tmp/ansible-tmp-1532667584.21-187334804190948/command.py'”‘”‘”‘”‘”‘”‘”‘”‘ && sleep 0′”‘””
    Escalation succeeded
    (0, ‘\r\n\r\n{“changed”: true, “end”: “2018-07-27 07:59:44.500158”, “stdout”: “”, “cmd”: [“python”, “/home/horseinthesky/magic_wand.py”], “rc”: 0, “start”: “2018-07-27 07:59:44.486317”, “stderr”: “”, “delta”: “0:00:00.013841”, “invocation”: {“module_args”: {“warn”: true, “executable”: null, “_uses_shell”: false, “_raw_params”: “python \\”/home/horseinthesky/magic_wand.py\\””, “removes”: null, “argv”: null, “creates”: null, “chdir”: null, “stdin”: null}}}\r\n’, ‘Shared connection to 192.168.0.95 closed.\r\n’)
    ESTABLISH SSH CONNECTION FOR USER: horseinthesky
    SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=horseinthesky -o ConnectTimeout=10 -o ControlPath=/home/horseinthesky/.ansible/cp/1b79b08566 192.168.0.95 ‘/bin/sh -c ‘”‘”‘rm -f -r /home/horseinthesky/.ansible/tmp/ansible-tmp-1532667584.21-187334804190948/ > /dev/null 2>&1 && sleep 0′”‘””
    (0, ”, ”)
    changed: [192.168.0.95] => {
    “changed”: true,
    “cmd”: [
    “python”,
    “/home/horseinthesky/magic_wand.py”
    ],
    “delta”: “0:00:00.013841”,
    “end”: “2018-07-27 07:59:44.500158”,
    “invocation”: {
    “module_args”: {
    “_raw_params”: “python \”/home/horseinthesky/magic_wand.py\””,
    “_uses_shell”: false,
    “argv”: null,
    “chdir”: null,
    “creates”: null,
    “executable”: null,
    “removes”: null,
    “stdin”: null,
    “warn”: true
    }
    },
    “rc”: 0,
    “start”: “2018-07-27 07:59:44.486317”,
    “stderr”: “”,
    “stderr_lines”: [],
    “stdout”: “”,
    “stdout_lines”: []
    }

    TASK [kvm : Configure management interface via telnet] *****************************************************************************************************
    task path: /home/horseinthesky/netprog/Ansible/05_deploy_JunOS_vRR/roles/kvm/tasks/on.yml:38
    “`

    If i try connect to VM manually i would not be succeded. Port is in use untul i Ctrl+C playbook.
    “`
    horseinthesky@ubuntu:~$ telnet 0.0.0.0 3517
    Trying 0.0.0.0…
    Connected to 0.0.0.0.
    Escape character is ‘^]’.

    “`

    I guess it do not see login prompt and is waiting for another “Enter”. Do you have any ideas why?

Read Next

Sliding Sidebar