# Newbie needs help re-using a tasklist

**URL:** <https://forum.ansible.com/t/newbie-needs-help-re-using-a-tasklist/23563>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [January 26, 2017, 7:28am UTC](https://forum.ansible.com/t/newbie-needs-help-re-using-a-tasklist/23563 "2017-01-26T07:28:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rene](https://avatars.discourse-cdn.com/v4/letter/r/7cd45c/32.png) [@rene](https://forum.ansible.com/u/rene)\
**Post date:** [January 26, 2017, 7:28am UTC](https://forum.ansible.com/t/newbie-needs-help-re-using-a-tasklist/23563/1 "2017-01-26T07:28:59Z")

</div>

Dear community,

first of all I want to apologize for my lack of knowledge but I’m relatively new to Ansible  
and have a weak background regarding scripting/programming. I’m more the operating type and  
not the dev guy. 😉

But still, my boss asked tasked me to get familiar with Ansible (because we want to use it  
for our cloud deployment in the near future).

So I would really love if you could assist me with a few issues I ran into. I’ll try to ask  
meaningful questions and I’ll also try to give you all the information you need to understand  
my problem(s).

So let’s start right away.

What is the environment?

1. We are using the latest Ansible build right out of git. Our Ansible master is a Debian Jessie EC2 instance.
2. We are using Amazon AWS (EC2, RDS, Route53, CodeCommit and some other services).
3. We want to spin up Debian machines (some Apache reverse proxies as well as some application servers with the software our company develops → we use an embedded Tomcat → Spring Boot)

What is the issue?

As of now, I have a playbook wich is split up into smaller .yml files. The reason for that is, that we want to “re-use” some of .yml files in other playbooks so we don’t have to rewrite everything from scratch as we progress.

`

- name: Create the APP instance  
hosts: localhost  
connection: local  
remote\_user: admin  
become: yes  
gather\_facts: no

vars\_files:

- app\_vars.yml

tasks:

- include: generic\_ec2.yml
- include: generic\_debian.yml
- include: generic\_git.yml
- include: generic\_services.yml
- include: generic\_ssh.yml
- include: app\_ssh.yml
- include: generic\_reboot.yml

`

This is the main playbook for the app server(s). As you can see, we include one variable files and several task lists. By the way, this works just fine but I wanted to improve certain things.

Therefore I have added one of task lists I struggle to improve.

`

- name: dist upgrade  
apt: upgrade=dist  
delegate\_to: “{{ groups.launched[0] }}”

- name: install apt packages  
apt: name={{ item }} state=latest  
with\_items: “{{ aptpackages }}”  
delegate\_to: “{{ groups.launched[0] }}”

- name: install apt packages backports  
apt: name={{ item }} default\_release=jessie-backports state=latest  
delegate\_to: “{{ groups.launched[0] }}”  
with\_items: “{{ aptpackagesbackports }}”

- name: autoremove apt packages  
shell: apt-get -y autoremove --purge  
delegate\_to: “{{ groups.launched[0] }}”

`

The issue I have with this is, that I want to re-use this part with a bunch of other playbooks. But not every other type of server needs packages removed or need packages from backports.

So I would like to implement some kind of check if there is something to remove/install or not. If not, Ansible will skip that particular task in the task list.

Our varible files usually look like this:

`

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [January 27, 2017, 7:50am UTC](https://forum.ansible.com/t/newbie-needs-help-re-using-a-tasklist/23563/2 "2017-01-27T07:50:37Z")

</div>

> As of now, I have a playbook wich is split up into smaller .yml  
> files. The reason for that is, that we want to "re-use" some of  
> .yml files in other playbooks so we don't have to rewrite  
> everything from scratch as we progress.

I would try to get familiar with using roles rather than including  
yml-files. Easier (for me) and less error prone.

[https://docs.ansible.com/playbooks\_roles.html](https://docs.ansible.com/playbooks_roles.html)

> delegate\_to: "{{ groups.launched[0] }}"

Rather than targetting localhost at the beginning and then delegating  
everything to hosts, I would try to work the other way.

Side note:  
I have only "hosts: {{ target }}" in my playbooks, that way I can  
decide on the command line, which hosts to target by using  
"ansible-playbook some.yml -e 'target=foobar'".

> The issue I have with this is, that I want to re-use this part with  
> a bunch of other playbooks. But not every other type of server  
> needs packages removed or need packages from backports.

Add a when condition to only execute the task, if e.g. your variable  
aptpackages is defined  
when: 'aptpackages is defined'  
(indentation equal to the name or apt lines)

Johannes

---

<div class="post-metadata">

**Author:** ![rene](https://avatars.discourse-cdn.com/v4/letter/r/7cd45c/32.png) [@rene](https://forum.ansible.com/u/rene)\
**Post date:** [January 30, 2017, 7:27am UTC](https://forum.ansible.com/t/newbie-needs-help-re-using-a-tasklist/23563/3 "2017-01-30T07:27:23Z")

</div>

Thanks, that helped a lot. 🙂

---

<div class="post-metadata">

**Author:** ![rene](https://avatars.discourse-cdn.com/v4/letter/r/7cd45c/32.png) [@rene](https://forum.ansible.com/u/rene)\
**Post date:** [January 30, 2017, 9:32am UTC](https://forum.ansible.com/t/newbie-needs-help-re-using-a-tasklist/23563/4 "2017-01-30T09:32:40Z")

</div>

As mentioned before, your answer helped a lot! Here is the solution I came up with.

`

- name: install apt packages  
apt: name={{ item }} state=latest  
delegate\_to: “{{ groups.launched[0] }}”  
with\_items: “{{ aptpackages }}”  
when: not((aptpackages is undefined) or (aptpackages is none) or (aptpackages | trim == ‘’))

`

I thought it would be more resilient to check if the variable is just undefined, none existing or empty. Because in all cases I want that action to be skipped. I tested all cases and it works for me. 🙂

I will also think about the other [suggestion](http://www.dict.cc/englisch-deutsch/suggestion.html)s you made.

René
