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:
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)
shell: psql -f install_{{ item }}.sql
register: install_result
shell: source check_install.sh;
register: check_install_result
failed_when: “(‘ERROR’ in check_install_result.stdout)”
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