I'm not aware of any generic way of providing those sort of values. Some
modules  provide their own way of using environmental variables or external
config files to facilitate what you're asking for.

kind regards
Pshem


On Tue, 3 Oct 2017 at 13:02 Behrang Saeedzadeh <behran...@gmail.com> wrote:

> Hi Pshem,
>
> Looks like my question was not phrased clearly enough. It is not about
> defining variables, I already use group, host, role, and play variables in
> my various Ansible projects.
>
> Let's pretend we have defined these variables:
>
> my_validate_certs: False
>
> my_method: POST
> my_user: admin
> my_password: p@ss
> my_force_basic_auth: yes
> my_body_format: json
>
>
> Almost all my URI tasks will use this configuration. So even though that I
> have defined these settings in variables, I still have to reference these
> variables every time I use the uri task:
>
> - name: Task 1
>   uri:
>
>     validate_certs: "{{my_validate_certs}}"
>     method: "{{my_method}}"
>     user: "{{my_user}}"
>     password: "{{my_pass}}"
>     force_basic_auth: "{{my_force_basic_auth}}"
>     body_format: "{{my_body_format}}"
>
>
> This is what I am trying to avoid. I want to tell Ansible: "Hey, for all
> the uri tasks, use these values as defaults".
>
>
> On Monday, October 2, 2017 at 6:18:42 PM UTC+11, Pshem Kowalczyk wrote:
>
>> Hi,
>>
>> If you want to use 'global' variables like that you have a few choices:
>>
>> 1. Use 'group_vars/all.yaml'
>> (all hosts belong to 'all' group by default)
>> 2. Use 'defaults/main.yaml' under your role
>> more info here:
>> http://docs.ansible.com/ansible/latest/playbooks_best_practices.html#directory-layout
>> 3. Use the 'include_vars' module
>> more info here:
>> http://docs.ansible.com/ansible/latest/include_vars_module.html
>>
>> More information about variables is here
>> http://docs.ansible.com/ansible/latest/playbooks_variables.html
>>
>> I think that the easiest way of building your 'own' modules (as a way of
>> abstracting things out) is to use roles and pass parameters to those roles
>> - more info here:
>> http://docs.ansible.com/ansible/latest/playbooks_reuse_roles.html#using-roles
>>
>> kind regards
>> Pshem
>>
>>
>> On Mon, 2 Oct 2017 at 15:49 Behrang Saeedzadeh <behr...@gmail.com> wrote:
>>
> Hi,
>>>
>>> I am writing a role that contains a dozen of tasks that invoke REST
>>> APIs. All tasks have a set of common and identical configurations:
>>>
>>> - name: Task 1
>>>   uri:
>>>     validate_certs: False
>>>     method: POST
>>>     user: "{{ user }}"
>>>     password: "{{ pass }}"
>>>     force_basic_auth: yes
>>>     body_format: json
>>>
>>>
>>> I am looking for a way to avoid repeating these parameters for all the
>>> tasks. One way is to use anchors:
>>>
>>> api_defaults: &API_DEFAULTS
>>>   validate_certs: False
>>>   method: POST
>>>   user: "{{ user }}"
>>>   password: "{{ pass }}"
>>>   force_basic_auth: yes
>>>   body_format: json
>>>
>>>
>>> Then I can reuse this similar to:
>>>
>>> - name: Task 1
>>>   uri:
>>>     << *API_DEFAULTS
>>>
>>>
>>> However, I still have to repeat the "<< *API_DEFAULTS" everywhere. Are
>>> there any other options available, other than developing new modules?
>>>
>>> For example to "define" modules based on existing modules, in a way
>>> similar to:
>>>
>>> - name: Define api
>>>   extend_module:
>>>     parent_module: uri
>>>     module_name: api
>>>     defaults:
>>>       validate_certs: False
>>>       method: POST
>>>       user: "{{ user }}"
>>>       password: "{{ pass }}"
>>>       force_basic_auth: yes
>>>       body_format: json
>>>
>>>
>>> And then use it like:
>>>
>>> - name: Task 1
>>>   api:
>>>     body:
>>>       a: 1
>>>       b: c
>>>
>>>
>>> Thanks in advance.
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Ansible Project" group.
>>>
>> To unsubscribe from this group and stop receiving emails from it, send an
>>> email to ansible-proje...@googlegroups.com.
>>> To post to this group, send email to ansible...@googlegroups.com.
>>
>>
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/ansible-project/4b750222-7c27-40b8-830f-010e8858fc1d%40googlegroups.com
>>> <https://groups.google.com/d/msgid/ansible-project/4b750222-7c27-40b8-830f-010e8858fc1d%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-project+unsubscr...@googlegroups.com.
> To post to this group, send email to ansible-project@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/d4523549-bb57-4386-8cc0-3043dc407f6e%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/d4523549-bb57-4386-8cc0-3043dc407f6e%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAEaZiRXk%2BZo8sG6%3Da0U%2Bz_a2%2BFxVfijrPPcgFZeBGm1g%2B4K3dg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to