首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可能的循环/条件

可能的循环/条件
EN

Stack Overflow用户
提问于 2018-08-31 20:22:52
回答 1查看 179关注 0票数 0

我一直在做一个创建虚拟服务器的剧本,并通过向用户询问一些问题来收集配置要求来完成这项工作。

我想运行一些预检查,以显示某些虚拟服务器属性存在,我已经能够计算出除配置文件之外的所有属性。

当您运行"list ltm profile“时,您需要指定协议,然后指定配置文件名称,例如,"list ltm profile tcp”。检查一个LTM配置文件是很好的,但我遇到的困难是当您需要检查多个配置文件时。

有没有一种方法可以循环我的问题,并将用户的输入传递给检查?假设用户想要检查以下配置文件:

代码语言:javascript
复制
list ltm profile http http
list ltm profile tcp tcp

下面是提出的问题:

代码语言:javascript
复制
- name: "vs_profile_type"
  prompt: "enter the profile(s) to run your pre-checks against"
  private: no 

这是我为这部剧的预检部分准备的:

代码语言:javascript
复制
  - name: Pre-check
    bigip_command:
      server: "{{ inventory_hostname }}"
      user: "{{ remote_username }}"
      password: "{{ remote_passwd }}"
      commands:
        - "tmsh list sys global-settings hostname"
        - "tmsh show sys failover"
        - "tmsh show cm sync-status"
        - "tmsh list ltm virtual {{ vs_name }}"
        - "tmsh list ltm profile {{ vs_profile_type }}"
        - "tmsh list ltm pool {{ vs_pool }}"
        - "tmsh list ltm rule {{ vs_rule }}"
      warn: no
      validate_certs: no
    delegate_to: localhost
    when: "'active' in Active_LTM['stdout'][0]"
    register: Active_LTM_Pre_Checks 

我也在考虑用户可能不想要配置文件的事实,所以如果他们按enter,我需要跳过"list ltm profile xxx xxx“检查。在另一篇文章中,我得到了一些帮助,但在为这个实例重新编写语法时,我似乎无法让它工作;你知道下面的语法可能有什么问题吗?

代码语言:javascript
复制
"tmsh list ltm profile {{ '{' + vs_profile_type + '}' if vs_profile_type else '' }} {{ '{' + vs_profile + '}' if vs_profile else '' }}"
EN

回答 1

Stack Overflow用户

发布于 2018-09-01 00:01:38

我们通过这种方式来解决这个问题。

向用户提出的问题:

代码语言:javascript
复制
- name: "vs_profile_type"
  prompt: "enter the profile to run your pre-checks against [Enter in the following format: tcp tcp, http http]"
  private: no

我们最终在一个单独的任务中对配置文件运行了这个检查:

代码语言:javascript
复制
  - name: Profile_Pre-check
    bigip_command:
      server: "{{ inventory_hostname }}"
      user: "{{ remote_username }}"
      password: "{{ remote_passwd }}"
      commands:
         - "tmsh list ltm profile {{ item }}"
      validate_certs: no
    delegate_to: localhost
    with_items:
      - "{{ vs_profile_type.split(',') }}"
    when: "'active' in Active_LTM['stdout'][0]"
    register: Active_LTM_Pre_Checks

下面是完成该检查/任务的过程:

代码语言:javascript
复制
- name: "vs_profile_type"
  prompt: "enter the profile to run your pre-checks against [Enter in the following format: tcp tcp, http http]"
  private: no

        bigip_command:
          server: "{{ inventory_hostname }}"
          user: "{{ remote_username }}"
          password: "{{ remote_passwd }}"
          commands:
  tasks:
      - name : Checking which LTM is active....
        bigip_command:
          server: "{{ inventory_hostname }}"
          user: "{{ remote_username }}"
          password: "{{ remote_passwd }}"
          commands:
            - "tmsh show sys failover"
          validate_certs: no
        delegate_to: localhost
        register: Active_LTM

      - name: The active LTMs management IP is....
        block:
          - debug:
              var: Active_LTM.stdout[0]

      - name: Profile_Pre-check
        bigip_command:
          server: "{{ inventory_hostname }}"
          user: "{{ remote_username }}"
          password: "{{ remote_passwd }}"
          commands:
             - "tmsh list ltm profile {{ item }}"
          validate_certs: no
        delegate_to: localhost
        with_items:
          - "{{ vs_profile_type.split(',') }}"
        when: "'active' in Active_LTM['stdout'][0]"
        register: Active_LTM_Pre_Checks

      - name: Please verify the profile pre-checks are correct
        debug:
          var: Active_LTM_Pre_Checks.stdout_lines
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52115395

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档