【问题标题】:Ansible loop/conditionalAnsible 循环/条件
【发布时间】:2019-04-12 21:42:11
【问题描述】:

我一直在制作一个创建虚拟服务器的游戏,并通过向用户询问一些问题来收集配置要求来实现它。

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

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

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

list ltm profile http http
list ltm profile tcp tcp

这是问的问题:

- name: "vs_profile_type"
  prompt: "enter the profile(s) to run your pre-checks against"
  private: no 

以下是我为该剧的预检部分准备的内容:

  - 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”检查。在另一篇文章中,我得到了一些帮助,但是在重新设计这个实例的语法时,我似乎无法让它工作;以下语法可能有什么想法?

"tmsh list ltm profile {{ '{' + vs_profile_type + '}' if vs_profile_type else '' }} {{ '{' + vs_profile + '}' if vs_profile else '' }}"

【问题讨论】:

  • 我不完全确定您说“规则是什么?”时的意思。但是,我需要获取用户输入,在本例中为“tcp tcp http http”,通过获取 tcp tcp 输入将其拆分并将其附加到“tmsh list ltm profile {{ vs_profile_type }}”检查,然后然后获取剩余的输入,即“http http”输入并将其附加到“tmsh list ltm profile {{ vs_profile_type }}”检查。希望这是有道理的。

标签: ansible f5 tmsh


【解决方案1】:

我们以这种方式完成了这项工作。

向用户提出的问题:

- 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

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

  - 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: "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

【讨论】:

    猜你喜欢
    • 2017-11-12
    • 1970-01-01
    • 2022-11-15
    • 2022-12-07
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 2021-09-14
    相关资源
    最近更新 更多