Ansible循环/有条件

时间:2018-08-31 12:22:52

标签: ansible f5 tmsh

我一直在努力创建虚拟服务器,并通过询问用户一些问题来收集其配置要求。

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

运行“ 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键,我将需要跳过“列出ltm个人资料xxx xxx”检查。在另一篇文章中,我对此有所帮助,但是在重新编写该实例的语法时,我似乎无法使其正常运行。任何想法可能与以下语法有什么区别?

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

1 个答案:

答案 0 :(得分:0)

我们已经通过这种方式进行了这项工作。

问题问用户:

- 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