Я пытаюсь перебрать словарь "ключ": & "Значение". Я не получаю правильных результатов. Я хочу напечатать значение определенного ключа из словаря.
Вот мой код:
- hosts: all
gather_facts: false
strategy: free
tasks:
- include_vars: roles/oracle/vars/install_vars.yaml
vars:
var_list:
- script_name
- set_fact:
ORACLE_HOMES_DIR: "/u01/app/oracle/product"
DB_HOME: "{{ ORACLE_HOMES_DIR }}/{{ ORACLE_VERSION }}/dbinst_1"
- name: Copy script to host
copy:
src: "{{ playbook_dir }}/{{ script_name }}"
dest: "/tmp/"
owner: "{{ USER_ORACLE }}"
group: "{{ GROUP_ORACLE }}"
mode: 0755
- name: Verify if the DB is open READ WRITE (or) not
become_user: "{{ USER_ORACLE }}"
environment:
ORACLE_SID: "{{ sid }}"
ORACLE_HOME: "{{ ORACLE_HOME }}"
shell: "echo \"set pagesize 0\n select trim(open_mode) from v\\$database;\" | {{ORACLE_HOME}}/bin/sqlplus -S / as sysdba"
with_items: "{{ hostvars[inventory_hostname]['sid_list'] }}"
loop_control:
loop_var: sid
register: om
- name: Get the OM output
debug:
msg: "om Results {{ item.0.sid }} is {{ item.1.value }}"
with_dict: "{{ om.results }}"
when: ("'sid' in item.key" and "'stdout' in item.key")
Я пытаюсь напечатать все значения для ключей "sid" и "stdout" здесь, ниже вывод словаря. Поэтому я могу использовать условие цикла для циклического выполнения запросов SQL для всех идентификаторов безопасности, которые находятся в режиме «READ WRITE».
ok: [dwracdb1] => (item={'value': True, 'key': '_ansible_parsed'}) => {
"msg": "om Results _ansible_parsed is True"
}
ok: [dwracdb1] => (item={'value': [], 'key': 'stderr_lines'}) => {
"msg": "om Results stderr_lines is []"
}
ok: [dwracdb1] => (item={'value': True, 'key': '_ansible_item_result'}) => {
"msg": "om Results _ansible_item_result is True"
}
ok: [dwracdb1] => (item={'value': u'2019-05-17 14:15:02.065806', 'key': u'end'}) => {
"msg": "om Results end is 2019-05-17 14:15:02.065806"
}
ok: [dwracdb1] => (item={'value': False, 'key': '_ansible_no_log'}) => {
"msg": "om Results _ansible_no_log is False"
}
ok: [dwracdb1] => (item={'value': u'READ WRITE', 'key': u'stdout'}) => {
"msg": "om Results stdout is READ WRITE"
}
ok: [dwracdb1] => (item={'value': u'echo "set pagesize 0\nselect trim(open_mode) from v\\$database;" | /u01/app/oracle/product/11.2.0/dbinst_1/bin/sqlplus -S / as sysdba', 'key': u'cmd'}) => {
"msg": "om Results cmd is echo \"set pagesize 0\nselect trim(open_mode) from v\\$database;\" | /u01/app/oracle/product/11.2.0/dbinst_1/bin/sqlplus -S / as sysdba"
}
ok: [dwracdb1] => (item={'value': 0, 'key': u'rc'}) => {
"msg": "om Results rc is 0"
}
ok: [dwracdb1] => (item={'value': {u'module_args': {u'warn': True, u'executable': None, u'_uses_shell': True, u'_raw_params': u'echo "set pagesize 0\nselect trim(open_mode) from v\\$database;" | /u01/app/oracle/product/11.2.0/dbinst_1/bin/sqlplus -S / as sysdba', u'removes': None, u'argv': None, u'creates': None, u'chdir': None, u'stdin': None}}, 'key': u'invocation'}) => {
"msg": "om Results invocation is {u'module_args': {u'warn': True, u'executable': None, u'_uses_shell': True, u'_raw_params': u'echo \"set pagesize 0\\nselect trim(open_mode) from v\\\\$database;\" | /u01/app/oracle/product/11.2.0/dbinst_1/bin/sqlplus -S / as sysdba', u'removes': None, u'argv': None, u'creates': None, u'chdir': None, u'stdin': None}}"
}
ok: [dwracdb1] => (item={'value': False, 'key': 'failed'}) => {
"msg": "om Results failed is False"
}
ok: [dwracdb1] => (item={'value': u'0:00:00.072933', 'key': u'delta'}) => {
"msg": "om Results delta is 0:00:00.072933"
}
ok: [dwracdb1] => (item={'value': u'', 'key': u'stderr'}) => {
"msg": "om Results stderr is "
}
ok: [dwracdb1] => (item={'value': True, 'key': u'changed'}) => {
"msg": "om Results changed is True"
}
ok: [dwracdb1] => (item={'value': u'abhitest1', 'key': u'sid'}) => {
"msg": "om Results sid is abhitest1"
}
ok: [dwracdb1] => (item={'value': [u'READ WRITE'], 'key': u'stdout_lines'}) => {
"msg": "om Results stdout_lines is [u'READ WRITE']"
}
ok: [dwracdb1] => (item={'value': u'2019-05-17 14:15:01.992873', 'key': u'start'}) => {
"msg": "om Results start is 2019-05-17 14:15:01.992873"
}
ok: [dwracdb1] => (item={'value': None, 'key': '_ansible_ignore_errors'}) => {
"msg": "om Results _ansible_ignore_errors is "
}
ok: [dwracdb1] => (item={'value': u'abhitest1', 'key': u'_ansible_item_label'}) => {
"msg": "om Results _ansible_item_label is abhitest1"
}
ok: [dwracdb1] => (item={'value': True, 'key': '_ansible_parsed'}) => {
"msg": "om Results _ansible_parsed is True"
}
ok: [dwracdb1] => (item={'value': [], 'key': 'stderr_lines'}) => {
"msg": "om Results stderr_lines is []"
}
ok: [dwracdb1] => (item={'value': True, 'key': '_ansible_item_result'}) => {
"msg": "om Results _ansible_item_result is True"
}
ok: [dwracdb1] => (item={'value': u'2019-05-17 14:15:05.315947', 'key': u'end'}) => {
"msg": "om Results end is 2019-05-17 14:15:05.315947"
}
ok: [dwracdb1] => (item={'value': False, 'key': '_ansible_no_log'}) => {
"msg": "om Results _ansible_no_log is False"
}
ok: [dwracdb1] => (item={'value': u'READ WRITE', 'key': u'stdout'}) => {
"msg": "om Results stdout is READ WRITE"
}
ok: [dwracdb1] => (item={'value': u'echo "set pagesize 0\nselect trim(open_mode) from v\\$database;" | /u01/app/oracle/product/11.2.0/dbinst_1/bin/sqlplus -S / as sysdba', 'key': u'cmd'}) => {
"msg": "om Results cmd is echo \"set pagesize 0\nselect trim(open_mode) from v\\$database;\" | /u01/app/oracle/product/11.2.0/dbinst_1/bin/sqlplus -S / as sysdba"
}
ok: [dwracdb1] => (item={'value': 0, 'key': u'rc'}) => {
"msg": "om Results rc is 0"
}
ok: [dwracdb1] => (item={'value': {u'module_args': {u'warn': True, u'executable': None, u'_uses_shell': True, u'_raw_params': u'echo "set pagesize 0\nselect trim(open_mode) from v\\$database;" | /u01/app/oracle/product/11.2.0/dbinst_1/bin/sqlplus -S / as sysdba', u'removes': None, u'argv': None, u'creates': None, u'chdir': None, u'stdin': None}}, 'key': u'invocation'}) => {
"msg": "om Results invocation is {u'module_args': {u'warn': True, u'executable': None, u'_uses_shell': True, u'_raw_params': u'echo \"set pagesize 0\\nselect trim(open_mode) from v\\\\$database;\" | /u01/app/oracle/product/11.2.0/dbinst_1/bin/sqlplus -S / as sysdba', u'removes': None, u'argv': None, u'creates': None, u'chdir': None, u'stdin': None}}"
}
ok: [dwracdb1] => (item={'value': False, 'key': 'failed'}) => {
"msg": "om Results failed is False"
}
ok: [dwracdb1] => (item={'value': u'0:00:00.070937', 'key': u'delta'}) => {
"msg": "om Results delta is 0:00:00.070937"
}
ok: [dwracdb1] => (item={'value': u'', 'key': u'stderr'}) => {
"msg": "om Results stderr is "
}
ok: [dwracdb1] => (item={'value': True, 'key': u'changed'}) => {
"msg": "om Results changed is True"
}
ok: [dwracdb1] => (item={'value': u'dw1', 'key': u'sid'}) => {
"msg": "om Results sid is dw1"
}
ok: [dwracdb1] => (item={'value': [u'READ WRITE'], 'key': u'stdout_lines'}) => {
"msg": "om Results stdout_lines is [u'READ WRITE']"
}
ok: [dwracdb1] => (item={'value': u'2019-05-17 14:15:05.245010', 'key': u'start'}) => {
"msg": "om Results start is 2019-05-17 14:15:05.245010"
}
ok: [dwracdb1] => (item={'value': None, 'key': '_ansible_ignore_errors'}) => {
"msg": "om Results _ansible_ignore_errors is "
}
ok: [dwracdb1] => (item={'value': u'dw1', 'key': u'_ansible_item_label'}) => {
"msg": "om Results _ansible_item_label is dw1"