0

I'm trying to create a playbook that will configure an LTM virtual server by asking the user some questions. Here's a sample of what the configuration might look like:

tmsh create ltm virtual junk_virtual { destination 192.168.51.60:80 ip-protocol tcp pool junk_pool profiles add { tcp { } http { } junk_profile { } } }

Most of this is simple to accomplish, but I'm wondering how to get around the optional items in the configuration. Let's say I ask the user if they want to configure a profile:

   - name: "virtual_server_profile"
      prompt: "Enter a profile"
      private: no

And in the case they do want to configure a profile I'd pass the "virtual_server_profile" variable into the virtual configuration command:

 - name: Implementation
    bigip_command:
      server: "{{ inventory_hostname }}"
      user: "{{ remote_username }}"
      password: "{{ remote_passwd }}"
      commands:
        - "tmsh create ltm virtual junk_virtual { destination 192.168.51.60:80 ip-protocol tcp pool junk_pool profiles add { {{ virtual_server_profile }} }"
      validate_certs: no
    delegate_to: localhost

In the event the user presses enter at the prompt creating a null value, is there a way to remove/ignore the "profiles add { {{ virtual_server_profile }} }" portion of the configuration?

Eugène Adell
  • 3,089
  • 2
  • 18
  • 34
Pete
  • 35
  • 2
  • 9

1 Answers1

1

You have mismatched braces in your example, I assume you wanted to close with } after the virtual_server_profile. Otherwise just fix it yourself.

Here's a syntax you need:

commands:
  - tmsh create ltm virtual junk_virtual { destination 192.168.51.60:80 ip-protocol tcp pool junk_pool {{ 'profiles add {' + virtual_server_profile + '} ' if virtual_server_profile else '' }}}
  • concatenate profiles add { and } strings to the variable virtual_server_profile
  • use conditional to check for virtual_server_profile truthiness, and print either the above value, or an empty string
techraf
  • 64,883
  • 27
  • 193
  • 198
  • Thanks for your prompt reply @techraf, I appreciate your assistance as I'm new to ansible and coding in general :) and good catch, I just threw that syntax together and missed that brace. Just to confirm, I take it I wouldn't need to add anything else to the vars_prompt portion of the play? – Pete Aug 29 '18 at 22:56
  • Worked perfectly, @techraf. Thanks again. Understood the logic once I got configuring it. – Pete Aug 30 '18 at 12:24