Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assign executable in popen only if it is explicitly set #31347

Closed
wants to merge 2 commits into from

Conversation

ganeshrn
Copy link
Member

@ganeshrn ganeshrn commented Oct 5, 2017

SUMMARY

Fixes #30836

  • As default executable is None assign executable value
    only if it set explicitly.
ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

module_utils/basic.py

ANSIBLE VERSION
2.4
ADDITIONAL INFORMATION

Playbook:

- name: Test playbook
  vars:
    output: [u'Port      Name               Status       Reason               Err-disabled Vlans\\nFa1/0/1   EDGE_IF_VLAN_341   err-disabled bpduguard\\nFa1/0/33  EDGE_IF_VLAN_341   err-disabled bpduguard']
  tasks:
  - name: Test task
    shell: echo {{ output }} | grep -Po '.*bpduguard' | awk '{ print $1 }'
    register: disabled_ports
  - debug: msg={{ disabled_ports }}

Before:

 {
    "changed": true,
    "cmd": "echo [u'Port      Name               Status       Reason               Err-disabled Vlans\\nFa1/0/1   EDGE_IF_VLAN_341   err-disabled bpduguard\\nFa1/0/33  EDGE_IF_VLAN_341   err-disabled bpduguard'] | grep -Po '.*bpduguard' | awk '{ print $1 }'",
    "delta": "0:00:00.004178",
    "end": "2017-10-05 04:53:20.899535",
    "failed": false,
    "invocation": {
        "module_args": {
            "_raw_params": "echo [u'Port      Name               Status       Reason               Err-disabled Vlans\\nFa1/0/1   EDGE_IF_VLAN_341   err-disabled bpduguard\\nFa1/0/33  EDGE_IF_VLAN_341   err-disabled bpduguard'] | grep -Po '.*bpduguard' | awk '{ print $1 }'",
            "_uses_shell": true,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "warn": true
        }
    },
    "rc": 0,
    "start": "2017-10-05 04:53:20.895357",
    "stderr": "",
    "stderr_lines": [],
    "stdout": "[uPort",
    "stdout_lines": [
        "[uPort"
    ]
}

After

{
    "changed": true,
    "cmd": "echo [u'Port      Name               Status       Reason               Err-disabled Vlans\\nFa1/0/1   EDGE_IF_VLAN_341   err-disabled bpduguard\\nFa1/0/33  EDGE_IF_VLAN_341   err-disabled bpduguard'] | grep -Po '.*bpduguard' | awk '{ print $1 }'",
    "delta": "0:00:00.005992",
    "end": "2017-10-05 06:19:45.140702",
    "failed": false,
    "invocation": {
        "module_args": {
            "_raw_params": "echo [u'Port      Name               Status       Reason               Err-disabled Vlans\\nFa1/0/1   EDGE_IF_VLAN_341   err-disabled bpduguard\\nFa1/0/33  EDGE_IF_VLAN_341   err-disabled bpduguard'] | grep -Po '.*bpduguard' | awk '{ print $1 }'",
            "_uses_shell": true,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "warn": true
        }
    },
    "rc": 0,
    "start": "2017-10-05 06:19:45.134710",
    "stderr": "",
    "stderr_lines": [],
    "stdout": "Fa1/0/1\nFa1/0/33",
    "stdout_lines": [
        "Fa1/0/1",
        "Fa1/0/33"
    ]
}

*  As default executable is None assign executable value
   only if it set explicitly.
@ganeshrn ganeshrn requested review from bcoca and abadger October 5, 2017 11:00
@ansibot ansibot added affects_2.5 This issue/PR affects Ansible v2.5 bugfix_pull_request module_utils/basic needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Oct 5, 2017
@bcoca bcoca removed the needs_triage Needs a first human triage before being processed. label Oct 5, 2017
if executable:
args = [executable, '-c', args]
else:
shell = True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is incorrect, the caller is specifically asking to use shell

if use_unsafe_shell:
if executable is None:
executable = os.environ.get('SHELL')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why you are removing this, i can see issues with invalid shell/blocker shells set (nologin, /bin/false) but we can work around that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so reread what $SHELL means .. seems i made a mistake, i assumed it was 'current shell' but it is the 'default shell', probably removing these 2 lines will fix most issues, the else: shell = True below should stay.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing these lines will re-break: #24169

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OTOH, we might need to find a new fix for #24169 as bcoca is quoting the POSIX definition of SHELL above and the POSIX definition of popen http://pubs.opengroup.org/onlinepubs/9699919799/ seems to be why this is hardcoded to sh in the first place.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really, this works 'by accident', only when default shell matches the executable requested, as my previous comment states, i misinterpreted what the value of $SHELL represents

After rereading, actual fix of the ticket would require we pass ansible_shell_executable information to all modules and fallback to that

@@ -2621,14 +2622,9 @@ def run_command(self, args, check_rc=False, close_fds=True, executable=None, dat
msg = "Argument 'args' to run_command must be list or string"
self.fail_json(rc=257, cmd=args, msg=msg)

shell = False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not move this. You may want to remove setting shell = True in the above conditional block, though.

@abadger
Copy link
Contributor

abadger commented Oct 5, 2017

Another note, when you cherry-pick this you'll probably want to cherry pick 5f22b4f as well (it wasn't deemed important enough to backport but it shouldn't change any behaviour so it should be safe to do so.)

@abadger
Copy link
Contributor

abadger commented Oct 5, 2017

Closing this as we will need something like #31361 to fix both bugs. The ios_command bug has been fixed in 2.4.1 by reverting the original commit.

@abadger abadger closed this Oct 5, 2017
@ganeshrn ganeshrn deleted the default_executable_shell branch October 5, 2017 17:49
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bugfix_pull_request labels Mar 6, 2018
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.5 This issue/PR affects Ansible v2.5 bug This issue/PR relates to a bug. module_utils/basic needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ios_command: Broken stdout_lines behavior between Ansible 2.3 and 2.4
4 participants