3

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.

  1. 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 the when 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.
  2. 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
Zeitounator
  • 38,476
  • 7
  • 53
  • 66
Tash
  • 31
  • 5
  • A skipped task generates output in a registered var (i.e. "this task was skipped"). So yes, if you use the same register, it will be overridden. – Zeitounator Apr 07 '23 at 10:07

1 Answers1

3

You can template your shell command and use a Jinja condition to run one command or the other instead of having a when:

- name: Find vcmp count info
  shell: >-
    {%- if 'Z100' in dev_fact.system_info.platform 
          or 'Z101' in dev_fact.system_info.platform 
    -%}
      tmsh list sys provision \
        | grep -b1 nominal \
        | grep "sys provision ltm" \
        | wc -l
    {%- else -%}
      tmsh show vcmp health module-provision \
        | grep 'ltm  nominal' \
        | grep ltm \
        | wc -l
    {%- endif -%}
  register: nommodltm
  failed_when: "'unexpected' in nommodltm.stdout"

- name: Print device info - vCMP Devices
  debug: 
    msg: "Lic LTM Nominal Info: {{ nommodltm.stdout_lines[0] | default('') }}"
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • Hi β.εηοιτ.βε, testing showed that the solution is working as expected. I needed to make some tweaks for the jinja2 template to save output to a file in the correct format, but your solution is exactly what I needed. Thank you again for your help. – Tash Apr 07 '23 at 16:15
  • Hi β.εηοιτ.βε, any suggestions for Ansible online training or Books to further the skill sets. – Tash Apr 07 '23 at 16:15
  • What is the advantage of adding default('') to the last line of the code. I use the debug/msg option to print both, i.e. with and without default and didn't notice any difference in the output. – Tash Apr 07 '23 at 16:25
  • The advantage of the `default` is to have a default returned even if `nommodltm` comes out a empty, or null or if there is no item `0` in the `stdout_lines` list: https://jinja.palletsprojects.com/en/3.1.x/templates/#jinja-filters.default – β.εηοιτ.βε Apr 07 '23 at 20:09