Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible Command module says that '|' is illegal character

I am using Ansible to deploy my project and I trying to check if an specified package is installed, but I have a problem with it task, here is the task:

- name: Check if python-apt is installed
  command: dpkg -l | grep python-apt
  register: python_apt_installed
  ignore_errors: True

And here is the problem:

$ ansible-playbook -i hosts idempotent.yml

PLAY [lxc-host] *************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [10.0.3.240]

TASK: [idempotent | Check if python-apt is installed] ************************* 
failed: [10.0.3.240] => {"changed": true, "cmd": ["dpkg", "-l", "|", "grep", "python-apt"], "delta": "0:00:00.015524", "end": "2014-07-10 14:41:35.207971", "rc": 2, "start": "2014-07-10 14:41:35.192447"}
stderr: dpkg-query: error: package name in specifier '|' is illegal: must start with an alphanumeric character
...ignoring

PLAY RECAP ******************************************************************** 
10.0.3.240                 : ok=2    changed=1    unreachable=0    failed=0 

Why is illegal this character '|' .

like image 463
Robert Avatar asked Jul 10 '14 14:07

Robert


People also ask

How do I run an Ansible ad hoc command?

Ad hoc commands are commands which can be run individually to perform quick functions. These commands need not be performed later. For example, you have to reboot all your company servers. For this, you will run the Adhoc commands from '/usr/bin/ansible'.

How do you use warn false in Ansible?

If you want to continue to use sudo, you can then disable this warning by setting warn to false, like this. Or, you could set comment_warnings to false in ansible. cfg, like this. However, this is probably to permissive, as any command that would generate a warning would no longer generate a warning.

What is the difference between Shell and command in Ansible?

The shell module executes commands in nodes or Shell scripts. Another dedicated Ansible module is Script that transfers the Shell script from the control machine to the remote server and executes it. In the command module, the given command executes on all selected nodes.


2 Answers

From the doc:

command - Executes a command on a remote node

The command module takes the command name followed by a list of space-delimited arguments. The given command will be executed on all selected nodes. It 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).

shell - Executes a commands in nodes

The shell module takes the command name followed by a list of space-delimited arguments. It is almost exactly like the command module but runs the command through a shell (/bin/sh) on the remote node.

Therefore you have to use shell: dpkg -l | grep python-apt.

like image 59
Sylvain Leroux Avatar answered Oct 06 '22 13:10

Sylvain Leroux


read about the command module in the Ansible documentation:

It will not be processed through the shell, so .. operations like "<", ">", "|", and "&" will not work

As it recommends, use the shell module:

- name: Check if python-apt is installed
  shell: dpkg -l | grep python-apt
  register: python_apt_installed
  ignore_errors: True

For what it's worth, you can check/confirm the installation in a debian environment using the apt command:

- name: ensure python-apt is installed
  apt: name=python-apt state=present
like image 24
tedder42 Avatar answered Oct 06 '22 13:10

tedder42