英文:
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.
- 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
答案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('') }}"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论