Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set condition for single option of ansible module alone

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..

like image 754
jake Avatar asked Dec 24 '16 06:12

jake


People also ask

How do you apply condition in Ansible?

To implement conditions in Ansible, we use the when keyword. The keyword takes Boolean expressions based on a value or a variable from previous tasks or facts gathered from the remote hosts.

What is one option that Ansible gives you to check your playbook for syntax errors?

Use this command to run a playbook: $ ansible-playbook <playbook. yml> Use this command to check the playbook for syntax errors: $ ansible-playbook <playbook. yml> --syntax-check.

What is diff mode in Ansible?

In diff mode, Ansible provides before-and-after comparisons. Modules that support diff mode display detailed information. You can combine check mode and diff mode for detailed validation of your playbook or role.


1 Answers

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 }}"
like image 125
Konstantin Suvorov Avatar answered Sep 25 '22 07:09

Konstantin Suvorov