From within a role, call another role and pass variables at runtime?

428 views
Skip to first unread message

burn...@umn.edu

unread,
Nov 5, 2016, 7:41:12 PM11/5/16
to Ansible Project
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

Brian Coca

unread,
Feb 9, 2017, 10:37:27 PM2/9/17
to Ansible Project
roles before 2.2 only had dependencies, but in that version we
introduced 'include_role' that can be placed as a task.


----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages