Ansible loop/conditional

305 views Asked by At

I've been working on a play to create a virtual server and am doing it by asking the user some questions to gather the configuration requirements.

I'd like to run some pre-checks to show that certain virtual server properties exist, I've been able to work out all the properties with the exception of the profile.

When you run "list ltm profile" you need to specify the protocol, then the profile name, for example, "list ltm profile tcp tcp". Checking one LTM profile is fine, but the difficulty I'm having is when you need to check multiple profiles.

Is there a way I could loop my question and pass the users input into the check? Let's say the user wants to check the below profiles:

list ltm profile http http
list ltm profile tcp tcp

Here is the question asked:

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

And here is what I have for the pre-check portion of the play:

  - 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 

I was also looking at accounting for the fact that the user may not want a profile, so if they press enter, I'd need the "list ltm profile xxx xxx" check skipped. In another post I had some help doing this, but in re-working the syntax for this instance, I couldn't seem to get it working; any ideas what might be off with the below syntax?

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

There are 1 answers

0
Pete On

We got this working doing it this way.

Question asked to the user:

- 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

We wound up running this check for the profile in a separate task:

  - 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

Here is the play up to that check/task:

- 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