Hello Ansible Project - Assistance requested.
I’m attempting to create a new playbook that heavily uses loop iterations over lists and dictionaries.This is my first time using loops of any significance within Ansible, and I fear I’ve bit off more than I can chew.
The Goal: Create a playbook to deploy a list of application fixpacks, but first backup all of the required files of said fixpacks.
Currently I have something like the below approach:
- User passes JSON list on command line as extra-var {{ FPs }}
- Items in JSON list are used to include_vars
- role vars contain a list of dictionaries
- Loop over the list of include_vars, which has a list of dictionaries to backup required files.
Sample code thus far…
1) Command Line
ansible-playbook pb.yml --extra-vars ‘{ “FPs”: [‘FP1’,‘FP2’] }’
2) Include Vars
`
- name: Include vars for FP list
include_vars: “{{ item }}.yml”
register: include_vars_output
with_items: “{{ FPs }}”
`
3) role vars:
`
FP1.yml
List of Dictionaries
backup_files:
-
src_path: “{{ HOME }}/distribution/”
filename: “scope.war”
dest_path: “{{ Backup_dir }}/scope/” -
src_path: “{{ HOME }}/cbs/bin/”
filename: “PkPreProcessDOC”
dest_path: “{{ Backup_dir }}/cbs/bin/”
`
`
FP2.yml
List of Dictionaries
backup_files:
- src_path: “{{ HOME }}/distribution/”
filename: “build.xml”
dest_path: “{{ Backup_dir }}/scope/”
`
4) Backup required files before deployment
Disclaimer: Code breaks here since I can’t figure out how to parse out the variables I need
`
- name: Backup files
copy:
src: “{{ item.src_path }}{{ item.filename }}”
dest: “{{ item.dest_path }}{{ item.filename }}.pre{{ item.item }}”
remote_src: yes
with_items: “{{ include_vars_output.results.ansible_facts.backup_files }}”
`
Sample include_vars_output.results debug output:
{
“item”: {
“ansible_facts”: {
“backup_files”: [
{
“dest_path”: “{# Backup_dir #}/scope/”,
“filename”: “scope.war”,
“src_path”: “{# HOME #}/distribution/”
}
]
},
“invocation”: {
“module_args”: {
“_raw_params”: “FP1.yml”
},
“module_name”: “include_vars”
},
“item”: “FP1”
}
}
Error Text:
[DEPRECATION WARNING]: Skipping task due to undefined Error, in the future this will be a
fatal error.: ‘list object’ has no attribute ‘ansible_facts’.
This feature will be removed
in a future release. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.
Example above is two fixpacks with a total of 3 files to backup. I end up with a rather deep list of lists. of lists… of dictionaries.
I’ve checked syntax, loops, and examples but can’t seem to dig myself out.
It may well be that I am approaching this all wrong or attempting the impossible.
Thanks