Re: [ansible-project] retries/loop for a bunch of tasks

2024-04-03 Thread Dick Visser
You could put the tasks from the block in a separate file, and then
include that with include_tasks.
That does allow you to loop over it.

On Wed, 3 Apr 2024 at 14:37, Azadeh Amirhosseini
 wrote:
>
> Hi All,
>
> how can I implement 'retries:' for a bunch of tasks. Unfortunately  it does 
> not work with 'block:' and i get the error: 'ERROR! 'until' is not a valid 
> attribute for a block'. Any idea or trick to get the result is highly 
> welocomed.
>
> tasks:
> - name: "execute show commands before Update"
> nxos_command:
> commands: "{{ item }}"
> ignore_errors: true
> with_items: "{{ todo }}"
> register: before_install
>
> - set_fact:
> out_before: "{{ out_before + [item.item] + item.stdout }}"
> with_items: "{{ before_install.results }}"
>
> # more tasks come here to do the Software Update...
>
> - name: "Check Output after update"
> block:
> - name: "execute show commands after Update"
> nxos_command:
> commands: "{{ item }}"
> ignore_errors: true
> with_items: "{{ todo2 }}"
> register: after_install
>
> - set_fact:
> error_data: "{{ 'false' if before_install == after_install else 'true'}}"
>
> retries: 3
> delay: 300
> until: error_data == 'false'
>
> --
> 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-project+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/ansible-project/2ab45753-43fe-431e-8ed4-891dde801db8n%40googlegroups.com.

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAF8BbLZnjfC5skj27pP_8-cwSJ-JZu52jWoZeiW7o70hvVxHsw%40mail.gmail.com.


[ansible-project] retries/loop for a bunch of tasks

2024-04-03 Thread Azadeh Amirhosseini
Hi All,

how can I implement 'retries:' for a bunch of tasks. Unfortunately  it does 
not work with 'block:' and i get the error: 'ERROR! 'until' is not a valid 
attribute for a block'. Any idea or trick to get the result is highly 
welocomed.

tasks:
- name: "execute show commands before Update"
nxos_command:
commands: "{{ item }}"
ignore_errors: true
with_items: "{{ todo }}"
register: before_install

- set_fact:
out_before: "{{ out_before + [item.item] + item.stdout }}"
with_items: "{{ before_install.results }}"

# more tasks come here to do the Software Update...

- name: "Check Output after update"
block:
- name: "execute show commands after Update"
nxos_command:
commands: "{{ item }}"
ignore_errors: true
with_items: "{{ todo2 }}"
register: after_install

- set_fact:
error_data: "{{ 'false' if before_install == after_install else 'true'}}"

retries: 3
delay: 300
until: error_data == 'false'

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/2ab45753-43fe-431e-8ed4-891dde801db8n%40googlegroups.com.


Re: [ansible-project] Allow and skip empty loop

2024-04-03 Thread Vladimir Botka
On Wed, 3 Apr 2024 12:11:34 +0300
Adrian Sevcenco  wrote:

> delete_groups:
> 
> loop: "{{ delete_groups }}"
> 
> The error that i receive is:
> Invalid data passed to 'loop', it requires a list, got this instead: None.

Use the filter *default*

  loop: "{{ delete_groups | default([], true) }}"

See:
https://jinja.palletsprojects.com/en/3.1.x/templates/#jinja-filters.default

Optionally, use the module *ansible.utils.validate*
See:
https://docs.ansible.com/ansible/latest/collections/ansible/utils/validate_module.html


-- 
Vladimir Botka

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20240403140804.117e8718%40planb.


pgpf3SIGgcqB_.pgp
Description: OpenPGP digital signature


Re: [ansible-project] Allow and skip empty loop

2024-04-03 Thread 'Chiku' via Ansible Project

Writing delete_groups: like that it mean empty string type.

So you need to write like delete_groups: [] as empty list type.


Le 03/04/2024 à 11:11, Adrian Sevcenco a écrit :



Hi! I would like to have an task that would delete groups
ONLY if the variable have a content ...

so i have an delete_users_groups.yml:
---
delete_groups:

delete_users:


and an users_task.yml with

- name: Delete groups on nodes
ansible.builtin.group:
name: "{{ group_del_name }}"
state: absent
loop_control:
loop_var: group_del_name
loop: "{{ delete_groups }}"

and a playbook with:

---
- hosts: "{{ target | default('all') }}"
gather_facts: False
vars_files:
- "{{ playbook_dir }}/../additions/users/user_group_info.yml"
- "{{ playbook_dir }}/../additions/users/delete_users_groups.yml"

tasks:
- include_tasks: tasks/users_task.yml


The error that i receive is:
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 tried to add wantlist = True and i got
template error while templating string: expected token 'end of print 
statement', got '='. String: {{ delete_groups, wantlist=True }}


Any idea how can the loop pe skiped if the target of the loop is empty?

Thanks a lot!
Adrian
--
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAOL4_EXP6xjdOsxKmB_%3D3dQa41B3kdRdR%3DLutuJab0wsxr4PJw%40mail.gmail.com 
.


--
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/80005a87-0428-4a5d-b44d-f6569db2c6c5%40tsgri13.fr.


[ansible-project] Allow and skip empty loop

2024-04-03 Thread Adrian Sevcenco
Hi! I would like to have an task that would delete groups
ONLY if the variable have a content ...

so i have an delete_users_groups.yml:
---
delete_groups:

delete_users:


and an users_task.yml with

- name: Delete groups on nodes
ansible.builtin.group:
name: "{{ group_del_name }}"
state: absent
loop_control:
loop_var: group_del_name
loop: "{{ delete_groups }}"

and a playbook with:

---
- hosts: "{{ target | default('all') }}"
gather_facts: False
vars_files:
- "{{ playbook_dir }}/../additions/users/user_group_info.yml"
- "{{ playbook_dir }}/../additions/users/delete_users_groups.yml"

tasks:
- include_tasks: tasks/users_task.yml


The error that i receive is:
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 tried to add wantlist = True and i got
template error while templating string: expected token 'end of print
statement', got '='. String: {{ delete_groups, wantlist=True }}

Any idea how can the loop pe skiped if the target of the loop is empty?

Thanks a lot!
Adrian

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAOL4_EXP6xjdOsxKmB_%3D3dQa41B3kdRdR%3DLutuJab0wsxr4PJw%40mail.gmail.com.