Using local_action with shell module can't deal with the command line arugment.

2,816 views
Skip to first unread message

廖鹏辉

unread,
Sep 17, 2015, 6:42:54 AM9/17/15
to Ansible Project

I want to echo something to a file using local_action and shell module. When I use echo -e, ansible will treat -e as string to print rather than a command line argument. See below:

$ cat t.yaml
- hosts: localhost
  remote_user: root
  gather_facts: False
  tasks:
    - name: test
      local_action: shell echo -e abc
$ ansible-playbook -vv t.yaml

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

TASK: [test] ******************************************************************
<127.0.0.1> REMOTE_MODULE command echo -e abc #USE_SHELL
changed: [localhost -> 127.0.0.1] => {"changed": true, "cmd": "echo -e abc", "delta": "0:00:00.006123", "end": "2015-09-17 11:04:00.381722", "rc": 0, "start": "2015-09-17 11:04:00.375599", "stderr": "", "stdout": "-e abc", "warnings": []}

PLAY RECAP ********************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0

The stdout filed shows that ansible echo a string that is -e abc which include the command line argument -e. I also test it without local_action option in the playbook. Its behavior is normal as I expected.

$ cat t.yaml
- hosts: localhost
  remote_user: root
  gather_facts: False
  tasks:
    - name: test
      shell:  echo -e abc
$ ansible-playbook -vv t.yaml

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

TASK: [test] ******************************************************************
<localhost> REMOTE_MODULE command echo -e abc #USE_SHELL
changed: [localhost] => {"changed": true, "cmd": "echo -e abc", "delta": "0:00:00.001969", "end": "2015-09-17 03:06:25.842056", "rc": 0, "start": "2015-09-17 03:06:25.840087", "stderr": "", "stdout": "abc", "warnings": []}

PLAY RECAP ********************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0

This time the stdout does not include the -e argument.

Is there something else I need to do when use local_action and shell module to execute command with argument?

Matt Martz

unread,
Sep 17, 2015, 7:34:44 AM9/17/15
to ansible...@googlegroups.com
The problem is likely due to /bin/sh being something like dash which doesn't support some arguments.  You might have to specify an executable= for /bin/bash or similar
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/c683d261-56c4-4180-920d-57ed5ea717ab%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Reply all
Reply to author
Forward
0 new messages