Reputation: 2777
I have a variable called joomlaversion which I get using json_query. The value of joomlaversion is 4.0.2 but I am trying to swap the dots with dashes so it becomes 4-0-2
How can I substitute dots for dashes in this ansible variable value?
I am using Ansible 2.9.6
Here is what I have tried.
---
- name: Download JSON content
uri:
url: https://api.github.com/repos/joomla/joomla-cms/releases
return_content: yes
register: jsoncontent
- name: Get latest version of Joomla from the tag using contains
set_fact:
joomlaversion: "{{ jsoncontent.json | to_json | from_json |json_query(jmesquery)|json_query(jmesquery2) }}"
vars:
jmesquery: "[? (draft==`false` && prerelease==`false`)]"
jmesquery2: "[?name.contains(@, 'Joomla! 4')].tag_name|[0]"
- name: Replace the dots with dashes in Joomla version
set_fact:
joomlaversion2: "{{ joomlaversion }} | replace('.', '-')"
#joomlaversion2: '{{ joomlaversion | regex_findall("\\."),("\\-") }}'
Rather than changing the dots to dashes it is appending | replace('.','-') on to the variable value, so it becomes "4.0.2 | replace ('.', '-')"
Perhaps I could use filters as is mentioned at https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#manipulating-strings
If I could split it using split(".") then join it again afterwards perhaps?
Upvotes: 1
Views: 1897
Reputation: 7350
If I could split it using split(".") then join it again afterwards perhaps?
You could very well do that!
E.g.:
- set_fact:
joomlaversion2: "{{ joomlaversion.split('.') | join('-') }}"
Or, use regex_replace
, which will find a pattern and replace it with hyphen.
# since . matches any char, we need to escape it with \
- set_fact:
joomlaversion2: "{{ joomlaversion | regex_replace('\.', '-') }}"
Upvotes: 2
Reputation: 17037
you have to add {{ }} and escape . with \:
- set_fact:
joomlaversion: "4.0.2"
- set_fact:
joomlaversion2: "{{ joomlaversion | regex_replace('\\.', '-') }}"
- debug:
var: joomlaversion2
result:
ok: [localhost] => {
"joomlaversion2": "4-0-2"
}
Upvotes: 1