Re: [ansible-devel] new module: TypeError: 'NoneType' object is not iterable

2018-11-15 Thread Marek Slebodnik
Gruess Gott Christian,

what I did while debugging my ansible module is .

*export ANSIBLE_KEEP_REMOTE_FILES=1*
run ansible scipt /playbook etc

*ansible-playbook -i /tmp/i /home/mslebodn/lbuild/pb/vcs_update.yml*

and on target machine , find prepared ansible module and run it with pdb

[ans@r7_ms_vcs1 ~]$* find . -name '*veritas*'*
./.ansible/tmp/ansible-tmp-1542277938.32-134723917146469/veritas.py
[ans@r7_ms_vcs1 ~]$
*./.ansible/tmp/ansible-tmp-1542277938.32-134723917146469/veritas.py
explode*
Module expanded into:
/home/ans/.ansible/tmp/ansible-tmp-1542277938.32-134723917146469/debug_dir
[ans@r7_ms_vcs1 ~]$ *cd
/home/ans/.ansible/tmp/ansible-tmp-1542277938.32-134723917146469/debug_dir*
[ans@r7_ms_vcs1 debug_dir]$ *python -m pdb ansible_module_veritas.py args*



št 15. 11. 2018 o 11:26  napísal(a):

> im trying to programm a new ansible module. while testing i get the
> following error.
>
> Traceback (most recent call last):
>   File "/tmp/ansible_EHl4xq/ansible_module_aix_stanza.py", line 276, in
> 
> main()
>   File "/tmp/ansible_EHl4xq/ansible_module_aix_stanza.py", line 257, in
> main
> (changed, backup_file, diff, msg) = do_stanza(module, path, stanza,
> options, state, backup, create)
> TypeError: 'NoneType' object is not iterable
>
> i cannot see whats the particular problem here...any hints?
>
>
> #!/usr/bin/python
> # -*- coding: utf-8 -*-
>
>
> from __future__ import absolute_import, division, print_function
> __metaclass__ = type
>
> ANSIBLE_METADATA = {
> 'metadata_version': '1.1',
> 'status': ['preview'],
> 'supported_by': 'community'
> }
>
> DOCUMENTATION = '''
> ---
> module: aix_stanza
>
> short_description: modify aix stanza files
>
> version_added: "0.1"
>
> description:
> - "adds stanza lines/addr/value pairs, changes attr/value pairs,
> removes stanzas/attr/value pairs"
>
> options:
> path:
> description:
> - Path to the stanza file
> required: true
> stanza:
> description:
> - name of add_stanza
> required: true
> options:
>  description:
>  - comman separated key/value pairs (key=val,key=val,...)
> backup:
> description:
>   - Create a backup file including the timestamp information so you
> can get
> the original file back if you somehow clobbered it incorrectly.
> type: bool
> default: 'no'
>   state:
>  description:
>- If set to C(absent) the stanza will be removed if present instead
> of created.
>  choices: [ absent, present ]
>  default: present
>   others:
>  description:
>- All arguments accepted by the M(file) module also work here
>
> extends_documentation_fragment:
> - files
>
> author:
> - Christian Tremel (@flynn1973)
> '''
>
> EXAMPLES = '''
> - name: add ldap user stanza
>   aix_stanza:
> path: /etc/security/user
> stanza: exampleuser
> options: SYSTEM=LDAP,registry=LDAP
> state: present
> mode: 0644
> backup: yes
>
> - name: add filesystem entry
>   aix_stanza:
> path: /etc/filesystems
> stanza: /examplemount
> options: dev=/dev/lvosystem_c,vfs=jfs2,log=INLINE,mount=true
> state: present
> backup: yes
> '''
>
>
> import os
> import re
> import tempfile
> import traceback
> from ansible.module_utils.basic import *
>
>
>
> def match_option(option, line):
> option = list(options.items())
> option = re.escape(option)
> return re.match('( |\t)*%s( |\t)*(=|$)' % option, line) \
> or re.match('#( |\t)*%s( |\t)*(=|$)' % option, line) \
> or re.match(';( |\t)*%s( |\t)*(=|$)' % option, line)
>
> def match_active_option(option, line):
> option = list(options.items())
> option = re.escape(option)
> return re.match('( |\t)*%s( |\t)*(=|$)' % option, line)
>
> def match_stanza(stanza, line):
> stanza = re.escape(stanza)
> return re.match('^%s:[^:]*(?=[\s])' % stanza, line)
>
> def do_stanza(module, filename, stanza, options, state='present', backup=
> False, create=True):
>
> diff = dict(
> before='',
> after='',
> before_header='%s (content)' % filename,
> after_header='%s (content)' %  filename,
> )
>
> if not os.path.exists(filename):
> if not create:
> module.fail_json(rc=257, msg='Destination %s does not exist !'
> % filename)
> destpath = os.path.dirname(filename)
> if not os.path.exists(destpath) and not module.check_mode:
> os.makedirs(destpath)
> stanza_lines = []
> else:
> stanza_file = open(filename, 'r')
> try:
> stanza_lines = stanza_file.readlines()
> finally:
> stanza_file.close()
>
> if module._diff:
> diff['before'] = ''.join(stanza_lines)
>
> changed = False
>
> # stanza file may be empty so add at least a newline
> if not stanza_lines:
> stanza_lines.append('\n')
>
>  # last line should always be a newline to 

[ansible-devel] new module: TypeError: 'NoneType' object is not iterable

2018-11-15 Thread christian . tremel
im trying to programm a new ansible module. while testing i get the 
following error.

Traceback (most recent call last):
  File "/tmp/ansible_EHl4xq/ansible_module_aix_stanza.py", line 276, in 

main()
  File "/tmp/ansible_EHl4xq/ansible_module_aix_stanza.py", line 257, in main
(changed, backup_file, diff, msg) = do_stanza(module, path, stanza, 
options, state, backup, create)
TypeError: 'NoneType' object is not iterable

i cannot see whats the particular problem here...any hints?


#!/usr/bin/python
# -*- coding: utf-8 -*-


from __future__ import absolute_import, division, print_function
__metaclass__ = type

ANSIBLE_METADATA = {
'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'
}

DOCUMENTATION = '''
---
module: aix_stanza

short_description: modify aix stanza files

version_added: "0.1"

description:
- "adds stanza lines/addr/value pairs, changes attr/value pairs, 
removes stanzas/attr/value pairs"

options:
path:
description:
- Path to the stanza file
required: true
stanza:
description:
- name of add_stanza
required: true
options:
 description:
 - comman separated key/value pairs (key=val,key=val,...)
backup:
description:
  - Create a backup file including the timestamp information so you can 
get
the original file back if you somehow clobbered it incorrectly.
type: bool
default: 'no'
  state:
 description:
   - If set to C(absent) the stanza will be removed if present instead 
of created.
 choices: [ absent, present ]
 default: present
  others:
 description:
   - All arguments accepted by the M(file) module also work here

extends_documentation_fragment:
- files

author:
- Christian Tremel (@flynn1973)
'''

EXAMPLES = '''
- name: add ldap user stanza
  aix_stanza:
path: /etc/security/user
stanza: exampleuser
options: SYSTEM=LDAP,registry=LDAP
state: present
mode: 0644
backup: yes

- name: add filesystem entry
  aix_stanza:
path: /etc/filesystems
stanza: /examplemount
options: dev=/dev/lvosystem_c,vfs=jfs2,log=INLINE,mount=true
state: present
backup: yes
'''


import os
import re
import tempfile
import traceback
from ansible.module_utils.basic import *



def match_option(option, line):
option = list(options.items())
option = re.escape(option)
return re.match('( |\t)*%s( |\t)*(=|$)' % option, line) \
or re.match('#( |\t)*%s( |\t)*(=|$)' % option, line) \
or re.match(';( |\t)*%s( |\t)*(=|$)' % option, line)

def match_active_option(option, line):
option = list(options.items())
option = re.escape(option)
return re.match('( |\t)*%s( |\t)*(=|$)' % option, line)

def match_stanza(stanza, line):
stanza = re.escape(stanza)
return re.match('^%s:[^:]*(?=[\s])' % stanza, line)

def do_stanza(module, filename, stanza, options, state='present', backup=
False, create=True):

diff = dict(
before='',
after='',
before_header='%s (content)' % filename,
after_header='%s (content)' %  filename,
)

if not os.path.exists(filename):
if not create:
module.fail_json(rc=257, msg='Destination %s does not exist !' % 
filename)
destpath = os.path.dirname(filename)
if not os.path.exists(destpath) and not module.check_mode:
os.makedirs(destpath)
stanza_lines = []
else:
stanza_file = open(filename, 'r')
try:
stanza_lines = stanza_file.readlines()
finally:
stanza_file.close()

if module._diff:
diff['before'] = ''.join(stanza_lines)

changed = False

# stanza file may be empty so add at least a newline
if not stanza_lines:
stanza_lines.append('\n')

 # last line should always be a newline to keep up with POSIX standard
if stanza_lines[-1] == "" or stanza_lines[-1][-1] != '\n':
stanza_lines[-1] += '\n'
changed = True

if not stanza:
module.fail_json(msg="stanza name not set", rc=rc, err=err)

stanza_format = '\n%s\n'
option_format = '\t%s = %s\n'


for index, line in enumerate(stanza_lines):
if line.startswith('%s:' % stanza):
within_stanza = True
stanza_start = index
if within_stanza:
if state == 'present':
# loop through options dict
for opt in list(options.keys()):
# insert missing option lines at the end of the 
section
for i in range(index, 0, -1):
# search backwards for previous non-blank or 
non-comment lines
if not re.match(r'^[ \t]*([#;].*)?$', 
stanza_lines[i - 1]):
stanza_lines.insert(i, option_format % (opt, 
options[opt]))
msg = 'options added'