This is more of a basic shell problem, rather than an Ansible problem. That command line generates an error even without ansible:
$ echo cat \/\/*[local-name()='project']\/*[local-name()='version'] | xmllint --shell pom.xml | sed '\/^\/ >/d' | sed 's/<[^>]*.//g' bash: syntax error near unexpected token `('
The argument to the cat command is insufficiently quoted, and you appear to be escaping the forward-slash (/) which is unnecessary and may in fact cause problems. Try this:
- hosts: localhost tasks: - shell: > echo cat '//*[local-name()="project"]/*[local-name()="version"]' | xmllint --shell pom.xml | sed '\/^\/ >/d' | sed 's/<[^>]*.//g' register: artifactId - debug: var: artifactId.stdout_lines
Using the > folded scalar operator lets you avoid a level of quoting, and that makes the command easier to manage. It also allows you to format it to be more readable.
Given the following input:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <version>4.0.0</version> </project>
The above playbook results in:
TASK [command] ***************************************************************** changed: [localhost] TASK [debug] ******************************************************************* ok: [localhost] => { "artifactId.stdout_lines": [ "4.0.0" ] }
While this works, you might want to consider using some sort of XPath module for ansible instead. This one appears to have recent activity, although I haven't tried it myself.