I have a loop which constructs in-memory targets based on list of databases found on server. These targets are assigned into different groups. The whole playbook works fine when executed from command line, but it "fails" when executed in Ansible Automation Platform (ansible 2.13.10).
This file is executed in a loop to add dynamic targets based databases detected.
UPDATED:
******************************************************
main.yml
tasks:
# detect all database installations on target host
# variable sid_list will be used inside add_db_targets.yml
- oracle_oratab:
running_only: true
register: sid_list
tags: [always]
- debug:
var: sid_list
tags: [always]
# See notes in add_db_targets.yml
- include_tasks: add_db_targets.yml
loop: "{{ ansible_play_hosts | sort() }}"
loop_control:
loop_var: outer_item
run_once: yes
tags: [always]
*******************************************************
add_db_targets.yml:
- name: Add crs/has for host {{ outer_item }}
add_host:
groups:
- "{{ 'oraclecrs' if hostvars[outer_item]['sid_list']['oracle_homes'][ hostvars[outer_item]['sid_list']['oracle_list'][item].ORACLE_HOME ].home_ty\
pe == 'crs' else 'oracledatabase' }}"
name: "{{ outer_item }}_{{ item }}"
hostname: "{{ outer_item }}"
ansible_connection: ssh
ansible_ssh_user: "{{ ansible_ssh_user }}"
ansible_port: "{{ ansible_port }}"
#ansible_ssh_private_key_file: "{{ ansible_ssh_private_key_file }}"
ansible_host: "{{ hostvars[outer_item]['ansible_host'] }}"
ORACLE_BASE: "{{ hostvars[outer_item]['sid_list']['oracle_list'][item].ORACLE_BASE }}"
ORACLE_HOME: "{{ hostvars[outer_item]['sid_list']['oracle_list'][item].ORACLE_HOME }}"
ORACLE_SID: "{{ hostvars[outer_item]['sid_list']['oracle_list'][item].ORACLE_SID }}"
oracle_owner: "{{ hostvars[outer_item]['sid_list']['oracle_list'][item].owner }}"
delegate_to: 127.0.0.1
changed_when: False
loop: "{{ hostvars[outer_item]['sid_list']['oracle_list'].keys() | sort }}"
tags: [always]
The long condition adds a target into group either oraclecrs or oracledatabase.
Following playbooks are applied onto either these dynamically added targets:
# Segment 3. CRS/HAS Management.
- name: Oracle CRS/HAS Management
hosts:
- oraclecrs
roles:
- role: upgrade_crs
# Segment 4. DB home Management.
- name: Oracle DB Management
hosts:
- oracledatabase
roles:
- role: upgrade_database
This approach works for me when executed from command line, it even works from Execution environment podman image. When executed in AAP the playbook ends with:
TASK [Add crs/has for host dbhost] ******************************
task path: /runner/project/Oracle12cto19c/add_db_targets.yml:30
creating host via 'add_host': hostname=dbhost_+ASM
creating host via 'add_host': hostname=dbhost_IVANDB01
ok: [dbhost -> 127.0.0.1] => (item=+ASM) => {
"add_host": {
"groups": [
"oracle",
"oraclecrs"
],
"host_name": "dbhost_+ASM",
"host_vars": {
"ORACLE_BASE": "/opt/oracle/grid",
"ORACLE_HOME": "/opt/oracle/grid/product/19.21.0.0",
"ORACLE_SID": "+ASM",
"ansible_connection": "ssh",
"ansible_host": "dbhost",
}
},
"ansible_loop_var": "item",
"changed": false,
"item": "+ASM"
}
ok: [dbhost -> 127.0.0.1] => (item=IVANDB01) => {
"add_host": {
"groups": [
"oracle",
"oracledatabase"
],
"host_name": "dbhost_IVANDB01",
"host_vars": {
"ORACLE_BASE": "/opt/oracle/ormerck",
"ORACLE_HOME": "/opt/oracle/ormerck/product/19.21.0.0/db1",
"ORACLE_SID": "IVANDB01",
"ansible_connection": "ssh",
"ansible_host": "dbhost",
}
},
"ansible_loop_var": "item",
"changed": false,
"item": "IVANDB01"
}
META: ran handlers
META: ran handlers
PLAY [Oracle CRS/HAS Management] ***********************************************
skipping: no hosts matched
PLAY [Oracle DB Management] ****************************************************
skipping: no hosts matched
I looks like in-memory targets are added, but their groups are somehow ignored, and no hosts are matched.
UPDATE: full example is here: https://github.com/ibre5041/ansible_oracle_modules_example/blob/main/oracle_oratab.yml
Explanation:
On AAP I had been executing playbook with bigger inventory and also with
--limitparameter.And as documentation states:
In mine case I had been adding targets dynamically having names like:
dbhost_A,db_host_B, ...etc.The solution is to use wildcard for limit when you also use
add_host. For example:This wildcard limit will allow matching dynamically added targets.