1

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?

TryHarder
  • 2,704
  • 8
  • 47
  • 65

2 Answers2

2

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('\.', '-') }}"
seshadri_c
  • 6,906
  • 2
  • 10
  • 24
  • Thanks. I tested both solutions, but found that both the split function and the regex_replace were appended to the value. For example, the value became `joomlaversion.split('.') | join('-')"` joomlaversion should be replaced with it's value, so I also tried using `"{{joomlaversion}}.split('.') | join('-')"` which gave me `4.0.2.split('.') | join('-')" ` but again the split was appended rather than executed. Any ideas why? – TryHarder Nov 02 '21 at 10:06
  • 1
    Both of the above worked, but only after changing them to `joomlaversion2: "{{ joomlaversion.split('.') | join('-')}}"` or `joomlaversion2: "{{ joomlaversion | regex_replace('\\.', '-') }}"` Thanks. – TryHarder Nov 02 '21 at 10:53
  • Sorry, my bad. I forgot to add the jinja `{{ .. }}` delimiters around the variables. My example *was* just working on text `joomlaversion`. (edited now) – seshadri_c Nov 02 '21 at 11:06
1

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"
}
Frenchy
  • 16,386
  • 3
  • 16
  • 39