1

I want to use the dpkg -l | grep -i staff command using ansible, but it gives me an error:

ansible all -m 'dpkj -l | grep -i stuff' 


ERROR! this task 'dpkj' has extra params, which is only allowed in the following modules: command, win_command, shell, win_shell, script, include, include_vars, include_tasks, include_role, import_tasks, import_role, add_host, group_by, set_fact, raw, meta

then I made playbook for it:

- hosts: all
  tasks:
  - name: Ansible command
    command: dpkg -l | grep -i staff

and it gives me an error again:

FAILED! => {"changed": true, "cmd": ["dpkg", "-l", "|", "grep", "-i", "staff"], "delta": "0:00:00.010754", "end": "2019-06-26 18:15:10.373642", "msg": "non-zero return code", "rc": 1, "start": "2019-06-26 18:15:10.362888", "stderr": "dpkg-query: no packages found matching |\ndpkg-query: no packages found matching -i\ndpkg-query: no packages found matching stuff"

How can I execute this command using ansible?

1
  • 1
    Looks like a simple typo in the first command :)
    – oxr463
    Jun 26 '19 at 17:48
3

You're using a pipe in a simple command; Ansible: difference between "shell" and "command" modules mentions the Ansible documentation for the command module:

The command(s) will not be processed through the shell, so variables like $HOME and operations like <, >, |, ; and & will not work. Use the shell module if you need these features.

Notice the | symbol in that list of operations that "will not work".

6
  • ok, I delete '| grep -i stuff' and it working fine,but how can I see the output of 'dpkg' command
    – Ash Adams
    Jun 26 '19 at 18:18
  • and is it possible to use 'grep' some how?
    – Ash Adams
    Jun 26 '19 at 18:19
  • did you change the playbook to use shell instead of command?
    – Jeff Schaller
    Jun 26 '19 at 18:20
  • 1
    That seems to be a common question (at least, on Stack Overflow); it appears you need to use a register directive to assign the output to a variable. See stackoverflow.com/questions/20563639/… for some examples.
    – Jeff Schaller
    Jun 26 '19 at 18:48
  • 1
    Since we got the command executed, you might consider this question asked & answered, and open a new question with your updated playbook and question about capturing output (including how you want it displayed & when).
    – Jeff Schaller
    Jun 26 '19 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.