Missing Required Python Library for Ansible Projects

The following issue is commonly reported within the Network to Code Slack Community on the #ansible channel. A user gets an error that a library is installed that they already have installed. The message received in Ansible generally looks something like:

{"changed": false, "failed": true, "msg": "This module requires TextFSM"}<br>

If you are just looking for a simple answer, likely just setting the ansible_python_interpreter: "python" in your group_vars/all.yml will fix the issue. However, if you want to understand more about the underlying reasons, read on.

Demonstration of the Issue

This simple playbook will be used for the purpose of this demonstration.

---

- name: "GET VERSION INFORMATION"
  hosts: "all"
  gather_facts: "no"
  connection: "local"

  tasks:

  - name: "RUN NTC SHOW COMMAND"
    ntc_show_command:
      connection: "ssh"
      platform: "cisco_ios"
      command: "show version"
      template_dir: "/templates"
      host: "{{ inventory_hostname }}"
      username: "{{ ansible_user }}"
      password: "{{ ansible_password }}"

Giving it a try, we get the following expected output.

(required_lib) root@f3149f50819b:~/ntc_pb# ansible-playbook -i inventory pb_test_textfsm.yml
SSH password:

PLAY [GET VERSION INFORMATION] *****************************************************************************************************************************************************************************

TASK [RUN NTC SHOW COMMAND] ********************************************************************************************************************************************************************************
fatal: [10.1.100.52]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "msg": "This module requires TextFSM"}

PLAY RECAP *************************************************************************************************************************************************************************************************
10.1.100.52                : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

(required_lib) root@f3149f50819b:~/ntc_pb#

Since we did not have TextFSM installed, this result is expected. Now, on to to installing TextFSM.

(required_lib) root@f3149f50819b:~/ntc_pb# pip install textfsm
Collecting textfsm
  Downloading https://files.pythonhosted.org/packages/bd/27/0b149b6da3e47cc8daebace6920093114392171a8f5c24f1f2ad9a9e9c4d/textfsm-1.1.0-py2.py3-none-any.whl
Collecting future (from textfsm)
  Downloading https://files.pythonhosted.org/packages/45/0b/38b06fd9b92dc2b68d58b75f900e97884c45bedd2ff83203d933cf5851c9/future-0.18.2.tar.gz (829kB)
    100% |████████████████████████████████| 829kB 5.4MB/s
Requirement already satisfied: six in /tmp/required_lib/lib/python3.6/site-packages (from textfsm) (1.15.0)
Installing collected packages: future, textfsm
  Running setup.py install for future ... done
Successfully installed future-0.18.2 textfsm-1.1.0
You are using pip version 10.0.1, however version 20.2b1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(required_lib) root@f3149f50819b:~/ntc_pb#

So that should fix the issue, right?

(required_lib) root@f3149f50819b:~/ntc_pb# ansible-playbook -i inventory pb_test_textfsm.yml
SSH password:

PLAY [GET VERSION INFORMATION] *****************************************************************************************************************************************************************************

TASK [RUN NTC SHOW COMMAND] ********************************************************************************************************************************************************************************
fatal: [10.1.100.52]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "msg": "This module requires TextFSM"}

PLAY RECAP *************************************************************************************************************************************************************************************************
10.1.100.52                : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

(required_lib) root@f3149f50819b:~/ntc_pb#

It turns out no, there is a hint though. "ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, is not /root/required_lib/bin/python which is the Python version we are expecting. There are two options. You can either hard code the Python virtual environment or be more generic to use the one used when you type python to access the interactive shell. If you can set the ansible_python_interpreter in your all.yml file, setting it to “what happens when you type python is generally what users want. However, there are edge cases where this is not desirable, so adjust accordingly.

(required_lib) root@f3149f50819b:~/ntc_pb# cat group_vars/all.yml
---
ansible_python_interpreter: "python"
(required_lib) root@f3149f50819b:~/ntc_pb#

Now to see a successful run of the playbook.

(required_lib) root@f3149f50819b:~/ntc_pb# ansible-playbook -i inventory pb_test_textfsm.yml

PLAY [GET VERSION INFORMATION] *****************************************************************************************************************************************************************************

TASK [RUN NTC SHOW COMMAND] ********************************************************************************************************************************************************************************
ok: [10.1.100.52]

PLAY RECAP *************************************************************************************************************************************************************************************************
10.1.100.52                : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

(required_lib) root@f3149f50819b:~/ntc_pb#

Alternate Configuration Methods

The following options are available, in this preference order, as I personally tested in Ansible 2.9.10.

  • You can set the ansible_python_interpreter value at any place within your inventory.
  • Set the ANSIBLE_PYTHON_INTERPRETER environment value.
  • You can also set interpreter_python key in the [defaults] section of ansible.cfg.

Conclusion

For more information refer to the Ansible Documentation

-Ken



ntc img
ntc img

Contact Us to Learn More

Share details about yourself & someone from our team will reach out to you ASAP!

Thanks for submitting the form.

Author