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?
- We are using the latest Ansible build right out of git. Our Ansible master is a Debian Jessie EC2 instance.
- We are using Amazon AWS (EC2, RDS, Route53, CodeCommit and some other services).
- 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:
`