6

Is that possible to set conditions for a single option of the ansible module.

   Using set_fact we can set condition aand use that variable in the ansible module. Alternatively is there any other option to set the condition using if loop in jinja or something like that.

E.g:

 - name: Simple PUT operation
   s3:
     bucket: mybucket
     object: /my/desired/key.txt
     src: /usr/local/myfile.txt
     mode: put

I want to add s3_url option if it satifies the condition sample_var= myapp.If it doesnot satify it wont have to add s3_url

   - name: Simple PUT operation
     s3:
       bucket: mybucket
       object: /my/desired/key.txt
       src: /usr/local/myfile.txt
       mode: put 
       s3_url: "s3my url"------if it satisfies sample_var=myapp 

How can i use jinja here when it satisfies my condition it needs to add this option to the ansible module?

Thanks in advance..

jake
  • 333
  • 1
  • 4
  • 12

1 Answers1

11

You may want to read about omitting parameters:

- name: Simple PUT operation
  s3:
    bucket: mybucket
    object: /my/desired/key.txt
    src: /usr/local/myfile.txt
    mode: put 
    s3_url: "{{ 's3my_url' if sample_var == 'myapp' else omit }}"
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
  • suppose we are setting conditions inside like this s3_url: "{{ 's3my_url' if {{ sample_var }} == {{ myapp }} else omit }}", it doesnot execute. When setting the conditions.how can we handle such conditions??? – jake Dec 27 '16 at 12:06
  • The syntax is exactly as I posted, don't use nested braces. If `myapp` is a variable and not a literal string, use `myapp` without single quotes. – Konstantin Suvorov Dec 27 '16 at 12:14