Repository: ansible/ansible

Issue:
New symbolic perms code not python 2.4 compatible
##### Issue Type:

Bug Report
##### Ansible Version:

`ansible 1.8 (devel f8a30faeee) last updated 2014/08/27 14:33:57 (GMT -500)`
##### Environment:

Control: Mac OS, virtualenv, python 2.7.5

Target: RHEL 5
##### Summary:

As added in fc3597af5d3c0a866ee492f4553c0fe6608fd302 the following lines are not python 2.4 compatible:

https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/basic.py#L616
https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/basic.py#L626
https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/basic.py#L636
##### Steps To Reproduce:
1. ansible all -i rhel5.example.org, -m command -a whoami
##### Expected Results:

```
rhel5.example.org | success | rc=0 >>
sivel
```
##### Actual Results:

```
rhel5.example.org | FAILED >> {
    "failed": true,
    "msg": "SUDO-SUCCESS-rvariyjzhgvfbcvycyizbtzyrvijozvz\r\n  File \"/home/sivel/.ansible/tmp/ansible-tmp-1409169069.66-277569040462994/command\", line 833\r\n    'X': stat.S_IXUSR if apply_X_permission else 0,\r\n                       ^\r\nSyntaxError: invalid syntax\r\nX11 forwarding request failed\r\n",
    "parsed": false
}
```



Discussion:

Resolving patch:
diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py
--- a/lib/ansible/module_utils/basic.py
+++ b/lib/ansible/module_utils/basic.py
@@ -608,12 +608,24 @@ def _get_octal_mode_from_symbolic_perms(self, path_stat, user, perms):
 
         # Permission bits constants documented at:
         # http://docs.python.org/2/library/stat.html#stat.S_ISUID
+        if apply_X_permission:
+            X_perms = {
+                'u': {'X': stat.S_IXUSR},
+                'g': {'X': stat.S_IXGRP},
+                'o': {'X': stat.S_IXOTH}
+            }
+        else:
+            X_perms = {
+                'u': {'X': 0},
+                'g': {'X': 0},
+                'o': {'X': 0}
+            }
+
         user_perms_to_modes = {
             'u': {
                 'r': stat.S_IRUSR,
                 'w': stat.S_IWUSR,
                 'x': stat.S_IXUSR,
-                'X': stat.S_IXUSR if apply_X_permission else 0,
                 's': stat.S_ISUID,
                 't': 0,
                 'u': prev_mode & stat.S_IRWXU,
@@ -623,7 +635,6 @@ def _get_octal_mode_from_symbolic_perms(self, path_stat, user, perms):
                 'r': stat.S_IRGRP,
                 'w': stat.S_IWGRP,
                 'x': stat.S_IXGRP,
-                'X': stat.S_IXGRP if apply_X_permission else 0,
                 's': stat.S_ISGID,
                 't': 0,
                 'u': (prev_mode & stat.S_IRWXU) >> 3,
@@ -633,7 +644,6 @@ def _get_octal_mode_from_symbolic_perms(self, path_stat, user, perms):
                 'r': stat.S_IROTH,
                 'w': stat.S_IWOTH,
                 'x': stat.S_IXOTH,
-                'X': stat.S_IXOTH if apply_X_permission else 0,
                 's': 0,
                 't': stat.S_ISVTX,
                 'u': (prev_mode & stat.S_IRWXU) >> 6,
@@ -641,6 +651,10 @@ def _get_octal_mode_from_symbolic_perms(self, path_stat, user, perms):
                 'o': prev_mode & stat.S_IRWXO }
         }
 
+        # Insert X_perms into user_perms_to_modes
+        for key, value in X_perms.items():
+            user_perms_to_modes[key].update(value)
+
         or_reduce = lambda mode, perm: mode | user_perms_to_modes[user][perm]
         return reduce(or_reduce, perms, 0)
 


Test patch: