Hello - 

Can Ansible call a role from within another role, as if it's calling a 
function? Could that other role be passed certain variables at runtime?

I have a few tasks that are repeated several times in my plays. Right now, 
I just copy/paste the same task and tweak an object or two as needed. 
However, if I want to change something about that repeated task I then have 
to find all the places I put it in order for it to change. Also, the 
repeated tasks pollute the readability of the task yaml. It lacks 
modularity. 

I dropped some sample code at the end. The yellow items pull the md5 of two 
files for comparison, so I can take certain actions if they don't match. The 
blue items are logged results, wherein I may want to tweak the content 
later to match changing demands. Could I split out these functions into 
their own roles (with passed variables) and call them within a certain 
play? I could then have one task with pseudo code "Call role md5 
compare(filepath, filename)" and "Call role md5 logging(copy_output)". 

I'm new to Ansible and still getting my feet wet. Open to any suggestions. 
Thank you! :)

Sample Code:
---
- name: Stage MHE wm.properties file from template
  template:
    src=wm.properties.j2
    dest="{{ staged_config_dir }}/{{ inventory_hostname_short 
}}/wm.properties"
    backup=yes

- name: Calculate md5 of staged config
  stat: path="{{ staged_config_dir }}/{{ inventory_hostname_short 
}}/wm.properties"
  register: staged_stat

- name: Calculate md5 of target file
  stat: path="{{ WM_Home 
}}/distribution/DeploymentDirector/installer/properties/wm.properties"
  register: target_stat

- name: deploy staged wm.properties file when diff than target
  copy:
    src: "{{ staged_config_dir }}/{{ inventory_hostname_short 
}}/wm.properties"
    dest: "{{ WM_Home 
}}/distribution/DeploymentDirector/installer/properties/wm.properties"
    backup: yes
    remote_src: yes
  register: wm_properties_copy
  when: staged_stat.stat.md5!=target_stat.stat.md5

- name: log deployed changes
  copy:
    content: "UPDATED wm.properties!\nSource file: {{ 
wm_properties_copy.src }}\nNew file: {{ wm_properties_copy.dest }}\nBackup 
file: {{ wm_properties_copy.backup_file }}"
    dest: "{{ staged_config_dir }}/{{ inventory_hostname_short }}/diff.txt"
  when: staged_stat.stat.md5!=target_stat.stat.md5

- name: Stage MHE ILSStartup.def.xml file from template
  template:
    src: ILSStartup.def.xml.j2
    dest: "{{ staged_config_dir }}/{{ inventory_hostname_short 
}}/ILSStartup.def.xml"
    backup: yes

- name: Calculate md5 of staged ILSStartup.def.xml
  stat: path="{{ staged_config_dir }}/{{ inventory_hostname_short 
}}/ILSStartup.def.xml"
  register: staged_stat

- name: Calculate md5 of target ILSStartup.def.xml
  stat: path="{{ WM_Home 
}}/distribution/DeploymentDirector/installer/templates/ILSStartup.def.xml"
  register: target_stat

- name: deploy staged ILSStartup.def.xml file when diff than target
  copy:
    src: "{{ staged_config_dir }}/{{ inventory_hostname_short 
}}/ILSStartup.def.xml"
    dest: "{{ WM_Home 
}}/distribution/DeploymentDirector/installer/templates/ILSStartup.def.xml"
    backup: yes
    remote_src: yes
  register: ILSStartup_def_xml_copy
  when: staged_stat.stat.md5!=target_stat.stat.md5

- name: log ILSStartup.def.xml diff results
  copy:
    content: "UPDATED ILSStartup.def.xml!\nSource file: {{ 
ILSStartup_def_xml_copy.src }}\nNew file: {{ ILSStartup_def_xml_copy.dest 
}}\nBackup file: {{ ILSStartup_def_xml_copy.backup_file }}"
    dest: "{{ staged_config_dir }}/{{ inventory_hostname_short 
}}/ILS_diff.txt"
  when: staged_stat.stat.md5!=target_stat.stat.md5



Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/7ffdeb60-aea4-4219-b295-0b633a082a9e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to