Unable to use test-module to develop modules under Arch Linux

Hi guys, I’m trying to write some modules for Ansible, but getting stumped on this error below…

OS: Arch Linux
Linux laps 4.4.37-1-lts #1 SMP Thu Dec 8 22:29:57 CET 2016 x86_64 GNU/Linux

I git cloned the ansible repo and ran the env-setup successfully:

$ source ansible/hacking/env-setup
running egg_info
creating lib/ansible.egg-info
writing top-level names to lib/ansible.egg-info/top_level.txt
writing requirements to lib/ansible.egg-info/requires.txt
writing lib/ansible.egg-info/PKG-INFO
writing dependency_links to lib/ansible.egg-info/dependency_links.txt
writing manifest file ‘lib/ansible.egg-info/SOURCES.txt’
reading manifest file ‘lib/ansible.egg-info/SOURCES.txt’
reading manifest template ‘MANIFEST.in’
no previously-included directories found matching ‘v2’
no previously-included directories found matching ‘docsite’
no previously-included directories found matching ‘ticket_stubs’
no previously-included directories found matching ‘packaging’
no previously-included directories found matching ‘test’
no previously-included directories found matching ‘hacking’
warning: no previously-included files found matching ‘lib/ansible/modules/core/.git*’
warning: no previously-included files found matching ‘lib/ansible/modules/extras/.git*’
no previously-included directories found matching ‘lib/ansible/modules/core/.git’
no previously-included directories found matching ‘lib/ansible/modules/extras/.git’
writing manifest file ‘lib/ansible.egg-info/SOURCES.txt’

Setting up Ansible to run out of checkout…

PATH=/home/user/git/ansible/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl
PYTHONPATH=/home/user/git/ansible/lib:
MANPATH=/home/user/git/ansible/docs/man:

Remember, you may wish to specify your host file with -i

Done!

I created a bare minimum module file to use (confirmed it works on Ubuntu 16.04):

$ cat test.py
from ansible.module_utils.basic import AnsibleModule

def main():
module = AnsibleModule(
argument_spec = dict(
state = dict(default=‘present’, choices=[‘present’, ‘absent’]),
name = dict(required=True),
enabled = dict(required=True, type=‘bool’),
something = dict(aliases=[‘whatever’])
)
)

module.exit_json(changed=True, something_else=12345)

if name == ‘main’:
main()

Running the test-module asks for options (as expected)

$ ./ansible/hacking/test-module
Usage: test-module -[options] (-h for help)

Options:
-h, --help show this help message and exit
-m MODULE_PATH, --module-path=MODULE_PATH
REQUIRED: full path of module source to execute
-a MODULE_ARGS, --args=MODULE_ARGS
module argument string
-D DEBUGGER, --debugger=DEBUGGER
path to python debugger (e.g. /usr/bin/pdb)
-I INTERPRETER_TYPE=INTERPRETER_PATH, --interpreter=INTERPRETER_TYPE=INTERPRETER_PATH
path to interpreter to use for this module (e.g.
ansible_python_interpreter=/usr/bin/python)
-c, --check run the module in check mode
-n, --noexecute do not run the resulting module
-o FILENAME, --output=FILENAME
Filename for resulting module

However, when running the module with test-module (with and without the --interpreter flag present), results in the following error:

$ ./ansible/hacking/test-module -m test.py --interpreter=ansible_python_interpreter=/usr/bin/python2
Traceback (most recent call last):
File “./ansible/hacking/test-module”, line 245, in
main()
File “./ansible/hacking/test-module”, line 227, in main
(modfile, modname, module_style) = boilerplate_module(options.module_path, options.module_args, options.interpreter, options.check, options.filename)
File “./ansible/hacking/test-module”, line 149, in boilerplate_module
if module_style == ‘new’ and ‘ANSIBALLZ_WRAPPER = True’ in module_data:
TypeError: a bytes-like object is required, not ‘str’

From what I read it sounds like it’s a python3-related error, but overriding the interpreter I would assume would use python2.

Any insight into fixing this would be great.

Andrew

Hi Andrew,

Did you try something like b’ANSIBALLZ_WRAPPER = True’ ?

Perhaps you can find more hints here: http://www.diveintopython3.net/strings.html#byte-arrays

PS: this is just to try, then for the fix that will support both python 2 & 3 you’ll have to use something like six, ie: https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/source_control/git.py#L242

Hope this helps

Thanks for the suggestions.

Changing it to b’ANSIBALLZ_WRAPPER = True’ ended up giving me this error:

modfile2.write(module_data)
#!/usr/bin/env python
TypeError: write() argument must be str, not bytes

Then changing it to modfile2.write(str(module_data)) to see what would happen ended up with:

File “/usr/lib/python3.5/subprocess.py”, line 1551, in _execute_child
raise child_exception_type(errno_num, err_msg)
OSError: [Errno 8] Exec format error

I changed the shebang in the test-module from #!/usr/bin/env python to #!/usr/bin/env python2 seemed to do the trick though…

Andrew

Hi Guys, can we use a test.pyo file instead of a test.py file to provide module-path?