dynamic variable help

38 views
Skip to first unread message

Manoj Chander

unread,
May 20, 2021, 6:43:07 AM5/20/21
to Ansible Project

Hi , 

Im a newbie learnig ansible .im stuck with the below 


I need to get input from user and based on input  dynamically create a url .

input group1 is a single value and  group2 is a list 

using  with_items and loop im able to create variable but couldnt register it and  pass it to get_url module . below is one of my failed attempts .

ansible-playbook -e "app_name=group1 or app_name=group2 ...."

---
- name: Block to Deploy on Servers
  block:
        - set_fact:
     artifact_url: "https://my.url"
        - set_fact:

             group_id: "{% if app_name == 'group1' %}/com/connectors/{% elif app_name == 'group2' %}
             /com/connectors/{% else %}/com/services/{% endif %}"
             register: group_id 
 
   - set_fact:
            artifacturl: "{ artifact_url }}{{ group_id }}{{ item }}/{{ app_version }}/{{ item }}-{{ app_version }}.jar"
when: app_name == group2
loop:
   - jar1
   - jar2 
register: artifacturl
   - set_fact:
            artifacturl: "{ artifact_url }}{{ group_id }}{{ app_name }}/{{ app_version }}/{{ app_name }}-{{ app_version }}.jar"
            when: app_name != group2

pass the constructed variable to another block 

- name: Block to get file "{{app_name}}"
  block:
        - name: download "{{app_name}}" form url
          get_url:
            url:  "{{ artifacturl }}"
            dest: "/manual/{{ app_name }}.jar"
  loop:
     - {{ artifacturl }}

André Oswald

unread,
May 21, 2021, 2:09:29 AM5/21/21
to Ansible Project
Hi,

for this to work artifacturl always needs to be a list. A first attempt could look like this:

---
- name: Block to Deploy on Servers
  block:
        - set_fact:
     artifact_url: "https://my.url"
        - set_fact:

             group_id: "{% if app_name == 'group1' %}/com/connectors/{% elif app_name == 'group2' %}
             /com/connectors/{% else %}/com/services/{% endif %}"
             register: group_id 
 
   - set_fact:
            artifacturls: 
            - "{ artifact_url }}{{ group_id }}jar1/{{ app_version }}/{{ item }}-{{ app_version }}.jar"
            - "{ artifact_url }}{{ group_id }}jar2/{{ app_version }}/{{ item }}-{{ app_version }}.jar"
      when: app_name == group2
 
   - set_fact:
            artifacturls: 
             - "{ artifact_url }}{{ group_id }}{{ app_name }}/{{ app_version }}/{{ app_name }}-{{ app_version }}.jar"
      when: app_name != group2

pass the constructed variable to another block 

- name: Block to get file "{{app_name}}"
  block:
        - name: download "{{app_name}}" form url
          get_url:
            url:  "{{ item }}"
            dest: "/manual/{{ app_name }}.jar"
  loop:
     - {{ artifacturls }}

This leads to a bit of duplication for group2 which could be omitted by a more complex construct like this:

   - set_fact:
            artifacturls: "({{ artifact_urls | default([]))  + {{ artifact_url }}{{ group_id }}{{ item }}/{{ app_version }}/{{ item }}-{{ app_version }}.jar"
      when: app_name == group2
      loop:
      - jar1
      - jar2 


Hope that helps,
André

Manoj Chander

unread,
May 24, 2021, 9:46:54 AM5/24/21
to Ansible Project
Greetings  André,

Thanks you so much for ur time. 

The above solution works but i have some 100 jars to download .

Is it possible  to set_fact  in a loop store the constructed variable  in a list and pass it to  get_url  method .  

---
- include_vars:
   file: vars.yml
- set_fact:
    b_url: http://{{item}}/sr/pr/
 # jar is a list in vars.yml
 loop: "{{ jars }}" 
 register: b_url 
- debug:
    msg: "no of sites are ==>   {{ b_url  }}  "
- name: Container Checks
 block:
       - name: download "{{file }}"
         get_url:
           url:  "{{ item }}"
           dest: "/manual/jar/"
   loop: {{b_url}}

regards,
Manoj

Stefan Hornburg (Racke)

unread,
May 24, 2021, 11:56:10 AM5/24/21
to ansible...@googlegroups.com
On 5/24/21 3:46 PM, Manoj Chander wrote:
> Greetings  André,
>
> Thanks you so much for ur time. 
>
> The above solution works but i have some 100 jars to download .
>
> Is it possible  to set_fact  in a loop store the constructed variable  in a list and pass it to  *get_url*  method .  
>
> ---
> - include_vars:
>    file: vars.yml
> - set_fact:
>     b_url: http://{{item}}/sr/pr/
>  # jar is a list in vars.yml
>  loop: "{{ jars }}" 
>  register: b_url 
> - debug:
>     msg: "no of sites are ==>   {{ b_url  }}  "
> - name: Container Checks
>  block:
>        - name: download "{{file }}"
>          get_url:
>            url:  "{{ item }}"
>            dest: "/manual/jar/"
>    loop: {{b_url}}
>

Hello Manoj,

1) Using register in a set_fact task doesn't make sense, as set_fact is supposed to set the
variables.

2) Do you really need to use set_fact?

- name: download "{{file }}"
get_url:
url: "http://{{item}}/sr/pr/"
dest: "/manual/jar/"
loop: "{{ jars }}"

Regards
Racke

> regards,
> Manoj
>
> On Friday, May 21, 2021 at 11:39:29 AM UTC+5:30 André Liebigt wrote:
>
> Hi,
>
> for this to work artifacturl always needs to be a list. A first attempt could look like this:
>
> *---*
> *- name: Block to Deploy on Servers*
> *  block:*
> *        - set_fact:*
> *     artifact_url: "https://my.url <https://my.url/>"*
> *        - set_fact:*
> *
> *
> *             group_id: "{% if app_name == 'group1' %}/com/connectors/{% elif app_name == 'group2' %}*
> *             /com/connectors/{%
else %}/com/services/{% endif %}"*
> *             register: group_id *
> * *
> *   - set_fact:*
> *            artifacturls: *
> *            - "{ artifact_url }}{{ group_id }}jar1/{{ app_version }}/{{ item }}-{{ app_version }}.jar"*
> *            **- "{ artifact_url
}}{{ group_id }}jar2/{{ app_version }}/{{ item }}-{{ app_version }}.jar"*
> *      when: app_name == group2*
> * *
> **
> *   - set_fact:*
> *            artifacturls: *
> *             - "{ artifact_url }}{{ group_id }}{{ app_name }}/{{ app_version }}/{{ app_name }}-{{ app_version }}.jar"*
> *      when: app_name != group2*
>
> /pass the constructed variable to another block /
>
> *- name: Block to get file "{{app_name}}"*
> *  block:*
> *        - name: download "{{app_name}}" form url*
> *          get_url:*
> *            url:  "{{ item }}"*
> *            dest: "/manual/{{ app_name }}.jar"*
> *  loop:*
> *     - {{ artifacturls }}*
>
> This leads to a bit of duplication for group2 which could be omitted by a more complex construct like this:
>
> *   - set_fact:*
> *            artifacturls: "({{ artifact_urls | default([]))  + {{ artifact_url }}{{ group_id }}{{ item }}/{{
> app_version }}/{{ item }}-{{ app_version }}.jar"*
> *      when: app_name == group2*
> *      loop:*
> *      - jar1*
> *      - jar2 *
> *
> *
>
> Hope that helps,*
> *
> André
> On Thursday, May 20, 2021 at 12:43:07 PM UTC+2 cmanoj....@gmail.com
wrote:
>
>
> Hi , 
>
> Im a newbie learnig ansible .im stuck with the below 
>
>
> /I need to get input from user and based on input  dynamically create a url ./
> /
> /
> /input group1 is a single value and  group2 is a list /
> /
> /
> /using  with_items and loop im able to create variable but
couldnt register it and  pass it to get_url module .
> below is one of my failed attempts ./
> /
> /
> /ansible-playbook -e "app_name=group1 or app_name=group2 ...."/
>
> *---*
> *- name: Block to Deploy on Servers*
> *  block:*
> *        - set_fact:*
> *     artifact_url: "https://my.url <https://my.url>"*
> *        - set_fact:*
> *
> *
> *             group_id: "{% if app_name == 'group1' %}/com/connectors/{% elif app_name == 'group2' %}*
> *             /com/connectors/{% else %}/com/services/{% endif %}"*
> *             register: group_id *
> * *
> *   - set_fact:*
> *            artifacturl: "{ artifact_url }}{{ group_id }}{{ item }}/{{ app_version }}/{{ item }}-{{ app_version
> }}.jar"*
> *when: app_name == group2*
> *loop:*
> *   - jar1*
> *   - jar2 *
> *register: artifacturl*
> **
> *   - set_fact:*
> *            artifacturl: "{ artifact_url }}{{ group_id }}{{ app_name }}/{{ app_version }}/{{ app_name }}-{{
> app_version }}.jar"*
> *            when: app_name != group2*
>
> /pass the constructed variable to another block /
>
> *- name: Block to get file "{{app_name}}"*
> *  block:*
> *        - name: download "{{app_name}}" form url*
> *          get_url:*
> *            url:  "{{ artifacturl }}"*
> *            dest: "/manual/{{ app_name }}.jar"*
> *  loop:*
> *     - {{ artifacturl }}*
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/9482dd18-ae18-40db-a254-6fc936adbb93n%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/9482dd18-ae18-40db-a254-6fc936adbb93n%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

OpenPGP_signature

Manoj Chander

unread,
May 30, 2021, 3:27:28 AM5/30/21
to ansible...@googlegroups.com
Hi racker, 

Yeah I understood ,I finally found a way to use loop and loop_var to build my list of variable and download them . 

Thanks for Ur help really appreciate it .

Regards,
Mano

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7d40df1e-238c-0b8a-f867-25e147ec8f92%40linuxia.de.
Reply all
Reply to author
Forward
0 new messages