Ansible var_prompt

99 views Asked by At

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?

1

There are 1 answers

2
techraf On BEST ANSWER

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