Tag Archives: python

Personal DevOps #3

While most of the prerequisites are met for my automated server setup I came across some issues when I started with my very first Ansible playbooks.

First Ansible Playbooks

First of all, I wanted to start with a quite simple ping playbook, to ensure the servers are reachable by Ansible.

# Playbook to ping all hosts 
---
- hosts: all
  gather_facts: false
  tasks:
    - ping:

When I run this script I was immediately confronted with the very first error. I really love when such things happen. Nothing can motivate one more than immediate failures like the following.

FAILED! => {
"changed": false,
"module_stderr": "Shared connection to xxx.xxx.xxx.xxx closed.\r\n",
"module_stdout": "/bin/sh: 1: /usr/bin/python: not found\r\n",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 127
}

As I started with a minimal Ubuntu 18.04 LTS installation, there is simply no Python 2 installed. However, to run the Ansible tasks on the node, Python is required. I made use of the raw task in Ansible to update the package lists as well as install the package python-minimal. In addition, I added the package python2.7-apt in this bootstraper as it is needed later on. Once Python has been installed the ping playbook worked without any problems.

# Bootstrap playbook to install python 2 and python-apt
# It checks first so no unecessary apt updates are performed
---
- hosts: all
  gather_facts: False
  
  tasks:
  - name: install python 2
    raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal)
  - name: install python-apt 
    raw: test -e  /usr/lib/python2.7/dist-packages/apt || (apt install -y python2.7-apt)

For both packages, I test for the corresponding directories on the node to avoid unnecessary updates.

Note: When testing for a directory on the shell the following line became very handy:

> [ -e /usr/lib/python2.7/dist-packages/apt ] && echo "Found" || echo "Not found"

At a second step, I created a maintenance playbook to update and upgrade the packages on my node.

# Playbook to update Ubuntu packages 
---
- hosts: all
  gather_facts: false
  tasks:
  - name: update and upgrade apt packages
    become: true
    apt:
      upgrade: yes
      update_cache: yes
      cache_valid_time: 86400

Before including the pyhton-apt package to the bootstraper, I got the following error when dry running the playbook.

fatal: [xxx.xxx.xxx.xxx]: FAILED! => {"changed": false, "msg": "python-apt must be installed to use check mode. If run normally this module can auto-install it."}

Conclusion

While this is not any rocket science for sure, I now have a few essential scripts to bring my server to a base level I can start working with.

SOAP with Python

To evaluate a SOAP service, I decided to increase my Python skills by one by consuming the SOAP service with Python. SOAP Web Services from Dive Into Python seemed a quite good starting point with detailed instructions for setting up your Python environment to do so.

After starting to work thought this article how to install the required SOAP libraries, I realized quickly links are broken and various information became obsolete.  Therefore, I decided to summarize the amendments to set up SOAP libraries for Python, in this article focusing on how to set up PyXML, fpconst and SOAPpy.

Prerequisites
Before starting lets be sure Python is installed. You might check with

where python

to determine which python will be accessed via the PATH variable.In my case it is a Python 2.7 running on Windows 8.0.

Python 2.7 on Windows 8.0PyXML
PyXML can’t be accessed via http://pyxml.sourceforge.net/. Instead you have to access the site via http://pyxml.sourceforge.net/topics/ directly. Unfortunately, you might realize that the latest supported version from there is Python 2.4. You will find a Windows Installer for PyXML 0.8.4 for Python 2.7 at the OddThinking blog instead.

Run through the installer, just make sure, you pick the right Python installation during the process.

PyXML Installation DialogVerifying the installation is quite simple using the Python console, though.

>>> import xml
>>> xml.__version__
'0.8.4'
>>

fpconst
Instead of picking up the fpconst package from Unversity of Washington’s broken link,  you’ll meanwhile find fpconst 0.7.2 within the Python Package Index. Download and extract the archive to any temporary folder. Change folders to the extracted one and run

python setup.py install

from the console.

fpconst Package installationAgain very the installed package with

>>> import fpconst
>>> fpconst.__version__
'0.7.2'
>>>

SOAPpy
SOAPpy is probably the package causing the most headache. I first picked SOAPpy 0.12.0_rc1 from the download section at  http://pywebsvcs.sourceforge.net/. It caused more or less trouble, trying to install the package. In addition, SOAPpy was announced to be merged into the ZSI (Zolera SOAP Infrastructure) web service toolkit. If you are going to try ZSI anyway, download and installation are similar to the example above using

python setup.py install

Even the latest version of ZSI installs fine with Python 2.7.

Anyway, when sticking with SOAPpy, you might want to get the latest version (0.12.5) from the Python Package Index. Extracting the archive and following the steps above will result in an import error, though.

Traceback (most recent call last):
  File "setup.py", line 7, in <module>
    from setuptools import setup, find_packages
ImportError: No module named setuptools

To solve this, navigate to the setuptoools 0.9.8 in the Python Package Index, and download ez_setup.py to your local machine. Run the script with

python ez_setup.py

which will compile quite a bunch of stuff on your local machine. Follow the instructions, and add the C:\Python27\Scripts to your PATH variable.

Once installation is complete, you will find an easy_install.exe program in your Python Scripts subdirectory. For simple invocation and best results, add this directory to your PATH environment variable, if it is not already present.

Head back to the download folder of SOAPpy 0.12.5 and run once again

python setup.py. install

which should finally end up in something similar to

...

Installed c:\python27\lib\site-packages\docutils-0.11-py2.7.egg
Searching for fpconst==0.7.2
Best match: fpconst 0.7.2
Adding fpconst 0.7.2 to easy-install.pth file

Using c:\python27\lib\site-packages
Finished processing dependencies for SOAPpy==0.12.5

For the very last time, verify the installation by

>>> import SOAPpy
>>> SOAPpy.__version__
'0.12.5'
>>>

By now, you should be ready to use all three packages with your Python 2.7 installation.