0

Can you tell me how to remove the comma at the end of the line?

Final output:

{name: "name1", My country = "region1a", My country = "region1b"},

{name: "name2", My country = "region2a", My country = "region2b"},

you only need to delete one (highlighted) comma at the end of the second line. The output is generated in this way

{% for country in AllСountry %}
{name: "{{ country }}",{% for count in lookup('vars', country) %} My country = "{{ count }}",{% if loop.last %} My country = "{{ count }}"{% endif %}{% endfor %}},
{% endfor %}

as a result, we need this output

{name: "name1", My country = "region1a", My country = "region1b"},
{name: "name2", My country = "region2a", My country = "region2b"}
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Alex alex
  • 116
  • 1
  • 1
  • 7
  • Have you tried anything yourself, or done any research on this subject? See [here](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). – Take-Some-Bytes Nov 26 '20 at 04:56
  • Yes, of course. Replace can't be applied, and loop. last works for the entire cycle. Perhaps I don't have so much experience that the solution to this issue was obvious. – Alex alex Nov 26 '20 at 05:31

1 Answers1

1

You could use loop.last for this, and so enclose you comma in a condition testing if you are looping on the last item or not.

You template would then end up being:

{% for country in AllСountry %}
{name: "{{ country }}",{% for count in lookup('vars', country) %} My country = "{{ count }}",{% if loop.last %} My country = "{{ count }}"{% endif %}{% endfor %}}{% if not loop.last %},{% endif %}
{% endfor %}

This said, and as raised on your other question, I feel like you are making things complex for yourself, what exactly are you trying to achieve? Are you trying to build a JSON? (in which case, please note that My country = "region2a" is invalid).

If you are indeed trying to construct a data structure, then you should create lists and dictionaries that represent those and then use simple existing filters like to_json, to_yaml, etc.

Otherwise, the point still apply: if you want to create a comma separated list of string, create a list of string in Ansible, then just join them.

For example:

- debug:
    msg: "{{ some_list | join(', ') }}"
  vars:
    some_list:
      - foo
      - bar
      - baz

Would give you

foo, bar, baz
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • Thank you very much. it really works, but you need to delete {% if loop.last %} My country = " {{ count }}" – Alex alex Nov 26 '20 at 12:50
  • In fact, I understand how to create a configuration file. In order for me to understand how to write it myself, I ask a question about a part that I don't understand and then try it myself. Thank you very much for your help. Your answers are very helpful. – Alex alex Nov 26 '20 at 12:57
  • Yep, I think generally (or that's what I grasp from your questions), you are trying to do too much in the template and Jinja, when you should actually do the processing on your data structure, then let the magic happen. – β.εηοιτ.βε Nov 26 '20 at 13:07
  • now I'm trying to figure out how to implement '{% if item in capital %} ' for both data structures listed here [link](https://stackoverflow.com/questions/65007906/ansibleundefinedvariable-no-variable-found-with-this-name-region1a) – Alex alex Nov 26 '20 at 13:15
  • it remains to implement only this [link](https://stackoverflow.com/questions/65000766/ansible-template-from-jinja2) – Alex alex Nov 26 '20 at 13:18