Re: [ansible-project] which is a correct way to reuse code from roles?

69 views
Skip to first unread message

Michael Wozniak

unread,
Oct 2, 2014, 2:55:41 PM10/2/14
to ansible...@googlegroups.com
Does this work:

- include: ../../includes_common/pkg_from_repo.yaml
  with_items:
   - { name: 'bash' }
   - { name: 'bash-completion' }
   - { name: 'screen' }

etc...



On Thu, Oct 2, 2014 at 5:20 AM, Dmitry Sukhodoyev <rave...@gmail.com> wrote:
Hello. Now I do sime things from roles/host_generic/tasks/main.yaml:

- include: ../../includes_common/pkg_from_repo.yaml
  vars
:
   name
: 'bash'
- include: ../../includes_common/pkg_from_repo.yaml
  vars
:
   name
: 'bash-completion'
- include: ../../includes_common/pkg_from_repo.yaml
  vars
:
   name
: "{{generic_vim_pkg_name}}"
- include: ../../includes_common/pkg_from_repo.yaml
  vars
:
   name
: 'screen'
- include: ../../includes_common/pkg_from_repo.yaml
  vars
:
   name
: 'logrotate'
- include: ../../includes_common/pkg_from_repo.yaml
  vars
:
   name
: 'sudo'
- include: ../../includes_common/pkg_from_repo.yaml
  vars
:
   name
: 'wget'
- include: ../../includes_common/pkg_from_repo.yaml
  vars
:
   name
: 'rsync'
- include: ../../includes_common/pkg_from_repo.yaml
  vars
:
   name
: 'munin-node'
I guess it's a long way. Is it possible to reuse some code from roles shorter? Using role from other role will be perfect solution, but I unable to found way how.

roles/includes_common/pkg_from_repo.yaml is installer for all possible operation systems is I replace includes in previous file to modules command, this file will bigger at 3-4 times.
---
- name: install RedHat package
  yum: name="{{name}}" state=present
  when: ansible_os_family == 'RedHat'
- name: install RedHat package
  apt: name="{{name}}" state=present
  when: ansible_os_family == 'Debian'
- name: install RedHat package
  openbsd_pkg: name="{{name}}" state=present
  when: ansible_system == 'OpenBSD'
- name: install FreeBSD package
  pkgng: name="{{name}}" state=present
  when: ansible_system == 'FreeBSD'

Also, it is incorrect decision to move pkg_from_repo.yaml to another directory like roles/host_generic/tasks, because from other roles it will be called as "include: ../../host_genetic/tasks/pkg_from_files.yaml" - much more text in call.

--
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/ab61eb2a-8a0e-4652-8d9a-0bf61cb73c9f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael DeHaan

unread,
Oct 2, 2014, 4:57:35 PM10/2/14
to ansible...@googlegroups.com
One cannot add a with_items to an include.

I think I've discussed not doing the "package abstraction thing" in another thread, and why it would be better to just list the packages per os, like

- yum: name={{ item }} state=installed
  with_items: yum_packages
  when: ansible_os_family == 'RedHat'

etc



Igor Homyakov

unread,
Oct 3, 2014, 2:44:09 AM10/3/14
to ansible...@googlegroups.com
Hi Dmitry,

I would suggest you to use my approach bellow:

# Load a variable file based on the OS type, or a default if not found.
- include_vars: "{{ item }}"
with_first_found:
- "{{ ansible_distribution }}.yml"
- "{{ ansible_os_family }}.yml"
- "default.yml"
- action: '{{ pkg_mgr }} name={{ item }} state=latest'
with_items: packages

# Ubuntu.yml
pkg_mgr: apt
packages:
- bash
- screen
- curl

Here is a gist: https://gist.github.com/hostmaster/db37c8e6c2a43997a224

-- Best, Igor
Reply all
Reply to author
Forward
0 new messages