Issue 1:
ISSUE TYPE

Bug Report

COMPONENT NAME

modules/web_infrastructure/apache2_module.py

ANSIBLE VERSION

ansible 2.4.0.0
config file = None
configured module search path = [u'/Users/*****/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /Library/Python/2.7/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 2.7.10 (default, Jul 15 2017, 17:16:57) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]

CONFIGURATION

Empty

OS / ENVIRONMENT

SUMMARY

When enabling php5 on a 14.04 system an exception is thrown.

I believe the issue has to do with the variable not being checked for Noneness first:

for search, reexpr in re_workarounds:
if search in name:
rematch = re.search(reexpr, name)
return rematch.group(1) + '_module'

STEPS TO REPRODUCE

apache_php_install_modules:
- php5
- libapache2-mod-php5

apache_php_enable_module: php5.0

- name: Install php modules
package:
name: "{{ item }}"
state: present
with_items: "{{ apache_php_install_modules }}"
tags:
php
scgi

- name: Enable apache modules
apache2_module:
name: "{{ item }}"
state: present
with_items:
- scgi
- "{{ apache_php_enable_module }}"
notify:
- Restart apache
tags:
php
scgi

EXPECTED RESULTS

either "ok" or "changed"

ACTUAL RESULTS

Exception is thrown

Using module file /home/travis/virtualenv/python2.7.13/lib/python2.7/site-packages/ansible/modules/web_infrastructure/apache2_module.py
<localhost> EXEC /bin/sh -c 'echo ~ && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/travis/.ansible/tmp/ansible-tmp-1506660428.72-709978674497 `" && echo ansible-tmp-1506660428.72-709978674497="` echo /home/travis/.ansible/tmp/ansible-tmp-1506660428.72-709978674497 `" ) && sleep 0'
<localhost> PUT /tmp/tmpiUGMfp TO /home/travis/.ansible/tmp/ansible-tmp-1506660428.72-709978674497/apache2_module.py
<localhost> EXEC /bin/sh -c 'chmod u+x /home/travis/.ansible/tmp/ansible-tmp-1506660428.72-709978674497/ /home/travis/.ansible/tmp/ansible-tmp-1506660428.72-709978674497/apache2_module.py && sleep 0'
<localhost> EXEC /bin/sh -c 'sudo -H -S -n -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-rghrccwwnwgddngdcsfajyicavcmatch; /usr/bin/python /home/travis/.ansible/tmp/ansible-tmp-1506660428.72-709978674497/apache2_module.py; rm -rf "/home/travis/.ansible/tmp/ansible-tmp-1506660428.72-709978674497/" > /dev/null 2>&1'"'"' && sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_2VMVXu/ansible_module_apache2_module.py", line 238, in <module>
main()
File "/tmp/ansible_2VMVXu/ansible_module_apache2_module.py", line 233, in main
_set_state(module, module.params['state'])
File "/tmp/ansible_2VMVXu/ansible_module_apache2_module.py", line 184, in _set_state
if _module_is_enabled(module) != want_enabled:
File "/tmp/ansible_2VMVXu/ansible_module_apache2_module.py", line 141, in _module_is_enabled
searchstring = ' ' + create_apache_identifier(name)
File "/tmp/ansible_2VMVXu/ansible_module_apache2_module.py", line 170, in create_apache_identifier
return rematch.group(1) + '_module'
AttributeError: 'NoneType' object has no attribute 'group'
failed: [localhost] (item=php5) => {
"changed": false,
"failed": true,
"item": "php5",
"module_stderr": "Traceback (most recent call last):\n File \"/tmp/ansible_2VMVXu/ansible_module_apache2_module.py\", line 238, in <module>\n main()\n File \"/tmp/ansible_2VMVXu/ansible_module_apache2_module.py\", line 233, in main\n _set_state(module, module.params['state'])\n File \"/tmp/ansible_2VMVXu/ansible_module_apache2_module.py\", line 184, in _set_state\n if _module_is_enabled(module) != want_enabled:\n File \"/tmp/ansible_2VMVXu/ansible_module_apache2_module.py\", line 141, in _module_is_enabled\n searchstring = ' ' + create_apache_identifier(name)\n File \"/tmp/ansible_2VMVXu/ansible_module_apache2_module.py\", line 170, in create_apache_identifier\n return rematch.group(1) + '_module'\nAttributeError: 'NoneType' object has no attribute 'group'\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0
}

Issue 2:
ISSUE TYPE

Bug Report

COMPONENT NAME

apache2_module

ANSIBLE VERSION

ansible 2.4.0.0
config file = /home/ps/.ansible.cfg
configured module search path = [u'/home/ps/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python2.7/dist-packages/ansible
executable location = /usr/local/bin/ansible
python version = 2.7.13 (default, Jan 19 2017, 14:48:08) [GCC 6.3.0 20170118]

CONFIGURATION

OS / ENVIRONMENT

SUMMARY

STEPS TO REPRODUCE

apache_modules:
- php5

EXPECTED RESULTS

ACTUAL RESULTS

Using module file /usr/local/lib/python2.7/dist-packages/ansible/modules/web_infrastructure/apache2_module.py
<1.2.3.4> ESTABLISH SSH CONNECTION FOR USER: ansible
<1.2.3.4> SSH: EXEC ssh -o ControlMaster=auto -o ControlPersist=60s -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ansible -o ConnectTimeout=60 -o 'ControlPath=~/.ssh/ansible-ssh-%h-%p-%r' 1.2.3.4 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1506328589.49-122064381956952 `" && echo ansible-tmp-1506328589.49-122064381956952="` echo $HOME/.ansible/tmp/ansible-tmp-1506328589.49-122064381956952 `" ) && sleep 0'"'"''
<1.2.3.4> (0, 'ansible-tmp-1506328589.49-122064381956952=/home/ansible/.ansible/tmp/ansible-tmp-1506328589.49-122064381956952\n', '')
<1.2.3.4> PUT /tmp/tmpP42OZP TO /home/ansible/.ansible/tmp/ansible-tmp-1506328589.49-122064381956952/apache2_module.py
<1.2.3.4> SSH: EXEC sftp -b - -o ControlMaster=auto -o ControlPersist=60s -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ansible -o ConnectTimeout=60 -o 'ControlPath=~/.ssh/ansible-ssh-%h-%p-%r' '[1.2.3.4]'
<1.2.3.4> (0, 'sftp> put /tmp/tmpP42OZP /home/ansible/.ansible/tmp/ansible-tmp-1506328589.49-122064381956952/apache2_module.py\n', '')
<1.2.3.4> ESTABLISH SSH CONNECTION FOR USER: ansible
<1.2.3.4> SSH: EXEC ssh -o ControlMaster=auto -o ControlPersist=60s -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ansible -o ConnectTimeout=60 -o 'ControlPath=~/.ssh/ansible-ssh-%h-%p-%r' 1.2.3.4 '/bin/sh -c '"'"'chmod u+x /home/ansible/.ansible/tmp/ansible-tmp-1506328589.49-122064381956952/ /home/ansible/.ansible/tmp/ansible-tmp-1506328589.49-122064381956952/apache2_module.py && sleep 0'"'"''
<1.2.3.4> (0, '', '')
<1.2.3.4> ESTABLISH SSH CONNECTION FOR USER: ansible
<1.2.3.4> SSH: EXEC ssh -o ControlMaster=auto -o ControlPersist=60s -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ansible -o ConnectTimeout=60 -o 'ControlPath=~/.ssh/ansible-ssh-%h-%p-%r' -tt 1.2.3.4 '/bin/sh -c '"'"'sudo -H -S -p "[sudo via ansible, key=gbfxlqwavpoyetsyizstcudrknzonvlx] password: " -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-gbfxlqwavpoyetsyizstcudrknzonvlx; /usr/bin/python /home/ansible/.ansible/tmp/ansible-tmp-1506328589.49-122064381956952/apache2_module.py; rm -rf "/home/ansible/.ansible/tmp/ansible-tmp-1506328589.49-122064381956952/" > /dev/null 2>&1'"'"'"'"'"'"'"'"' && sleep 0'"'"''
<1.2.3.4> (0, '\r\nTraceback (most recent call last):\r\n File "/tmp/ansible_m43APd/ansible_module_apache2_module.py", line 238, in <module>\r\n main()\r\n File "/tmp/ansible_m43APd/ansible_module_apache2_module.py", line 233, in main\r\n _set_state(module, module.params[\'state\'])\r\n File "/tmp/ansible_m43APd/ansible_module_apache2_module.py", line 184, in _set_state\r\n if _module_is_enabled(module) != want_enabled:\r\n File "/tmp/ansible_m43APd/ansible_module_apache2_module.py", line 141, in _module_is_enabled\r\n searchstring = \' \' + create_apache_identifier(name)\r\n File "/tmp/ansible_m43APd/ansible_module_apache2_module.py", line 170, in create_apache_identifier\r\n return rematch.group(1) + \'_module\'\r\nAttributeError: \'NoneType\' object has no attribute \'group\'\r\n', 'Shared connection to 1.2.3.4 closed.\r\n')
failed: [test-host] (item=php5) => {
"changed": false,
"failed": true,
"item": "php5",
"module_stderr": "Shared connection to 1.2.3.4 closed.\r\n",
"module_stdout": "\r\nTraceback (most recent call last):\r\n File \"/tmp/ansible_m43APd/ansible_module_apache2_module.py\", line 238, in <module>\r\n main()\r\n File \"/tmp/ansible_m43APd/ansible_module_apache2_module.py\", line 233, in main\r\n _set_state(module, module.params['state'])\r\n File \"/tmp/ansible_m43APd/ansible_module_apache2_module.py\", line 184, in _set_state\r\n if _module_is_enabled(module) != want_enabled:\r\n File \"/tmp/ansible_m43APd/ansible_module_apache2_module.py\", line 141, in _module_is_enabled\r\n searchstring = ' ' + create_apache_identifier(name)\r\n File \"/tmp/ansible_m43APd/ansible_module_apache2_module.py\", line 170, in create_apache_identifier\r\n return rematch.group(1) + '_module'\r\nAttributeError: 'NoneType' object has no attribute 'group'\r\n",
"msg": "MODULE FAILURE",
"rc": 0
}

Most probably this part of code is the issue.

ansible 2.3.2

"""
Work around for php modules; php7.x are always listed as php7_module
"""
php_module = re.search(r'^(php\d)\.', name)
if php_module:
name = php_module.group(1)

ansible 2.4.0

# re expressions to extract subparts of names
re_workarounds = [
('php', r'^(php\d)\.'),
]

for a2enmod_spelling, module_name in text_workarounds:
if a2enmod_spelling in name:
return module_name

for search, reexpr in re_workarounds:
if search in name:
rematch = re.search(reexpr, name)
return rematch.group(1) + '_module'

return name + '_module'