1

I want to return the version from my pom.xml using the command below, but it doesn't work.

- name: ensure apache is at the latest version 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 

Can someone help, please?

1
  • what's the error you are getting? Commented Aug 9, 2017 at 18:34

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

i have test it in debian but it prints diffrent result with 2 lines: ok: [localhost] => { "artifactId.stdout_lines": [ " -------", "0.1.0-SNAPSHOT" ] }
It sounds like right now you may simply need to fix your XPath expression. If you would like to update your question with a reproducible example (a runnable playbook and a corresponding pom.xml that results in the described behavior) I would be happy to take a closer look.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.