Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

complex loop in ansible with_together

Tags:

ansible

How do I skip empty item in list if I use with_together?

see the code below:

- name: get data_files list
  shell: ls -l data_files | awk -F " " {'print $9'}
  register: csv_file_list
- debug: var=csv_file_list

- name: get table name list
  shell: ls -l data_files/ | awk -F " " {'print $9'} | sed -e "s/.csv//g" | sed -e "s/-/./g"
  register: table_list
- debug: var=table_list

- name: copy table from csv to demo db
  shell: psql -U postgres -d demo -c "\copy {{ item.1 }} from /home/ubuntu/data_files/{{ item.0 }} DELIMITER ',' CSV HEADER"
  with_together:
    - csv_file_list.stdout_lines
    - table_list.stdout_lines
  when: {{ item.1 }} != ''
like image 299
yaniv refael Avatar asked Feb 24 '16 15:02

yaniv refael


People also ask

What is {{ item }} Ansible?

item is not a command, but a variable automatically created and populated by Ansible in tasks which use loops. In the following example: - debug: msg: "{{ item }}" with_items: - first - second. the task will be run twice: first time with the variable item set to first , the second time with second .

How do you use nested loops in Ansible?

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. Now we can create nested loops using with_nested.

Do Until loops Ansible?

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.


1 Answers

Test if item.1 is not none.

when: item.1 != None
like image 95
helloV Avatar answered Oct 01 '22 18:10

helloV