# How to supply defaults to tasks in 2.x?

**URL:** <https://forum.ansible.com/t/how-to-supply-defaults-to-tasks-in-2-x/20635>\
**Category:** Ansible Project\
**Created:** [February 19, 2016, 6:41pm UTC](https://forum.ansible.com/t/how-to-supply-defaults-to-tasks-in-2-x/20635 "2016-02-19T18:41:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yvo1](https://avatars.discourse-cdn.com/v4/letter/y/7ba0ec/32.png) [@Yvo1](https://forum.ansible.com/u/Yvo1)\
**Post date:** [February 19, 2016, 6:41pm UTC](https://forum.ansible.com/t/how-to-supply-defaults-to-tasks-in-2-x/20635/1 "2016-02-19T18:41:35Z")

</div>

Hi,

I’m upgrading a few Ansible scripts from 1.8 to 2.0 and I’m not sure how to solve this deprecation issue:  
"[DEPRECATION WARNING]: Using variables for task params is unsafe, especially if the variables come from an external source like facts. This feature will be

removed in a future release. Deprecation warnings can be disabled by setting deprecation\_warnings=False in ansible.cfg."

I’ve read the porting guide and a few posts here and on GitHub, but I can’t use “items” in my case.

In my group\_vars I define the defaults for the apt task:

> apt\_defaults:  
> cache\_valid\_time: 3600  
> install\_recommends: no  
> update\_cache: yes

In my script I refer to them:

> - name: Install server and helper packages  
> apt: pkg={{item}} state=installed  
> args: “{{apt\_defaults}}”  
> with\_items:
> - ntp
> - curl
> - git
> - patch
> - htop

How can I solve this in the new version?

Thanks!

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 20, 2016, 4:41pm UTC](https://forum.ansible.com/t/how-to-supply-defaults-to-tasks-in-2-x/20635/2 "2016-02-20T16:41:49Z")

</div>

in both versions you can still solve this by using YAML references

vars:  
apt\_defaults: &apt\_defaults  
…

- apt:  
\<\<: \*apt\_defaults  
pkg: “{{item}}”  
…

---

<div class="post-metadata">

**Author:** ![Yvo1](https://avatars.discourse-cdn.com/v4/letter/y/7ba0ec/32.png) [@Yvo1](https://forum.ansible.com/u/Yvo1)\
**Post date:** [February 20, 2016, 10:14pm UTC](https://forum.ansible.com/t/how-to-supply-defaults-to-tasks-in-2-x/20635/3 "2016-02-20T22:14:09Z")

</div>

Brian, thank you for that workaround.

Your solution however forces `pkg` as a separate argument on a new line.  
How would I supply `install_recommends` to that (specific) task? This for example doesn’t work:

…

- apt:  
\<\<: \*apt\_defaults  
pkg: “{{item}}”

install\_recommends: yes

It would be nice if it was possible to simply set defaults for tasks.  
Something like:

defaults:  
apt:  
cache\_valid\_time: 3600

And then allowing these defaults to be overridden by the actual task.  
Even better if you could specify these defaults in the group\_vars.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 20, 2016, 10:29pm UTC](https://forum.ansible.com/t/how-to-supply-defaults-to-tasks-in-2-x/20635/4 "2016-02-20T22:29:42Z")

</div>

not sure why the install\_recommends fails for you, it should override the original key

---

<div class="post-metadata">

**Author:** ![Yvo1](https://avatars.discourse-cdn.com/v4/letter/y/7ba0ec/32.png) [@Yvo1](https://forum.ansible.com/u/Yvo1)\
**Post date:** [February 20, 2016, 10:30pm UTC](https://forum.ansible.com/t/how-to-supply-defaults-to-tasks-in-2-x/20635/5 "2016-02-20T22:30:49Z")

</div>

Your example has a small syntax error, pkg should be nested under args.

This works:

`  
…

vars:  
apt\_defaults: &apt\_defaults  
cache\_valid\_time: 3600  
install\_recommends: no  
update\_cache: yes

…

- name: Install server and helper packages  
apt:  
args:  
\<\<: \*apt\_defaults  
pkg: ‘{{item}}’  
with\_items:
- ntp
- curl
- git
- patch
- htop

`

I’m going to play around with it, see if I can move the defaults back to group\_vars.

---

<div class="post-metadata">

**Author:** ![Yvo1](https://avatars.discourse-cdn.com/v4/letter/y/7ba0ec/32.png) [@Yvo1](https://forum.ansible.com/u/Yvo1)\
**Post date:** [February 20, 2016, 10:44pm UTC](https://forum.ansible.com/t/how-to-supply-defaults-to-tasks-in-2-x/20635/6 "2016-02-20T22:44:59Z")

</div>

Ah this also works and is a bit shorter:  
`  
…

vars:  
apt\_defaults: &apt\_defaults  
cache\_valid\_time: 3600  
install\_recommends: no  
update\_cache: yes

…

- name: Install server and helper packages  
apt: pkg={{item}}  
args: \*apt\_defaults  
with\_items:
- ntp
- curl
- git
- patch
- htop  
`
