Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible how to skip the loop if the list is empty

I'm running Ansible and I try to make this task work. I have defined the default of the variable "docker_registries" to be an empty list: docker_registries: [] I noticed that I will get error when running the ansible playbook if the list is empty. this is the error I get:

fatal: [***.cloudapp.azure.com]: FAILED! => {"msg": "Invalid data passed to 'loop', it requires a list, got this instead: None. Hint: If you passed a list/dict of just one element, try adding wantlist=True to your lookup invocation or use q/query instead of lookup."}

I'm trying to add the condition that if the "docker_registries" is empty the task continues without raising an error. this is the code for this task:

- name: Log into additional docker registries, when required
  command: docker login -u {{item.username}} -p {{item.password}} {{item.server}}
  become: true
  loop: "{{docker_registries}}"

I tried to change the loop to loop: "{{ lookup(docker_registries, {'skip_missing': True})}}" but I get the error

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'NoneType' object has no attribute 'lower' fatal: [***.cloudapp.azure.com]: FAILED! => {"msg": "Unexpected failure during module execution.", "stdout": ""}

I'm pretty new with this. Can anyone help?

like image 498
atefeh Avatar asked Mar 17 '20 15:03

atefeh


People also ask

How do you skip in Ansible?

If you assign the always tag to a task or play, Ansible will always run that task or play, unless you specifically skip it ( --skip-tags always ). Fact gathering is tagged with 'always' by default. It is only skipped if you apply a tag and then use a different tag in --tags or the same tag in --skip-tags .

How do I Undefine an Ansible variable?

No, there is no way to unset a variable (top level) in Ansible. The only thing you can do, is to create a dictionary and store the variable as a key in this dictionary. Clearing the "parent" dictionary will essentially make the dictionary. key is defined conditional expression work.

Does Ansible do until loop?

To use this loop in task you essentially need to add 3 arguments to your task arguments: until - condition that must be met for loop to stop. That is Ansible will continue executing the task until expression used here evaluates to true. retry - specifies how many times we want to run the task before Ansible gives up.

How do you use multiple loops in Ansible?

Ansible's syntax also supports the idea of nested looping. Nested loops in many ways are similar in nature to a set of arrays that would be iterated over using the with_nested operator. Nested loops provide us with a succinct way of iterating over multiple lists within a single task.


1 Answers

You could use the when statement in combinaison with the iterable Jinja test.
Because, well, if you want to loop over your variable, then it should be iterable.

- name: Log into additional docker registries, when required
  command: docker login -u {{ item.username }} -p {{ item.password }} {{ item.server }}
  become: true
  loop: "{{ docker_registries }}"
  when: docker_registries is iterable

Based on the content of the variable, the when condition could still not do the trick, because the validity of the data passed to the loop will be considered first.

Now this said, you can use a conditional expression in the loop declaration itself to come around this issue:

- name: Log into additional docker registries, when required
  command: docker login -u {{ item.username }} -p {{ item.password }} {{ item.server }}
  become: true
  loop: "{{ docker_registries if docker_registries is iterable else [] }}"

Also mind that a string is an iterable in Python, as they are, simply put, a list of characters.

So you might want to go:

- name: Log into additional docker registries, when required
  command: docker login -u {{ item.username }} -p {{ item.password }} {{ item.server }}
  become: true
  loop: "{{ docker_registries if docker_registries is iterable and docker_registries is not string else [] }}"
like image 112
β.εηοιτ.βε Avatar answered Sep 29 '22 04:09

β.εηοιτ.βε