internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: ansible/ansible

Issue:
Hardcoded location of the chroot executable
On Gentoo, chroot is in /usr/bin and not /usr/sbin as ansible assumes.



Discussion:
I am fine with this just using the OS path if you would like to modify the chroot plugin.

(closed accidentally)


Resolving patch:
diff --git a/lib/ansible/runner/connection_plugins/chroot.py b/lib/ansible/runner/connection_plugins/chroot.py
--- a/lib/ansible/runner/connection_plugins/chroot.py
+++ b/lib/ansible/runner/connection_plugins/chroot.py
@@ -16,13 +16,11 @@
 # You should have received a copy of the GNU General Public License
 # along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
 
+import distutils.spawn
 import traceback
 import os
-import pipes
 import shutil
 import subprocess
-import select
-import fcntl
 from ansible import errors
 from ansible import utils
 from ansible.callbacks import vvv
@@ -45,6 +43,10 @@ def __init__(self, runner, host, port, *args, **kwargs):
         if not utils.is_executable(chrootsh):
             raise errors.AnsibleError("%s does not look like a chrootable dir (/bin/sh missing)" % self.chroot)
 
+        self.chroot_cmd = distutils.spawn.find_executable('chroot')
+        if not self.chroot_cmd:
+            raise errors.AnsibleError("chroot command not found in PATH")
+
         self.runner = runner
         self.host = host
         # port is unused, since this is local
@@ -62,12 +64,10 @@ def exec_command(self, cmd, tmp_path, sudo_user, sudoable=False, executable='/bi
 
         # We enter chroot as root so sudo stuff can be ignored
 
-        chroot_cmd = '/usr/sbin/chroot'
-
         if executable:
-            local_cmd = [chroot_cmd, self.chroot, executable, '-c', cmd]
+            local_cmd = [self.chroot_cmd, self.chroot, executable, '-c', cmd]
         else:
-            local_cmd = '%s "%s" %s' % (chroot_cmd, self.chroot, cmd)
+            local_cmd = '%s "%s" %s' % (self.chroot_cmd, self.chroot, cmd)
 
         vvv("EXEC %s" % (local_cmd), host=self.chroot)
         p = subprocess.Popen(local_cmd, shell=isinstance(local_cmd, basestring),


Test patch:
Repository: ansible/ansible

Issue:
Support scripts dir in roles
Could we extend roles to support scripts?

So 

```
- script: myscript.sh
```

Gets automagically sucked up from, roles/myrole/scripts/myscript.sh

?



Discussion:
+1
now I have to put those files outside the role

I think this should be possible.

Patches would be accepted.   Leaving this open.


Resolving patch:
diff --git a/lib/ansible/runner/action_plugins/script.py b/lib/ansible/runner/action_plugins/script.py
--- a/lib/ansible/runner/action_plugins/script.py
+++ b/lib/ansible/runner/action_plugins/script.py
@@ -41,7 +41,10 @@ def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **
         # FIXME: error handling
         args    = " ".join(tokens[1:])
         source  = template.template(self.runner.basedir, source, inject)
-        source  = utils.path_dwim(self.runner.basedir, source)
+        if '_original_file' in inject:
+            source = utils.path_dwim_relative(inject['_original_file'], 'files', source, self.runner.basedir)
+        else:
+            source = utils.path_dwim(self.runner.basedir, source)
 
         # transfer the file to a remote tmp location
         source  = source.replace('\x00','') # why does this happen here?


Test patch:
Repository: ansible/ansible

Issue:
copy fails when used with sudo_user
I'm getting an  error when using the copy action when specifying a sudo_user. Here's an example playbook:

```
- hosts: myserver
  sudo: True
  sudo_user: lorin
  tasks:
    - name: ensure .gitconfig file is present
      copy: src=files/.gitconfig dest=/home/lorin/.gitconfig
```

Running this returns the following error:

```
TASK: [ensure .gitconfig file is present] *************************************
failed: [myserver] => {"failed": true}
msg: Could not replace file: /tmp/ansible-1375213714.07-124608323343148/source to /home/lorin/.gitconfig: [Errno 13] Permission denied
```

Error reproduces when sudo_user is attached to either the play or the task.

I did a git-bisect, and the commit that introduced the commit appears to be dafbcf87d35bf3d1100b7ef66ee9099c2cd32814



Discussion:

Resolving patch:
diff --git a/lib/ansible/module_common.py b/lib/ansible/module_common.py
--- a/lib/ansible/module_common.py
+++ b/lib/ansible/module_common.py
@@ -867,8 +867,8 @@ def atomic_move(self, src, dest):
             # Optimistically try a rename, solves some corner cases and can avoid useless work.
             os.rename(src, dest)
         except (IOError,OSError), e:
-            # only try workarounds for errno 18 (cross device) and 1 (not permited)
-            if e.errno != errno.EPERM and e.errno != errno.EXDEV:
+            # only try workarounds for errno 18 (cross device), 1 (not permited) and 13 (permission denied)
+            if e.errno != errno.EPERM and e.errno != errno.EXDEV and e.errno != errno.EACCES:
                 self.fail_json(msg='Could not replace file: %s to %s: %s' % (src, dest, e))
 
             dest_dir = os.path.dirname(dest)


Test patch:
Repository: ansible/ansible

Issue:
Host specifiers should be sorted
This is rather minor, but to ensure consistent behavior, hosts should look like this:

hosts: webservers:&boston:!rack42

So when applying the host selectors, run those without the "&" first, then the &s, then the !s.



Discussion:

Resolving patch:
diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py
--- a/lib/ansible/inventory/__init__.py
+++ b/lib/ansible/inventory/__init__.py
@@ -136,11 +136,27 @@ def _get_hosts(self, patterns):
         finds hosts that match a list of patterns. Handles negative
         matches as well as intersection matches.
         """
-        try:
-            if patterns[0].startswith("!"):
-                patterns.insert(0, "all")
-        except IndexError:
-            pass
+
+        # Host specifiers should be sorted to ensure consistent behavior
+        pattern_regular = []
+        pattern_intersection = []
+        pattern_exclude = []
+        for p in patterns:
+            if p.startswith("!"):
+                pattern_exclude.append(p)
+            elif p.startswith("&"):
+                pattern_intersection.append(p)
+            else:
+                pattern_regular.append(p)
+
+        # if no regular pattern was given, hence only exclude and/or intersection
+        # make that magically work
+        if pattern_regular == []:
+            pattern_regular = ['all']
+
+        # when applying the host selectors, run those without the "&" or "!"
+        # first, then the &s, then the !s.
+        patterns = pattern_regular + pattern_intersection + pattern_exclude
 
         hosts = set()
         for p in patterns:


Test patch:
Repository: ansible/ansible

Issue:
Role templates access fails when role tasks/ has tasks in a subdir.
I was trying to share comon tasks includes between roles, by using
the following dir structure:

```
util/
   include1
   include2
roles
  foo
    templates
      tmpl1
    tasks
      task1
      task2
      util -> ../../../util
  bar
    tasks
      task3
      task4
      util -> ../../../util
```

and then including `util/include1` from task1/2/3/4.

However, I found that when include1 has a `template` (and presumably
a `copy`) task, tmpl1 isn't found because foo/tasks/templates/ is searched
rather then templates/.



Discussion:
Please share your playbooks that you are using with this, I don't have enough to replicate from what you have shared so far.

Thanks.

I understand, I'll convert it into a question: What's the proper way to share common tasks across roles?

Update: and here's the example repo. https://github.com/y-p/ansible_example_3312

I'll try to explain the problem again: I'd m trying to avoid having to specify a relative path, and use the 
shortcut of having files in roles/foo/templates be accessible directly by name, even when
called from an include, as long as it's executed in the context of a role.

Sounds like the answer here is to avoid the symlinks :)

Not if the problem is how to avoid duplication...

Well there are other ways to avoid duplication, but point is noted.

Still leaving this open to investigate when able.


Resolving patch:
diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py
--- a/lib/ansible/utils/__init__.py
+++ b/lib/ansible/utils/__init__.py
@@ -254,7 +254,11 @@ def path_dwim_relative(original, dirname, source, playbook_base, check=True):
     # (used by roles code)
 
     basedir = os.path.dirname(original)
-    template2 = os.path.join(basedir, '..', dirname, source)
+    if os.path.islink(basedir):
+        basedir = unfrackpath(basedir)
+        template2 = os.path.join(basedir, dirname, source)
+    else:
+        template2 = os.path.join(basedir, '..', dirname, source)
     source2 = path_dwim(basedir, template2)
     if os.path.exists(source2):
         return source2


Test patch:
Repository: ansible/ansible

Issue:
Expose shell glob as Jinja2 filter
User would like to iterate across filesystem files in a Jinja2 template

This seems like we'd want to expose a filter to do this.



Discussion:
Trying to parse what this means. Would something like the following be the intended look and feel?

  name: do things with files
  command: do_something {{ item }}
  with_items(lines?): {{"/path/to/*.glob"|as_fileglob }}

Does that meet the requirement? I think this is doable, and meets the need. Not 100% on the aesthetics. Something about it bugs me, but I can't put my finger on it.

Nope, this is a Jinja2 filter not a lookup plugin.

Something like

{% for x in "/tmp/files/*" | fileglob %}

{% endfor %}

Not super high priority.


Resolving patch:
diff --git a/lib/ansible/runner/filter_plugins/core.py b/lib/ansible/runner/filter_plugins/core.py
--- a/lib/ansible/runner/filter_plugins/core.py
+++ b/lib/ansible/runner/filter_plugins/core.py
@@ -21,6 +21,7 @@
 import yaml
 import types
 import pipes
+import glob
 from ansible import errors
 from ansible.utils import md5s
 
@@ -74,6 +75,10 @@ def quote(a):
     ''' return its argument quoted for shell usage '''
     return pipes.quote(a)
 
+def fileglob(pathname):
+    ''' return list of matched files for glob '''
+    return glob.glob(pathname)
+
 class FilterModule(object):
     ''' Ansible core jinja2 filters '''
 
@@ -115,5 +120,8 @@ def filters(self):
 
             # md5 hex digest of string
             'md5': md5s,
+
+            # file glob
+            'fileglob': fileglob,
         }
     


Test patch:
diff --git a/test/TestFilters.py b/test/TestFilters.py
--- a/test/TestFilters.py
+++ b/test/TestFilters.py
@@ -2,6 +2,7 @@
 Test bundled filters
 '''
 
+import os.path
 import unittest, tempfile, shutil
 from ansible import playbook, inventory, callbacks
 import ansible.runner.filter_plugins.core
@@ -83,6 +84,11 @@ def test_quotes(self):
         a = ansible.runner.filter_plugins.core.quote('ls | wc -l')
         assert a == "'ls | wc -l'"
 
+    def test_fileglob(self):
+        pathname = os.path.join(os.path.dirname(__file__), '*')
+        a = ansible.runner.filter_plugins.core.fileglob(pathname)
+        assert __file__ in a
+
     #def test_filters(self):
 
         # this test is pretty low level using a playbook, hence I am disabling it for now -- MPD.
Repository: ansible/ansible

Issue:
script fails when using sudo and non root
outputs error:

``````
chmod: changing permissions of `/tmp/ansible-1382129443.61-91452078194625/test.sh': Operation not permitted
/bin/bash: /tmp/ansible-1382129443.61-91452078194625/test.sh: Permission denied```
``````



Discussion:

Resolving patch:
diff --git a/lib/ansible/runner/action_plugins/script.py b/lib/ansible/runner/action_plugins/script.py
--- a/lib/ansible/runner/action_plugins/script.py
+++ b/lib/ansible/runner/action_plugins/script.py
@@ -58,15 +58,18 @@ def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **
 
         conn.put_file(source, tmp_src)
 
-        # fix file permissions when the copy is done as a different user
+        sudoable=True
+        # set file permissions, more permisive when the copy is done as a different user
         if self.runner.sudo and self.runner.sudo_user != 'root':
-            prepcmd = 'chmod a+rx %s' % tmp_src
+            cmd_args_chmod = "chmod a+rx %s" % tmp_src
+            sudoable=False
         else:
-            prepcmd = 'chmod +x %s' % tmp_src
+            cmd_args_chmod = "chmod +rx %s" % tmp_src
+        self.runner._low_level_exec_command(conn, cmd_args_chmod, tmp, sudoable=sudoable)
 
         # add preparation steps to one ssh roundtrip executing the script
         env_string = self.runner._compute_environment_string(inject)
-        module_args = prepcmd + '; ' + env_string + tmp_src + ' ' + args
+        module_args = env_string + tmp_src + ' ' + args
 
         handler = utils.plugins.action_loader.get('raw', self.runner)
         result = handler.run(conn, tmp, 'raw', module_args, inject)


Test patch:
Repository: ansible/ansible

Issue:
Add playbook base directory built in variable
I would find it helpful to have a way to easily access a playbooks base directory. This would clean up having to ../ out of the role to get to top level configuration files.  See [google groups discussion](https://groups.google.com/forum/#!topic/ansible-project/BPnZPe_Ixhg)



Discussion:

Resolving patch:
diff --git a/lib/ansible/playbook/__init__.py b/lib/ansible/playbook/__init__.py
--- a/lib/ansible/playbook/__init__.py
+++ b/lib/ansible/playbook/__init__.py
@@ -138,6 +138,7 @@ def __init__(self,
         self.basedir     = os.path.dirname(playbook) or '.'
         utils.plugins.push_basedir(self.basedir)
         vars = extra_vars.copy()
+        vars['playbook_dir'] = self.basedir
         if self.inventory.basedir() is not None:
             vars['inventory_dir'] = self.inventory.basedir()
 
diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py
--- a/lib/ansible/playbook/play.py
+++ b/lib/ansible/playbook/play.py
@@ -123,6 +123,7 @@ def __init__(self, playbook, ds, basedir):
         self.max_fail_pct     = int(ds.get('max_fail_percentage', 100))
 
         load_vars = {}
+        load_vars['playbook_dir'] = self.basedir
         if self.playbook.inventory.basedir() is not None:
             load_vars['inventory_dir'] = self.playbook.inventory.basedir()
 
diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py
--- a/lib/ansible/runner/__init__.py
+++ b/lib/ansible/runner/__init__.py
@@ -415,6 +415,7 @@ def _executor_internal(self, host, new_stdin):
         inject['vars']        = self.module_vars
         inject['defaults']    = self.default_vars
         inject['environment'] = self.environment
+        inject['playbook_dir'] = self.basedir
 
         if self.inventory.basedir() is not None:
             inject['inventory_dir'] = self.inventory.basedir()


Test patch:
Repository: ansible/ansible

Issue:
not_in_host_file() checks wrong user's known_hosts file when ran with sudo
See https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/connection_plugins/ssh.py#L119

When one runs ansible(-playbook) itself through sudo on the local machine, `host_file` resolves to the one in the $SUDO_USER's home folder (probably as $HOME still points to it) not the one from the user ansible is ran as.

This should reference `~$USER`?



Discussion:
`host_file = os.path.expanduser(os.path.expandvars("~${USER}/.ssh/known_hosts"))` solves this.
If that's an acceptable solution, I'll make a PR for it.

@sergevanginderachter yeah that seems good to me. Submit a PR and I'll test it.


Resolving patch:
diff --git a/lib/ansible/runner/connection_plugins/ssh.py b/lib/ansible/runner/connection_plugins/ssh.py
--- a/lib/ansible/runner/connection_plugins/ssh.py
+++ b/lib/ansible/runner/connection_plugins/ssh.py
@@ -116,7 +116,7 @@ def _send_password(self):
             os.close(self.wfd)
 
     def not_in_host_file(self, host):
-        host_file = os.path.expanduser("~/.ssh/known_hosts")
+        host_file = os.path.expanduser(os.path.expandvars("~${USER}/.ssh/known_hosts"))
         if not os.path.exists(host_file):
             print "previous known host file not found"
             return True


Test patch:
Repository: ansible/ansible

Issue:
synchronize module do not work fine with ansible_transport=local in host file
Using this file /tmp/hosts:

```
[test]
test ansible_connection=local
```

as a hosts file, I see that synchronize do not work fine if run like this ( assume 2 directory created /tmp:foo and /tmp/bar )

```
ansible all -i /tmp/hosts  -m synchronize -a 'src=/tmp/foo dest=/tmp/bar' 
```

This result into this:

```
$ ansible  -i ./hosts all -m synchronize -a 'src=/tmp/foo dest=/tmp/bar'  
test | FAILED >> {
    "cmd": "rsync --delay-updates --compress --timeout=10 --archive --rsh 'ssh  -o StrictHostKeyChecking=no' --out-format='<<CHANGED>>%i %n%L' /tmp/foo misc@test:/tmp/bar", 
    "failed": true, 
    "msg": "ssh: Could not resolve hostname test: Name or service not known\r\nrsync: connection unexpectedly closed (0 bytes received so far) [sender]\nrsync error: unexplained error (code 255) at io.c(226) [sender=3.1.0]\n", 
    "rc": 255
}
```

Using -c local do not work either.



Discussion:

Resolving patch:
diff --git a/lib/ansible/runner/action_plugins/synchronize.py b/lib/ansible/runner/action_plugins/synchronize.py
--- a/lib/ansible/runner/action_plugins/synchronize.py
+++ b/lib/ansible/runner/action_plugins/synchronize.py
@@ -99,7 +99,7 @@ def run(self, conn, tmp, module_name, module_args,
 
         if options.get('mode', 'push') == 'pull':
             (dest_host, src_host) = (src_host, dest_host)
-        if not dest_host is src_host:
+        if not dest_host is src_host and self.original_transport != 'local':
             user = inject.get('ansible_ssh_user',
                               self.runner.remote_user)
             private_key = inject.get('ansible_ssh_private_key_file', self.runner.private_key_file)


Test patch:
Repository: ansible/ansible

Issue:
unique filter returns a set, which is incompatible with with_items:
Just filing the issue to not lose track over it, having already discussed it on IRC.

Options could be wrap the set() in list() in the filter, or make with_items accept a set().

Or document that if you want it to be a list, just double filter it: |unique|list



Discussion:
I think I like "make with_items accept a set" too.


Resolving patch:
diff --git a/lib/ansible/runner/lookup_plugins/items.py b/lib/ansible/runner/lookup_plugins/items.py
--- a/lib/ansible/runner/lookup_plugins/items.py
+++ b/lib/ansible/runner/lookup_plugins/items.py
@@ -36,8 +36,8 @@ def __init__(self, basedir=None, **kwargs):
     def run(self, terms, inject=None, **kwargs):
         terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject) 
 
-        if not isinstance(terms, list):
-            raise errors.AnsibleError("with_items expects a list")
+        if not isinstance(terms, list) and not isinstance(terms,set):
+            raise errors.AnsibleError("with_items expects a list or a set")
 
         return flatten(terms)
 


Test patch:
Repository: ansible/ansible

Issue:
runner/__init__.py:_remote_md5() fails on FreeBSD when target is a directory.
Instead of returning a '3' indicating a directory it will calculate the MD5 hash of the directory. This breaks the behavior of a few modules including unarchive. Tested on FreeBSD 9 and 10.



Discussion:
Thanks for filing a ticket! I am the friendly GitHub Ansibot. I see you did not fill out your issue description based on our new issue template. Please copy the contents of this [template](https://raw2.github.com/ansible/ansible/devel/examples/issues/ISSUE_TEMPLATE.md) and **paste it into the description** of your ticket. 

Our system will automatically close tickets that do not have an issue template filled out within 7 days. Please note that due to large interest in Ansible, humans may not comment on your ticket if you ask them questions. Don't worry, you're still in the queue and the robots are looking after you.

known issue, already discussed in #5741 and #5659


Resolving patch:
diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py
--- a/lib/ansible/runner/__init__.py
+++ b/lib/ansible/runner/__init__.py
@@ -944,15 +944,16 @@ def _remote_md5(self, conn, tmp, path):
         path = pipes.quote(path)
         # The following test needs to be SH-compliant.  BASH-isms will
         # not work if /bin/sh points to a non-BASH shell.
-        test = "rc=0; [ -r \"%s\" ] || rc=2; [ -f \"%s\" ] || rc=1; [ -d \"%s\" ] && rc=3" % ((path,) * 3)
+        test = "rc=0; [ -r \"%s\" ] || rc=2; [ -f \"%s\" ] || rc=1; [ -d \"%s\" ] && echo 3 && exit 0" % ((path,) * 3)
         md5s = [
-            "(/usr/bin/md5sum %s 2>/dev/null)" % path,  # Linux
-            "(/sbin/md5sum -q %s 2>/dev/null)" % path,  # ?
+            "(/usr/bin/md5sum %s 2>/dev/null)" % path,          # Linux
+            "(/sbin/md5sum -q %s 2>/dev/null)" % path,          # ?
             "(/usr/bin/digest -a md5 %s 2>/dev/null)" % path,   # Solaris 10+
-            "(/sbin/md5 -q %s 2>/dev/null)" % path,     # Freebsd
-            "(/usr/bin/md5 -n %s 2>/dev/null)" % path,  # Netbsd
-            "(/bin/md5 -q %s 2>/dev/null)" % path,      # Openbsd
-            "(/usr/bin/csum -h MD5 %s 2>/dev/null)" % path # AIX
+            "(/sbin/md5 -q %s 2>/dev/null)" % path,             # Freebsd
+            "(/usr/bin/md5 -n %s 2>/dev/null)" % path,          # Netbsd
+            "(/bin/md5 -q %s 2>/dev/null)" % path,              # Openbsd
+            "(/usr/bin/csum -h MD5 %s 2>/dev/null)" % path,     # AIX
+            "(/bin/csum -h MD5 %s 2>/dev/null)" % path          # AIX also
         ]
 
         cmd = " || ".join(md5s)


Test patch:
Repository: ansible/ansible

Issue:
ValueError: I/O operation on closed file (Multiple hosts)
Very much like this issue: https://github.com/ansible/ansible/issues/5146

i applied the patch from this issue and it work when ansible has only one host to deal with, but i still get the problem on multiple hosts.

i use an external inventory which has 4 hosts, and pattern= to narrow it down.
When i say that it's working on one host, i mean that the pattern match only one host.

```
import ansible.runner
runner = ansible.runner.Runner(
   module_name  = "copy",
   module_args  = "src=/tmp/ansibletest dest=/tmp/ansibletest",
   pattern      = "myhost0*"
)
results = runner.run()
```

```
    results = runner.run()
  File "/usr/lib/python2.7/site-packages/ansible/runner/__init__.py", line 989, in run
    results = self._parallel_exec(hosts)
  File "/usr/lib/python2.7/site-packages/ansible/runner/__init__.py", line 902, in _parallel_exec
    new_stdin = os.fdopen(os.dup(sys.stdin.fileno()))
ValueError: I/O operation on closed file
```

Thank you very much



Discussion:

Resolving patch:
diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py
--- a/lib/ansible/runner/__init__.py
+++ b/lib/ansible/runner/__init__.py
@@ -1075,9 +1075,17 @@ def _parallel_exec(self, hosts):
             job_queue.put(host)
         result_queue = manager.Queue()
 
+        try:
+            fileno = sys.stdin.fileno()
+        except ValueError:
+            fileno = None
+
         workers = []
         for i in range(self.forks):
-            new_stdin = os.fdopen(os.dup(sys.stdin.fileno()))
+            if fileno is not None:
+                new_stdin = os.fdopen(os.dup(fileno))
+            else:
+                new_stdin = None
             prc = multiprocessing.Process(target=_executor_hook,
                 args=(job_queue, result_queue, new_stdin))
             prc.start()


Test patch: