如何根据事实来设置命令的执行条件并将其输出保存到同一变量中?

huangapple go评论93阅读模式
英文:

How can I condition the command to run based on facts and save its output to the same variable?

问题

以下是您要翻译的代码部分:

- 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
英文:

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

答案1

得分: 3

以下是您要翻译的内容:

- name: 查找vcmp计数信息
  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: 打印设备信息 - vCMP设备
  debug: 
    msg: "Lic LTM名义信息: {{ nommodltm.stdout_lines[0] | default('') }}"
英文:

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('') }}"

huangapple
  • 本文由 发表于 2023年4月7日 04:26:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定