0

I have dictionary to loop multiple strings in the list, if I provide 2 or more then it's always reading last value in the list, please suggest me.

- set_fact:
    env_microservice_variable_map: |
        {% set res = [] -%}
        {% for microservice_name in MICROSERVICE_NAMES -%}
          {% if microservice_name in MICROSERVICE_ENV_MAP -%}
            {% set microservice_envs = MICROSERVICE_ENV_MAP[microservice_name] -%}
          {% else -%}
            {% set microservice_envs = env_variable_map.keys() -%}
          {% endif -%}

          {% for env in microservice_envs -%}
            {% set variables = env_variable_map[env] -%}
            {% set ignored = variables.__setitem__("MICROSERVICE_NAME", microservice_name) -%}
            {% set ignored = res.extend([variables]) -%}
          {%- endfor %}
        {%- endfor %}
        {{ res }}


- name: Copy values file
  command: cp {{dir_path}}/helm/{{item.MICROSERVICE_NAME}}/values-template.yaml {{dir_path}}/helm/{{item.MICROSERVICE_NAME}}/values-{{item.EXEC_ENV}}-{{item.EXEC_REGION}}.yaml
  with_items: "{{ env_microservice_variable_map }}"
  become_user: jenkins

First one is set_fact where it has mapping. The 2nd task should be able to loop when we have multiple strings in the variable defined "MICROSERVICE_NAMES"

There is ansible command am running, it's always reading last string in the List(read-service), please help me, thanks.

ansible-playbook generate_values_files.yml -i hosts  --extra-vars "@generate_values_files_variable.yml" --extra-vars="{"'"MICROSERVICE_NAMES"'":{'processor-create','processor-update','read-service'}}" '--extra-vars={"MICROSERVICE_ENV_MAP":{}}'



    Varibales:

    dir_path: /jenkins
    EXEC_ENV: dd
    EXEC_REGION: west


     Basically we have multiple directories
     1. /jenkins/helm/processor-create/values-template.yml
     2.  /jenkins/helm/processor-update/values-template.yml
     3.  /jenkins/helm/read-service/values-template.yml
    Each folder has values-template.yml file init when i run above script it has to create multiple files based above template file in each folder.
      1. /jenkins/helm/processor-create/values-template.yml
         values-dd-west.yml
         values-mm-west.yml
         values-gg-west.yml
      2. /jenkins/helm/processor-update/values-template.yml
         values-dd-west.yml
         values-mm-west.yml
         values-gg-west.yml
      3. /jenkins/helm/read-service/values-template.yml
         values-dd-west.yml
        values-mm-west.yml
        values-gg-west.yml

Problem here is when i run above ansible tasks it's always generating files for last service in the list :"read-service".

4

1 Answer 1

0

I suspect you found the well-known (to those who was unlucky to find it) WTF of the jinja2.

If you set some variables inside the loop, they live only within this loop. You need to initialize a container (list or dict) outside of the loop and add items into it to get something out of the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.