Error seen while executing Ansible script - /usr/bin/python2.7 - No module named site

Error while executing Ansible script - /usr/bin/python2.7 - No module named site.

  • Issue is seen when below Ansible script runs on Rocky9 OS system. Ansible version details:

  • ansible [core 2.14.17]
    config file = /etc/ansible/ansible.cfg
    configured module search path = [‘/root/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]
    ansible python module location = /usr/lib/python3.9/site-packages/ansible
    ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
    executable location = /usr/bin/ansible
    python version = 3.9.18 (main, Jan 24 2024, 00:00:00) [GCC 11.4.1 20231218 (Red Hat 11.4.1-3)] (/usr/bin/python3)
    jinja version = 3.1.2
    libyaml = True

  • Issue is “not” seen when below Ansible script runs on CentOS Linux 7 (Core). Ansible version details:

  • [root@GRP-DSKP:~] $ ansible --version

  • ansible 2.9.27
    config file = /home/root/ansible.cfg
    configured module search path = [u’/home/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, Nov 14 2023, 16:14:06) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]

Output:

(env_ansible_012325) [root@rocky9_bench_system my_ansible_dir]# date ; ansible-playbook initiate_ifconfig_v2.yml
Sun Apr  6 08:45:41 PM UTC 2025

PLAY [Launching the script to initiate ifconfig on all systems] **********************************************************************************************************************

TASK [Run tasks for host1_linux_target_system] **************************************************************************************************************************************
[WARNING]: Unhandled error in Python interpreter discovery for host host1_linux_target_system: Expecting value: line 1 column 1 (char 0)
[WARNING]: Platform linux on host host1_linux_target_system is using the discovered Python interpreter at /usr/bin/python2.7, but future installation of another Python interpreter could
change the meaning of that path. See https://docs.ansible.com/ansible-core/2.14/reference_appendices/interpreter_discovery.html for more information.
fatal: [host1_linux_target_system]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python2.7"}, "changed": false, "module_stderr": "Connection to 105.1.1.125 closed.\r\n", "module_stdout": "Could not find platform independent libraries <prefix>\r\nConsider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\r\nImportError: No module named site\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}
...ignoring

TASK [debug] *************************************************************************************************************************************************************************
fatal: [host1_linux_target_system]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout_lines'. 'dict object' has no attribute 'stdout_lines'\n\nThe error appears to be in '/root/my_ansible_dir/initiate_ifconfig_v2.yml': line 12, column 11, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n          register: output_1\n        - debug:\n          ^ here\n"}

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

(env_ansible_012325) [root@rocky9_bench_system my_ansible_dir]#
- name: Launching the script to initiate ifconfig on all systems
  hosts: lab_1_group
  gather_facts: no
  tasks:
    - name: Run tasks for host1_linux_target_system
      block:
        - name: Execute ifconfig command
          shell:
            cmd: "ifconfig | grep 192.168"
          ignore_errors: yes
          register: output_1
        - debug:
            msg: "{{ output_1.stdout_lines }}"
      when: inventory_hostname == 'host1_linux_target_system'

2.14 does not work with python 2.7, see python support matrix i linked above