ansible / ansible-modules-core

Ansible modules - these modules ship with ansible
1.3k stars 1.95k forks source link

regex_replace filter groups not working with command module #5846

Closed jcrisologo closed 7 years ago

jcrisologo commented 7 years ago
ISSUE TYPE
COMPONENT NAME

command / shell / raw

ANSIBLE VERSION
ansible 2.2.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
CONFIGURATION
OS / ENVIRONMENT

N/A

SUMMARY

Regex group captures and backreferences do not seem to work properly when used with the command module, despite appearing to work with other modules.

STEPS TO REPRODUCE
- hosts: localhost
  connection: local
  tasks:
    - debug:    msg={{ 'foobar' | regex_replace('^(foo)bar$', '\\1') }}
    - command: echo {{ 'foobar' | regex_replace('^(foo)bar$', '\\1') }}

# This example came from trying to run https://github.com/MindPointGroup/RHEL6-STIG
    - debug:    msg={{ 'S.5....T.    /etc/rc.d/init.d/supervisord' | regex_replace('^..5......\\s+(/.+)$', '\\1') }}
    - command: echo {{ 'S.5....T.    /etc/rc.d/init.d/supervisord' | regex_replace('^..5......\\s+(/.+)$', '\\1') }}

Ran as 'ansible-playbook -i localhost, test.yml -vvvv'

EXPECTED RESULTS

The message printed out by the debug module should match the stdout of the command module.

ACTUAL RESULTS
Using /etc/ansible/ansible.cfg as config file
Set default localhost to localhost
Loading callback plugin default of type stdout, v2.0 from /usr/lib/python2.6/site-packages/ansible/plugins/callback/__init__.pyc

PLAYBOOK: test.yml *************************************************************
1 plays in test.yml

PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
Using module file /usr/lib/python2.6/site-packages/ansible/modules/core/system/setup.py
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: root
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1481053811.53-105230875962027 `" && echo ansible-tmp-1481053811.53-105230875962027="` echo $HOME/.ansible/tmp/ansible-tmp-1481053811.53-105230875962027 `" ) && sleep 0'
<localhost> PUT /tmp/tmp1nnc4i TO /root/.ansible/tmp/ansible-tmp-1481053811.53-105230875962027/setup.py
<localhost> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1481053811.53-105230875962027/ /root/.ansible/tmp/ansible-tmp-1481053811.53-105230875962027/setup.py && sleep 0'
<localhost> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1481053811.53-105230875962027/setup.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1481053811.53-105230875962027/" > /dev/null 2>&1 && sleep 0'
ok: [localhost]

TASK [debug] *******************************************************************
task path: /var/tmp/test.yml:4
ok: [localhost] => {
    "msg": "foo"
}

TASK [command] *****************************************************************
task path: /var/tmp/test.yml:5
Using module file /usr/lib/python2.6/site-packages/ansible/modules/core/commands/command.py
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: root
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1481053812.16-238399836137794 `" && echo ansible-tmp-1481053812.16-238399836137794="` echo $HOME/.ansible/tmp/ansible-tmp-1481053812.16-238399836137794 `" ) && sleep 0'
<localhost> PUT /tmp/tmp1nnc4i TO /root/.ansible/tmp/ansible-tmp-1481053812.16-238399836137794/command.py
<localhost> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1481053812.16-238399836137794/ /root/.ansible/tmp/ansible-tmp-1481053812.16-238399836137794/command.py && sleep 0'
<localhost> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1481053812.16-238399836137794/command.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1481053812.16-238399836137794/" > /dev/null 2>&1 && sleep 0'
changed: [localhost] => {
    "changed": true,
    "cmd": [
        "echo",
        "1"
    ],
    "delta": "0:00:00.002517",
    "end": "2016-12-06 19:50:12.248539",
    "invocation": {
        "module_args": {
            "_raw_params": "echo \\1",
            "_uses_shell": false,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "warn": true
        },
        "module_name": "command"
    },
    "rc": 0,
    "start": "2016-12-06 19:50:12.246022",
    "stderr": "",
    "stdout": "1",
    "stdout_lines": [
        "1"
    ],
    "warnings": []
}

TASK [debug] *******************************************************************
task path: /var/tmp/test.yml:8
ok: [localhost] => {
    "msg": "/etc/rc.d/init.d/supervisord"
}

TASK [command] *****************************************************************
task path: /var/tmp/test.yml:9
Using module file /usr/lib/python2.6/site-packages/ansible/modules/core/commands/command.py
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: root
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1481053812.35-102856372533899 `" && echo ansible-tmp-1481053812.35-102856372533899="` echo $HOME/.ansible/tmp/ansible-tmp-1481053812.35-102856372533899 `" ) && sleep 0'
<localhost> PUT /tmp/tmp1nnc4i TO /root/.ansible/tmp/ansible-tmp-1481053812.35-102856372533899/command.py
<localhost> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1481053812.35-102856372533899/ /root/.ansible/tmp/ansible-tmp-1481053812.35-102856372533899/command.py && sleep 0'
<localhost> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1481053812.35-102856372533899/command.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1481053812.35-102856372533899/" > /dev/null 2>&1 && sleep 0'
changed: [localhost] => {
    "changed": true,
    "cmd": [
        "echo",
        "S.5....T.",
        "/etc/rc.d/init.d/supervisord"
    ],
    "delta": "0:00:00.002650",
    "end": "2016-12-06 19:50:12.434084",
    "invocation": {
        "module_args": {
            "_raw_params": "echo S.5....T.    /etc/rc.d/init.d/supervisord",
            "_uses_shell": false,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "warn": true
        },
        "module_name": "command"
    },
    "rc": 0,
    "start": "2016-12-06 19:50:12.431434",
    "stderr": "",
    "stdout": "S.5....T. /etc/rc.d/init.d/supervisord",
    "stdout_lines": [
        "S.5....T. /etc/rc.d/init.d/supervisord"
    ],
    "warnings": []
}

PLAY RECAP *********************************************************************
localhost                  : ok=5    changed=2    unreachable=0    failed=0
jcrisologo commented 7 years ago

After goofing around a bit, I have modified the test playbook to produce the desired behavior. Within free-form arguments, you actually need to put less backslashes. This is not clear in the documentation which only states that you need more backslashes inside YAML arguments pre-2.0. Not sure if this is a bug or a documentation problem.

- hosts: localhost
  connection: local
  tasks:
    - debug:    msg={{ 'foobar' | regex_replace('^(foo)bar$', '\\1') }}
    - command: echo {{ 'foobar' | regex_replace('^(foo)bar$', '\1') }}

# This example came from trying to run https://github.com/MindPointGroup/RHEL6-STIG
    - debug:    msg={{ 'S.5....T.    /etc/rc.d/init.d/supervisord' | regex_replace('^..5......\\s+(/.+)$', '\\1') }}
    - command: echo {{ 'S.5....T.    /etc/rc.d/init.d/supervisord' | regex_replace('^..5......\s+(/.+)$', '\1') }}
Using /etc/ansible/ansible.cfg as config file

PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": "foo"
}

TASK [command] *****************************************************************
changed: [localhost] => {"changed": true, "cmd": ["echo", "foo"], "delta": "0:00:01.003961", "end": "2016-12-06 22:26:52.873410", "rc": 0, "start": "2016-12-06 22:26:51.869449", "stderr": "", "stdout": "foo", "stdout_lines": ["foo"], "warnings": []}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": "/etc/rc.d/init.d/supervisord"
}

TASK [command] *****************************************************************
changed: [localhost] => {"changed": true, "cmd": ["echo", "/etc/rc.d/init.d/supervisord"], "delta": "0:00:00.002626", "end": "2016-12-06 22:26:53.076810", "rc": 0, "start": "2016-12-06 22:26:53.074184", "stderr": "", "stdout": "/etc/rc.d/init.d/supervisord", "stdout_lines": ["/etc/rc.d/init.d/supervisord"], "warnings": []}

PLAY RECAP *********************************************************************
localhost                  : ok=5    changed=2    unreachable=0    failed=0
ansibot commented 7 years ago

This repository has been locked. All new issues and pull requests should be filed in https://github.com/ansible/ansible

Please read through the repomerge page in the dev guide.