如何单独设置ansible模块的单个选项的条件

时间:2016-12-24 06:46:40

标签: conditional ansible jinja2 ansible-playbook

是否可以为ansible模块的单个选项设置条件。

   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

我想添加s3_url选项如果它满足条件sample_var = myapp.If它不满足它不需要添加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 

如果我满足条件需要将此选项添加到ansible模块中,我如何在这里使用jinja?

提前致谢..

1 个答案:

答案 0 :(得分:5)

您可能想了解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 }}"