How to supply defaults to tasks in 2.x?

342 views
Skip to first unread message

Yvo

unread,
Feb 20, 2016, 11:35:15 AM2/20/16
to Ansible Project
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!
 

Brian Coca

unread,
Feb 20, 2016, 11:42:02 AM2/20/16
to ansible...@googlegroups.com
in both versions you can still solve this by using YAML references

vars:
  apt_defaults: &apt_defaults
...

- apt:
  <<: *apt_defaults
  pkg: "{{item}}"
...


----------
Brian Coca

Yvo

unread,
Feb 20, 2016, 5:14:09 PM2/20/16
to Ansible Project
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.

Brian Coca

unread,
Feb 20, 2016, 5:29:48 PM2/20/16
to ansible...@googlegroups.com
not sure why the install_recommends fails for you, it should override the original key

----------
Brian Coca

Yvo

unread,
Feb 20, 2016, 5:30:49 PM2/20/16
to Ansible Project
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.

Yvo

unread,
Feb 20, 2016, 5:44:59 PM2/20/16
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages