I'm attempting to create an idempotent DB2 script runner using ansible. My variables:
---
db2sql_hard_vars:
command_groups:
- description: first set of commands
dont_run_sql: 'select * from test'
commands:
-
sql: connect to master
-
sql: drop table test
ignore_errors: True
-
sql: >
create table
test(col1 char)
-
sql: select * from test
- description: second set of commands
dont_run_sql: 'select * from test1'
commands:
-
sql: connect to master
-
sql: drop table test1
ignore_errors: True
-
sql: >
create table1
test(col1 char)
-
sql: select * from test1
My role's main.yml task:
---
- name: check if the command group should be run
shell: db2 -v "{{ item.dont_run_sql }}"
with_items: "{{ db2sql_hard_vars.command_groups }}"
register: dont_run_sql
ignore_errors: True
- name: run the sql commands
# shell: db2 -v "{{ item.1.sql }}" {{ '|| true' if item.1.ignore_errors is defined and item.1.ignore_errors else '' }}
debug: var=item.1
when: item.0.rc != 0
with_subelements:
- dont_run_sql.results
- item.commands
However, I'm getting the below error. This is due to the fact that with_subelements doesn't allow the subelement property to be nested in Ansible 1.9.x (fixed in 2.0
https://github.com/ansible/ansible/issues/9863). Is there a way to accomplish the same in Ansible 1.9.2? Additionally, any comments on my DB2 script runner approach - good idea or bad idea? Also, I realise that my drop table statements are unnecessary due to the dont_run_sql check, but I've added it to illustrate the ignore_errors variable.
could not find 'item.commands' key in iterated item '{u'changed': True, u'stdout': u'', u'delta': u'0:00:00.001632', 'stdout_lines': [], u'end': u'2015-09-18 12:24:05.882970', 'item': {'commands': [{'sql': 'connect to master'}, {'ignore_errors': True, 'sql': 'drop table test'}, {'sql': 'create table test(col1 char)\n'}, {'sql': 'select * from test'}], 'description': 'first set of commands', 'dont_run_sql': 'select * from test'}, u'cmd': u'db2 -v "select * from test"', u'start': u'2015-09-18 12:24:05.881338', u'stderr': u'/bin/sh: 1: db2: not found', u'rc': 127, 'invocation': {'module_name': u'shell', 'module_args': u'db2 -v "select * from test"'}}'