Junos Automation (Scripting)
Highlighted
Junos Automation (Scripting)

Ansible and Junos , Python error ,   "module_stdout": "/usr/bin/python2.7: not found\r\n",

‎03-14-2019 02:03 AM

I have been fighting with this for days now

This is my first ever dabble with ansible and I cannot get the simplest playbooks or even a ping to work.

Centos 7,
ansible 2.4.2.0
Juniper.junos module installed into ansible

I’m testing with a vSRX

 

I've read the day 1 guides 

 

This is my error for every playbook I try to run

 

[root@localhost bin]# ansible -m ping all -vvv

ansible 2.4.2.0

  config file = /etc/ansible/ansible.cfg

  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']

  ansible python module location = /usr/lib/python2.7/site-packages/ansible

  executable location = /usr/bin/ansible

  python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]

Using /etc/ansible/ansible.cfg as config file

Parsed /etc/ansible/hosts inventory source with ini plugin

META: ran handlers

Using module file /usr/lib/python2.7/site-packages/ansible/modules/system/ping.py

<172.27.233.17> ESTABLISH SSH CONNECTION FOR USER: root

<172.27.233.17> SSH: EXEC sshpass -d11 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=root -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/7830714a9a 172.27.233.17 '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''

<172.27.233.17> (0, '/root\n', '')

<172.27.233.17> ESTABLISH SSH CONNECTION FOR USER: root

<172.27.233.17> SSH: EXEC sshpass -d11 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=root -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/7830714a9a 172.27.233.17 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1552552686.33-210239546229231 `" && echo ansible-tmp-1552552686.33-210239546229231="` echo /root/.ansible/tmp/ansible-tmp-1552552686.33-210239546229231 `" ) && sleep 0'"'"''

<172.27.233.17> (0, 'ansible-tmp-1552552686.33-210239546229231=/root/.ansible/tmp/ansible-tmp-1552552686.33-210239546229231\n', '')

<172.27.233.17> PUT /tmp/tmp2WWXJi TO /root/.ansible/tmp/ansible-tmp-1552552686.33-210239546229231/ping.py

<172.27.233.17> SSH: EXEC sshpass -d11 sftp -o BatchMode=no -b - -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=root -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/7830714a9a '[172.27.233.17]'

<172.27.233.17> (0, 'sftp> put /tmp/tmp2WWXJi /root/.ansible/tmp/ansible-tmp-1552552686.33-210239546229231/ping.py\n', '')

<172.27.233.17> ESTABLISH SSH CONNECTION FOR USER: root

<172.27.233.17> SSH: EXEC sshpass -d11 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=root -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/7830714a9a 172.27.233.17 '/bin/sh -c '"'"'chmod u+x /root/.ansible/tmp/ansible-tmp-1552552686.33-210239546229231/ /root/.ansible/tmp/ansible-tmp-1552552686.33-210239546229231/ping.py && sleep 0'"'"''

<172.27.233.17> (0, '', '')

<172.27.233.17> ESTABLISH SSH CONNECTION FOR USER: root

<172.27.233.17> SSH: EXEC sshpass -d11 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=root -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/7830714a9a -tt 172.27.233.17 '/bin/sh -c '"'"'/usr/bin/python2.7 /root/.ansible/tmp/ansible-tmp-1552552686.33-210239546229231/ping.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1552552686.33-210239546229231/" > /dev/null 2>&1 && sleep 0'"'"''

<172.27.233.17> (0, '/usr/bin/python2.7: not found\r\n', 'Shared connection to 172.27.233.17 closed.\r\n')

172.27.233.17 | FAILED! => {

    "changed": false,

    "module_stderr": "Shared connection to 172.27.233.17 closed.\r\n",

    "module_stdout": "/usr/bin/python2.7: not found\r\n",

    "msg": "MODULE FAILURE",

    "rc": 0

}

 

 

So its saying python cannot be found but look 

[root@localhost bin]# //usr/bin/python2.7

Python 2.7.5 (default, Oct 30 2018, 23:45:53)

[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>>

 

It looks like ansible usually runs something python on the remote device but this is not the case for network devices , surely we cannot be expected to install python on switches and routers ?. 

 

 My example playbook dies in the same way

[root@localhost ansible]# cat  uptime3.yaml

---

- name: Get device uptime

  hosts:

    - VSRX1

  roles:

    - Juniper.junos

  connection: local

  gather_facts: no

 

  vars_prompt:

    - name: username

      prompt: Junos Username

      private: no

 

    - name: password

      prompt: Junos Password

      private: yes

 

  tasks:

    - name: get uptime using galaxy module

      juniper_junos_command:

        commands:

          - show system uptime

        provider:

          host: "{{ ansible_host }}"

          port: 22

          user: "{{ username }}"

          passwd: "{{ password }}"

      register: uptime

 

    # - name: display uptimes (Python style)

    #     #   debug:

    #         #     var: uptime['stdout_lines']

    #

    #             - name: display uptimes (Ansible shortcut)

    #                   debug:

    #                           var: uptime.stdout_lines

 

 

 

[root@localhost ansible]#  ansible-playbook  uptime3.yaml -vvv

ansible-playbook 2.4.2.0

  config file = /etc/ansible/ansible.cfg

  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']

  ansible python module location = /usr/lib/python2.7/site-packages/ansible

  executable location = /usr/bin/ansible-playbook

  python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]

Using /etc/ansible/ansible.cfg as config file

Parsed /etc/ansible/hosts inventory source with ini plugin

 

PLAYBOOK: uptime3.yaml ********************************************************************************************************************************************

1 plays in uptime3.yaml

Junos Username: root

Junos Password:

 

PLAY [Get device uptime] ******************************************************************************************************************************************

META: ran handlers

 

TASK [get uptime using galaxy module] *****************************************************************************************************************************

task path: /etc/ansible/uptime3.yaml:20

Using module file /etc/ansible/roles/Juniper.junos/library/juniper_junos_command.py

<172.27.233.17> ESTABLISH SSH CONNECTION FOR USER: root

<172.27.233.17> SSH: EXEC sshpass -d11 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=root -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/7830714a9a 172.27.233.17 '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''

<172.27.233.17> (0, '/root\n', '')

<172.27.233.17> ESTABLISH SSH CONNECTION FOR USER: root

<172.27.233.17> SSH: EXEC sshpass -d11 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=root -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/7830714a9a 172.27.233.17 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1552553226.63-178839703888832 `" && echo ansible-tmp-1552553226.63-178839703888832="` echo /root/.ansible/tmp/ansible-tmp-1552553226.63-178839703888832 `" ) && sleep 0'"'"''

<172.27.233.17> (0, 'ansible-tmp-1552553226.63-178839703888832=/root/.ansible/tmp/ansible-tmp-1552553226.63-178839703888832\n', '')

<172.27.233.17> PUT /tmp/tmpowZMu1 TO /root/.ansible/tmp/ansible-tmp-1552553226.63-178839703888832/juniper_junos_command.py

<172.27.233.17> SSH: EXEC sshpass -d11 sftp -o BatchMode=no -b - -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=root -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/7830714a9a '[172.27.233.17]'

<172.27.233.17> (0, 'sftp> put /tmp/tmpowZMu1 /root/.ansible/tmp/ansible-tmp-1552553226.63-178839703888832/juniper_junos_command.py\n', '')

<172.27.233.17> ESTABLISH SSH CONNECTION FOR USER: root

<172.27.233.17> SSH: EXEC sshpass -d11 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=root -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/7830714a9a 172.27.233.17 '/bin/sh -c '"'"'chmod u+x /root/.ansible/tmp/ansible-tmp-1552553226.63-178839703888832/ /root/.ansible/tmp/ansible-tmp-1552553226.63-178839703888832/juniper_junos_command.py && sleep 0'"'"''

<172.27.233.17> (0, '', '')

<172.27.233.17> ESTABLISH SSH CONNECTION FOR USER: root

<172.27.233.17> SSH: EXEC sshpass -d11 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=root -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/7830714a9a -tt 172.27.233.17 '/bin/sh -c '"'"'/usr/bin/python2.7 /root/.ansible/tmp/ansible-tmp-1552553226.63-178839703888832/juniper_junos_command.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1552553226.63-178839703888832/" > /dev/null 2>&1 && sleep 0'"'"''

<172.27.233.17> (0, '/usr/bin/python2.7: not found\r\n', 'Shared connection to 172.27.233.17 closed.\r\n')

fatal: [172.27.233.17]: FAILED! => {

    "changed": false,

    "module_stderr": "Shared connection to 172.27.233.17 closed.\r\n",

    "module_stdout": "/usr/bin/python2.7: not found\r\n",

    "msg": "MODULE FAILURE",

    "rc": 0

}

        to retry, use: --limit @/etc/ansible/uptime3.retry

 

PLAY RECAP ********************************************************************************************************************************************************

172.27.233.17              : ok=0    changed=0    unreachable=0    failed=1  

 

[root@localhost ansible]# ^C

 

Any pointers at all would be appreciated 

4 REPLIES 4
Junos Automation (Scripting)

Re: Ansible and Junos , Python error ,   "module_stdout": "/usr/bin/python2.7: not found\r\n",

‎03-14-2019 04:15 AM

Hi Simon,

 

Please try to add Python to the path variable on the host and give it a shot:

https://www.digitalocean.com/community/tutorials/how-to-set-up-python-2-7-6-and-3-3-3-on-centos-6-4

 

Hope this helps.

 

Regards,
-r.

--------------------------------------------------

If this solves your problem, please mark this post as "Accepted Solution."
Kudos are always appreciated Smiley Happy.

 

Junos Automation (Scripting)
Solution
Accepted by topic author simon.bingham
‎03-14-2019 10:42 AM

Re: Ansible and Junos , Python error ,   "module_stdout": "/usr/bin/python2.7: not found\r\n",

‎03-14-2019 07:51 AM

Hi Simon,

 

I've tried the same steps and config. For me it works fine. However, I see some differences:

 


@simon.bingham wrote:

 

task path: /etc/ansible/uptime3.yaml:20

Using module file /etc/ansible/roles/Juniper.junos/library/juniper_junos_command.py

<172.27.233.17> ESTABLISH SSH CONNECTION FOR USER: root

<172.27.233.17> SSH: EXEC sshpass -d11 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o User=root -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/7830714a9a 172.27.233.17 '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''

<172.27.233.17> (0, '/root\n', '')


In your case ansible establishes SSH session with 172.27.233.17 (vSRX, right?) and tries to upload and run .py script there.

In my case I see "ESABLISH LOCAL CONNECTION" and EXEC without "SSH:" (local)

task path: /home/ab/aaa/uptime.yaml:20
<10.219.37.14> ESTABLISH LOCAL CONNECTION FOR USER: ab
<10.219.37.14> EXEC /bin/sh -c 'echo ~ab && sleep 0'

I'm not sure what's the exact reason for that on your setup, but here's a couple of things to check/try:

  • you use "config file = /etc/ansible/ansible.cfg". Check what does it have in there. On my machine it's a default config file generated by installing ansible several years ago.
  • same for inventory file which is "/etc/ansible/hosts" for you
  • use a different working directory with simple "ansible.cfg" and inventory as suggested in DAY1 book

Contents of my working dir:

ab@VB:~/aaa$ pwd
/home/ab/aaa

ab@VB:~/aaa$ ls -l
total 16
-rw-rw-r-- 1 ab ab  59 mrt 14 14:59 ansible.cfg
-rw-rw-r-- 1 ab ab  34 mrt 14 15:01 inventory
-rw-rw-r-- 1 ab ab   4 mrt 14 15:37 uptime.retry
-rw-rw-r-- 1 ab ab 518 mrt 14 15:13 uptime.yaml

ab@VB:~/aaa$ cat ansible.cfg
[defaults]
inventory = inventory
host_key_checking = False

ab@VB:~/aaa$ cat inventory
PTX     ansible_host=10.219.37.14

ab@VB:~/aaa$ cat uptime.yaml
---
- name: Get device uptime
  hosts:
    - all
  roles:
    - Juniper.junos
  connection: local
  gather_facts: no

  vars_prompt:
  - name: username
    prompt: Junos Username
    private: no

  - name: password
    prompt: Junos Password
    private: yes

  tasks:
  - name: get uptime using galaxy module
    juniper_junos_command:
      commands:
        - show system uptime
      provider:
        host: "{{ ansible_host }}"
        port: 22
        user: "{{ username }}"
        passwd: "{{ password }}"

Full log of successful attempt:

ab@VB:~/aaa$ ansible-playbook uptime.yaml -vvv
ansible-playbook 2.7.8
  config file = /home/ab/aaa/ansible.cfg
  configured module search path = [u'/home/ab/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python2.7/dist-packages/ansible
  executable location = /usr/local/bin/ansible-playbook
  python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]
Using /home/ab/aaa/ansible.cfg as config file
/home/ab/aaa/inventory did not meet host_list requirements, check plugin documentation if this is unexpected
/home/ab/aaa/inventory did not meet script requirements, check plugin documentation if this is unexpected
Parsed /home/ab/aaa/inventory inventory source with ini plugin

PLAYBOOK: uptime.yaml ***************************************************************************************************************************************
1 plays in uptime.yaml
Junos Username: labroot
Junos Password:

PLAY [Get device uptime] ************************************************************************************************************************************
META: ran handlers

TASK [get uptime using galaxy module] ***********************************************************************************************************************
task path: /home/ab/aaa/uptime.yaml:20
<10.219.37.14> ESTABLISH LOCAL CONNECTION FOR USER: ab
<10.219.37.14> EXEC /bin/sh -c 'echo ~ab && sleep 0'
<10.219.37.14> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/ab/.ansible/tmp/ansible-tmp-1552572975.16-116151779420689 `" && echo ansible-tmp-1552572975.16-116151779420689="` echo /home/ab/.ansible/tmp/ansible-tmp-1552572975.16-116151779420689 `" ) && sleep 0'
Using module file /home/ab/.ansible/roles/Juniper.junos/library/juniper_junos_command.py
<10.219.37.14> PUT /home/ab/.ansible/tmp/ansible-local-26344YouvSa/tmp39A4dG TO /home/ab/.ansible/tmp/ansible-tmp-1552572975.16-116151779420689/AnsiballZ_juniper_junos_command.py
<10.219.37.14> EXEC /bin/sh -c 'chmod u+x /home/ab/.ansible/tmp/ansible-tmp-1552572975.16-116151779420689/ /home/ab/.ansible/tmp/ansible-tmp-1552572975.16-116151779420689/AnsiballZ_juniper_junos_command.py && sleep 0'
<10.219.37.14> EXEC /bin/sh -c '/usr/bin/python /home/ab/.ansible/tmp/ansible-tmp-1552572975.16-116151779420689/AnsiballZ_juniper_junos_command.py && sleep 0'
<10.219.37.14> EXEC /bin/sh -c 'rm -f -r /home/ab/.ansible/tmp/ansible-tmp-1552572975.16-116151779420689/ > /dev/null 2>&1 && sleep 0'
ok: [PTX] => {
    "changed": false,
    "command": "show system uptime",
    "format": "text",
    "invocation": {
        "module_args": {
            "attempts": null,
            "baud": null,
            "commands": [
                "show system uptime"
            ],
            "console": null,
            "dest": null,
            "dest_dir": null,
            "formats": null,
            "host": "10.219.37.14",
            "level": null,
            "logdir": null,
            "logfile": null,
            "mode": null,
            "passwd": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "port": 22,
            "return_output": true,
            "ssh_config": null,
            "ssh_private_key_file": null,
            "timeout": 30,
            "user": "labroot"
        }
    },
    "msg": "The command executed successfully.",
    "stdout": "\nCurrent time: 2019-03-17 00:35:16 UTC\nTime Source:  LOCAL CLOCK \nSystem booted: 2019-03-16 22:55:11 UTC (01:40:05 ago)\nProtocols started: 2019-03-16 22:56:14 UTC (01:39:02 ago)\nLast configured: 2019-03-16 22:56:10 UTC (01:39:06 ago) by root\n12:35AM  up 1:40, 1 users, load averages: 0.27, 0.21, 0.22\n",
    "stdout_lines": [
        "",
        "Current time: 2019-03-17 00:35:16 UTC",
        "Time Source:  LOCAL CLOCK ",
        "System booted: 2019-03-16 22:55:11 UTC (01:40:05 ago)",
        "Protocols started: 2019-03-16 22:56:14 UTC (01:39:02 ago)",
        "Last configured: 2019-03-16 22:56:10 UTC (01:39:06 ago) by root",
        "12:35AM  up 1:40, 1 users, load averages: 0.27, 0.21, 0.22"
    ]
}
META: ran handlers
META: ran handlers

PLAY RECAP **************************************************************************************************************************************************
PTX                        : ok=1    changed=0    unreachable=0    failed=0
---
Regards,
Andrei.
Junos Automation (Scripting)

Re: Ansible and Junos , Python error ,   "module_stdout": "/usr/bin/python2.7: not found\r\n",

‎03-14-2019 09:51 AM

Thanks Andrei


Thanks that worked, when I simplified the config and inventory file and ran it from a new directory.

This is great I now have a working and non working example. I know this issue is not python on my local centos box and is not the vSRX,  I will keep digging to work out the exact reason this was not working, I suspect it was trying to run a Python Script directly on the Juniper devices rather than using netconf but I'm compeatly new to this so just guessing.

Thanks again Andrei
Regards
Simon Bingham

Junos Automation (Scripting)

Re: Ansible and Junos , Python error ,   "module_stdout": "/usr/bin/python2.7: not found\r\n",

‎03-14-2019 10:31 AM

this was the issue

172.27.233.17 ansible_connection=ssh 

  corrected to

172.27.233.17

 

resulted in the whole "python" issue.