Repository: ansible/ansible

Issue:
apache2_module fails for php5
##### 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
<!---
ANSIBLE_SSH_ARGS(/home/ps/.ansible.cfg) = -o ControlMaster=auto -o ControlPersist=60s
ANSIBLE_SSH_CONTROL_PATH(/home/ps/.ansible.cfg) = ~/.ssh/ansible-ssh-%%h-%%p-%%r
ANSIBLE_SSH_PIPELINING(/home/ps/.ansible.cfg) = True
CACHE_PLUGIN(/home/ps/.ansible.cfg) = memory
COMMAND_WARNINGS(/home/ps/.ansible.cfg) = False
DEFAULT_FORKS(/home/ps/.ansible.cfg) = 50
DEFAULT_GATHERING(/home/ps/.ansible.cfg) = implicit
DEFAULT_HASH_BEHAVIOUR(/home/ps/.ansible.cfg) = replace
DEFAULT_LOG_PATH(/home/ps/.ansible.cfg) = /home/ps/.ansible/ansible.log
DEFAULT_MANAGED_STR(/home/ps/.ansible.cfg) = This file is managed by Ansible, expect it to be overwritten
DEFAULT_MODULE_LANG(/home/ps/.ansible.cfg) = C
DEFAULT_POLL_INTERVAL(/home/ps/.ansible.cfg) = 15
DEFAULT_REMOTE_PORT(/home/ps/.ansible.cfg) = 22
DEFAULT_REMOTE_TMP(/home/ps/.ansible.cfg) = $HOME/.ansible/tmp
DEFAULT_SUDO_USER(/home/ps/.ansible.cfg) = root
DEFAULT_TIMEOUT(/home/ps/.ansible.cfg) = 60
DEFAULT_TRANSPORT(/home/ps/.ansible.cfg) = ssh
DISPLAY_SKIPPED_HOSTS(/home/ps/.ansible.cfg) = True
RETRY_FILES_ENABLED(/home/ps/.ansible.cfg) = False
-->

##### OS / ENVIRONMENT
<!---
N/A
-->

##### SUMMARY
<!--- apache2_module fails for module php5 -->

##### STEPS TO REPRODUCE
<!---
Use apache2_module with php5
-->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
apache_modules:
  - php5
```
##### EXPECTED RESULTS
<!--- Enable php5 module for apache2, same playbook works for ansible 2.0-2.3.2 -->

##### ACTUAL RESULTS
<!--- Step failed -->

<!--- Paste verbatim command output between quotes below -->
```
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'
```

apache2_module fails for php5
##### 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
<!---
ANSIBLE_SSH_ARGS(/home/ps/.ansible.cfg) = -o ControlMaster=auto -o ControlPersist=60s
ANSIBLE_SSH_CONTROL_PATH(/home/ps/.ansible.cfg) = ~/.ssh/ansible-ssh-%%h-%%p-%%r
ANSIBLE_SSH_PIPELINING(/home/ps/.ansible.cfg) = True
CACHE_PLUGIN(/home/ps/.ansible.cfg) = memory
COMMAND_WARNINGS(/home/ps/.ansible.cfg) = False
DEFAULT_FORKS(/home/ps/.ansible.cfg) = 50
DEFAULT_GATHERING(/home/ps/.ansible.cfg) = implicit
DEFAULT_HASH_BEHAVIOUR(/home/ps/.ansible.cfg) = replace
DEFAULT_LOG_PATH(/home/ps/.ansible.cfg) = /home/ps/.ansible/ansible.log
DEFAULT_MANAGED_STR(/home/ps/.ansible.cfg) = This file is managed by Ansible, expect it to be overwritten
DEFAULT_MODULE_LANG(/home/ps/.ansible.cfg) = C
DEFAULT_POLL_INTERVAL(/home/ps/.ansible.cfg) = 15
DEFAULT_REMOTE_PORT(/home/ps/.ansible.cfg) = 22
DEFAULT_REMOTE_TMP(/home/ps/.ansible.cfg) = $HOME/.ansible/tmp
DEFAULT_SUDO_USER(/home/ps/.ansible.cfg) = root
DEFAULT_TIMEOUT(/home/ps/.ansible.cfg) = 60
DEFAULT_TRANSPORT(/home/ps/.ansible.cfg) = ssh
DISPLAY_SKIPPED_HOSTS(/home/ps/.ansible.cfg) = True
RETRY_FILES_ENABLED(/home/ps/.ansible.cfg) = False
-->

##### OS / ENVIRONMENT
<!---
N/A
-->

##### SUMMARY
<!--- apache2_module fails for module php5 -->

##### STEPS TO REPRODUCE
<!---
Use apache2_module with php5
-->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
apache_modules:
  - php5
```
##### EXPECTED RESULTS
<!--- Enable php5 module for apache2, same playbook works for ansible 2.0-2.3.2 -->

##### ACTUAL RESULTS
<!--- Step failed -->

<!--- Paste verbatim command output between quotes below -->
```
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'
```



Discussion:
cc @berendt @n0trax @robinro
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
@pujan14 Thanks for this bug report.

You're right, the PHP-regex causes the problem. I'll create bugfix-PR for this.
Can you please tell me which distribution you're using?
@n0trax thanks for PR. My control machine is Debian 9 and ansible host is Ubuntu 14.04

> On 27. Sep 2017, at 10:11, n0trax <notifications@github.com> wrote:
> 
> @pujan14 Thanks for this bug report.
> 
> You're right, the PHP-regex causes the problem. I'll create bugfix-PR for this.
> Can you please tell me which distribution you're using?
> 
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub, or mute the thread.
> 

cc @berendt @n0trax @robinro
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
@pujan14 Thanks for this bug report.

You're right, the PHP-regex causes the problem. I'll create bugfix-PR for this.
Can you please tell me which distribution you're using?
@n0trax thanks for PR. My control machine is Debian 9 and ansible host is Ubuntu 14.04

> On 27. Sep 2017, at 10:11, n0trax <notifications@github.com> wrote:
> 
> @pujan14 Thanks for this bug report.
> 
> You're right, the PHP-regex causes the problem. I'll create bugfix-PR for this.
> Can you please tell me which distribution you're using?
> 
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub, or mute the thread.
> 


Resolving patch:
diff --git a/lib/ansible/modules/web_infrastructure/apache2_module.py b/lib/ansible/modules/web_infrastructure/apache2_module.py
--- a/lib/ansible/modules/web_infrastructure/apache2_module.py
+++ b/lib/ansible/modules/web_infrastructure/apache2_module.py
@@ -166,8 +166,11 @@ def create_apache_identifier(name):
 
     for search, reexpr in re_workarounds:
         if search in name:
-            rematch = re.search(reexpr, name)
-            return rematch.group(1) + '_module'
+            try:
+                rematch = re.search(reexpr, name)
+                return rematch.group(1) + '_module'
+            except AttributeError:
+                pass
 
     return name + '_module'
 


Test patch: