Loop over two tasks

150 views
Skip to first unread message

chen Augustin

unread,
Oct 22, 2014, 2:49:51 AM10/22/14
to ansible...@googlegroups.com

I have a play which has two tasks, something looks like the following snippet of code. The Task2 has dependency on the result of Task 1. And I want to loop over the two tasks, is it feasible? It appears the ‘with_items’ only loops over one task. The ‘include + with_items’ seems to be a solution, but it gets deprecated since Ansible 1.6. Do you have other solutions? Please advise.

 

- name: Task 1

  Module 1:

  register: result

 

- name: Task 2

  Module 2:

    Parameter:  “{{ result }}” 

Michael DeHaan

unread,
Oct 23, 2014, 12:12:08 PM10/23/14
to ansible...@googlegroups.com
Can you explain your use case in less abstract ways?

--
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-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2fbfed74-23bb-4152-b588-836caafdc4b8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Cindy Lamm

unread,
Dec 17, 2014, 7:51:29 AM12/17/14
to ansible...@googlegroups.com
I have as well the need to loop over a set of tasks. In my case it's about incremental updating of the schema in a database. I know there are tools like Flyway and schema-evolution-manager that would easily do the trick but they're not wished to be used in this project for different reasons. So I'd like to imitate what these tools are doing in Ansible.

Here is what I have so far:

Content of tasks/main.yml:
- name: get info which version is installed
  shell: psql -A -t -c "SELECT version_name FROM schema_deploy_version WHERE version_rank IN (SELECT MAX(version_rank) FROM schema_deploy_version)"
  register: installed_version_info

- include: incremental_install.yml
  when: (item | version_compare('{{ installed_version_info.stdout }}', '>')) and (item | version_compare('{{ target_version }}', '<='))
  with_items:
    - "{{ versions }}"

Content of vars/main.yml:
versions: ['4.5.0.0.002', '4.6.0.0.001', '4.7.0.0.004']

Content of tasks/incremental_install.yml: (simplified)
- name: install increment
  shell: psql -f install_{{ item }}.sql 
  register: install_result

- name: check install
  shell: source check_install.sh;
  register: check_install_result
  failed_when: "('ERROR' in check_install_result.stdout)"

- name: set schema version
  shell: psql -c "INSERT INTO schema_deploy_version(version_name) VALUES ('{{ item }}')"


Problem is that "include" and "with_items" is deprecated and not possible anymore. Putting the looping inside incremental_install.yml to each task would not work because in case the currently installed version is '4.5.0.0.002' and the target version is '4.7.0.0.004' it would run try to run each task individually first for version='4.6.0.0.001' and then for version='4.7.0.0.004' - but I only want to install version='4.7.0.0.004' after I checked that version='4.6.0.0.001' was ok. Thus the need to loop over a set of tasks.

http://stackoverflow.com/questions/24625539/ansible-1-6-include-with-items-deprecated recommends furthermore to write the tasks into a module. Or, since shell scripts are standard for the project I'm involved in, just write a shell script that encapsulates these 3 tasks and then loop over the shell task. 

Any other solutions?


Cheers,
Cindy
Reply all
Reply to author
Forward
0 new messages