The aim of the playbook below is to extract facts from a number of devices and use it to run specific shell commands. I believe I have most of it figured out with two exceptions.
- How can I run one set of commands versus the other based on a
when
statements? For now, I have the second set of commands listed under a different task with thewhen
conditional and both of them work, but the second task end up overwriting the register which is being used with Jinja2 templating to output a file. - How to use the same register/var to hold the output both commands without overwriting the
register
?
I suspect using the same task for both commands is the key but I do not find how to achieve this.
Here is the playbook. In short, I should be able to combine Task1.1 and Task2.1 under one task but I am struggling how to do that.
---
- name: Gather facts (f5)
bigip_device_info:
provider: "{{ provider }}"
gather_subset:
- system-info
- devices
register: dev_fact
####################### LTM Hosts ###############################
- name: Task1.1 - Find vcmp count info
shell: tmsh show vcmp health module-provision | grep 'ltm nominal' | grep ltm | wc -l
when: not ( 'Z100'in dev_fact['system_info']['platform'] or 'Z101' in dev_fact['system_info']['platform'] )
ignore_errors: yes
register: nommodltm
failed_when: "'unexpected' in nommodltm.stdout"
- name: "Set Fact for Nominal Module Count for LTM"
set_fact: nommodltm={{ nommodltm.stdout_lines[0] }}
ignore_errors: yes
- name: Print lic info - vCMP Devices
debug:
msg: Lic LTM Nominal Info={{ nommodltm }}
####################### LTM VE | Guests ###############################
- name: Task2.1 - Find vcmp count info
shell: tmsh list sys provision | grep -b1 nominal | grep "sys provision ltm" | wc -l
when: ( 'Z100'in dev_fact['system_info']['platform'] or 'Z101' in dev_fact['system_info']['platform'] )
ignore_errors: yes
register: nommodltm
failed_when: "'unexpected' in nommodltm.stdout"
- name: "Set Fact for Nominal Module Count for LTM"
set_fact: nommodltm={{ nommodltm.stdout_lines[0] }}
ignore_errors: yes
- name: Print device info - vCMP Devices
debug:
msg: Lic LTM Nominal Info={{ nommodltm }}
- name: Copy output to file
template:
src: invchktest.txt.j2
dest: "inventory_check/{{ date }}-{{ dc }}-inventory_check.csv"
delegate_to: localhost
ignore_errors: true