Skip to content
Advertisement

Ansible Replace the string variable in uri module

I want to download the filebeat7.15.2 binaries form artifactory. I am passing the version in my playbook as 7-15-2, but want to change in the playbook to 7.15.2 in uri module only. My binary is stored in example.com/artifactory/mvn-public-local/com/dbdb/rf/devops/filebeat/7-15-2/filebeat-7.15.2-linux-x86_64.tar.gz

my playbook:

- hosts: "{{ deployment_environment }}"
  remote_user: "{{ user }}"
  gather_facts: no
  
  vars_files:
    - params.yml
  vars:
    artifactory_url: "example.com/artifactory/mvn-public-local/com/dbdb/rf/devops"
    artifact_name: "filebeat"
    release_url: "{{ artifactory_url }}/{{ artifact_name }}/{{ filebeat_version }}"

  tasks:
    - set_fact:
        env_param: "{{ deployment_environment }}"

    - name: Create filebeat directory if not exist.
      file: 
        path: "{{ env_select[env_param].deployment_path }}/filebeat"
        state: directory
        mode: 0755
        recurse: yes
        
    - name: remove old scripts from box
      shell: "cd {{ env_select[env_param].deployment_path }}/filebeat; rm -rf *"
      
      
    - name: Download scripts from artifactory
      uri:
        url: "{{ release_url }}/filebeat-{{ filebeat_version }}-linux-x86_64.tar.gz"
        method: GET
        validate_certs: no
        force_basic_auth: true
        return_content: no
        force: no
        user: "{{ arti_username }}"
        password: "{{ arti_pass }}"
        dest: "{{ env_select[env_param].deployment_path }}/filebeat"
        creates: "{{ env_select[env_param].deployment_path }}/filebeat-{{ filebeat_version}}-linux-x86_64.tar.gz"

I am running the playbook like

ansible-playbook download_filebeat.yml deployment_environment user filebeat_version
ansible-playbook download_filebeat.yml uat1 user1 7-15-2

I am getting error as url not found: “url”: “example.com/artifactory/mvn-public-local/com/dbdb/rf/devops/filebeat/7-15-2/filebeat-7-15-2-linux-x86_64.tar.gz”,

Any suggest on how to change the example.com/artifactory/mvn-public-local/com/dbdb/rf/devops/filebeat/7-15-2/filebeat-7-15-2-linux-x86_64.tar.gz to example.com/artifactory/mvn-public-local/com/dbdb/rf/devops/filebeat/7-15-2/filebeat-7.15.2-linux-x86_64.tar.gz

Advertisement

Answer

Have you tried

#...
- name: Download scripts from artifactory
      uri:
        url: "{{ release_url }}/filebeat-{{ filebeat_version | regex_replace('-','.') }}-linux-x86_64.tar.gz"
        creates: "{{ env_select[env_param].deployment_path }}/filebeat-{{ filebeat_version | regex_replace('-','.') }}-linux-x86_64.tar.gz"
#...

you can also store the parsed filebeat version as it’s own variable/fact for the task similar to the following

- name: "Set filebeat semver"
  set_fact:
    filebeat_semver: "{{ filebeat_version | regex_replace('-','.') }}"

and then you can use the filebeat_semver whenever you want the parsed semantic version.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement