How to supply defaults to tasks in 2.x?

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!

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

vars:
apt_defaults: &apt_defaults

  • apt:
    <<: *apt_defaults
    pkg: “{{item}}”

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.

not sure why the install_recommends fails for you, it should override the original key

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.

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
    `