How to loop over playbook include? - ansible

(I'm currently running Ansible 2.1)
I have a playbook that gathers a list of elements and I have another playbook (that calls different hosts and whatnot) using said element as the basis for most operations. Therefore, whenever I use with_items over the playbook, it causes an error.
The loop control section of the docs say that "In 2.0 you are again able to use with_ loops and task includes (but not playbook includes) ". Is there a workaround? I really need to be able to call multiple hosts in an included playbook that runs over a set of entries. Any workarounds, ideas for such or anything are greatly appreciated!
P.S. I could technically command: ansible-playbook but I dont want to go down that rabbit hole if necessary

I think I faced same issues, and by the way, migrating to shows more than in 'item' already in use.
refering to http://docs.ansible.com/ansible/playbooks_best_practices.html , you should have an inventory (that contains all your hosts), and a master playbook (even if theorical).
A good way, instead of including playbooks, is to design roles, even if empty. Try to find a "common" role for everything that could be applied to most of your hosts.Then, include additional roles depending of usage, this will permit you to trigg on correct hosts.
You can also have roles that do nothing (meaning, nothing in 'tasks'), but that contain set of variables that can be common for two roles (you avoid then duplicate entries).

Related

Ordering of hosts in a group

I'm using ansible 1.9.0 and was curious how it determines the order of the hosts in a group.
firsthost = groups.controlplane[0]
I was under the impression doing this would result in the first host listed in that group in the inventory file. I found that is not the case. Sometimes it's the 3rd or 4th. Doesnt seem to be any pattern to it. It is based on the first host in that group that responds when gathering facts or something?
UPDATE actually I made a typo, I'm using 2.9.0.
What could be causing this? I have a group of 5 hosts and it seems its always picking the 3rd one as it exists in the inventory file when I use controlplane[0].
GOnna whip up a simple playbook to demonstrate.

How to exclude instances of the EC2 inventory in Ansible?

We have an Ansible server using EC2 dynamic inventory:
https://github.com/ansible/ansible/blob/devel/contrib/inventory/ec2.py
https://github.com/ansible/ansible/blob/devel/contrib/inventory/ec2.ini
However, with the number of instances we have, running ./ec2.py --list or ./ec2.py --refresh-cache returns a 28,000 line JSON response.
This I assume, causes it to randomly fail (returns a Python stack trace) as it only receives a partial response when sending a call to AWS, but is then fine if ran again.
Which is why I want to know if there's a way to cut this down.
I know there is a way to include specific instances by tag in the ec2.ini (i.e. # instance_filters = tag:env=staging), but with
the way our instances are tagged, is there a way to exclude
instances instead (something that would look similar to: # instance_filters = tag:name=!dev)?
is there a way to exclude instances instead
Just for completeness, I wanted to point out that the "inventory protocol" for ansible is super straightforward to implement, and they even have a JSON Schema for it.
You can see an example of the output it is expecting by running the newly included ansible-inventory script with --list to see the output it generates from one of the .ini style inventories, and then use that to emit your own:
$ printf 'somehost ansible_user=bob\n\n[some_group]\nsomehost\n' > sample
$ ansible-inventory -i ./sample --list
What I am suggesting is that you might have better luck making a custom inventory script, that does know your local business practices, rather than trying to force ec2.py into running a negation query (which, as best I can tell, it will not do).
To generate dynamic inventory, just make an executable -- as far as I know it can be in any language at all -- and then point the -i at the executable script instead of a "normal" file. Ansible will invoke that program, and operate on the JSON output as the inventory. There are several examples people have posted as gists, in all kinds of languages.
I would still love it if you would file an issue with ansible about ec2.py, because you have the situation that can make the bug report concrete for them in ways that a simple "it doesn't work for large inventories" doesn't capture. But in the mean time, writing your own inventory provider is actually less work than it sounds.
I use the option pattern_exclude in ec2.ini:
# If you want to exclude any hosts that match a certain regular expression
pattern_exclude = staging-*
and
hostname_variable = tag_Name

Ansible roles. Default values

I cannot find the answer in the docs, neither I can find an example: is it possible to name files in role/defaults/ other then main.yml? I mean, is main.yml the only valid name for a file with default values?
By the sound of this documentation;
http://docs.ansible.com/ansible/playbooks_roles.html#roles
Roles are just automation around ‘include’ directives as described
above, and really don’t contain much additional magic beyond some
improvements to search path handling for referenced files. However,
that can be a big thing!
This designates the following behaviors, for each role ‘x’:
If roles/x/tasks/main.yml exists, tasks listed therein will be added
to the play
If roles/x/handlers/main.yml exists, handlers listed
therein will be added to the play
If roles/x/vars/main.yml exists,
variables listed therein will be added to the play
If
roles/x/defaults/main.yml exists, variables listed therein will be
added to the play
If roles/x/meta/main.yml exists, any role
dependencies listed therein will be added to the list of roles (1.3
and later) Any copy, script, template or include tasks (in the role)
can reference files in roles/x/{files,templates,tasks}/ (dir depends
on task) without having to path them relatively or absolutely
Q: I mean, is main.yml the only valid name for a file with default values?
A: Sounds like it is only if you are wanting ansible to automatically load certain var_files for that role and auto include the task file.
Q: Is it possible to name files
A: Yes, it just means in the playbook you have to use the include directive and explicitly point it to the unique task file name.
Example:
- name: Do role
hosts: localhost
tasks:
- include: roles/operations/tasks/installProgram.yml
As you tell in the comments, it's possible to call a file anything else under the roles/my_role/defaults/. The issue is that files like roles/my_role/defaults/anything_else.yml will never be loaded as default. Even you will be able to load it with include_vars, it will gain the high precedence of included vars (18/22).
Nevertheless, since Ansible 2.6, your are able to use a directory main in place of a main.yml file!
By doing so, you can reach your goal by splitting your variables into roles/my_role/defaults/main/something.yml, roles/my_role/defaults/main/anything_else.yml, …
More details in this StackOverflow answer.
But indeed this feature is not yet documented (I just created an issue about that).
Ansible will look in the roles/ directory for tasks, variables, handlers, etc by default. roles/default is simply an example provided in their docs. You can structure your roles differently to suit your needs. For example, I have mine set up as such;
roles/windows/<somerole>/tasks/main.yml
roles/linux/<somerole>/tasks/main.yml
main.yml is the only file that will be loaded automatically (by "default") when running playbooks. If you want to specify another file name for whatever reason you need to use the include function and point to your file as described above.
The "Best Practices" guide offers some good insight into directory structuring.

What's the difference between defaults and vars in an Ansible role?

When creating a new Ansible role, the template creates both a vars and a defaults directory with an empty main.yml file. When defining my role, I can place variable definitions in either of these, and they will be available in my tasks.
What's the difference between putting the definitions into defaults and vars? What should go into defaults, and what should to into vars? Does it make sense to use both for the same data?
I know that there's a difference in precedence/priority between the two, but I would like to understand what should go where.
Let's say that my role would create a list of directories on the target system. I would like to provide a list of default directories to be created, but would like to allow the user to override them when using the role.
Here's what this would look like:
---
- directories:
- foo
- bar
- baz
I could place this either into the defaults/main.yml or in the vars/main.yml, from an execution perspective, it wouldn't make any difference - but where should it go?
The Ansible documentation on variable precedence summarizes this nicely:
If multiple variables of the same name are defined in different places, they win in a certain order, which is:
extra vars (-e in the command line) always win
then comes connection variables defined in inventory (ansible_ssh_user, etc)
then comes "most everything else" (command line switches, vars in play, included vars, role vars, etc)
then comes the rest of the variables defined in inventory
then comes facts discovered about a system
then "role defaults", which are the most "defaulty" and lose in priority to everything.
So suppose you have a "tomcat" role that you use to install Tomcat on a bunch of webhosts, but you need different versions of tomcat on a couple hosts, need it to run as different users in other cases, etc. The defaults/main.yml file might look something like this:
tomcat_version: 7.0.56
tomcat_user: tomcat
Since those are just default values it means they'll be used if those variables aren't defined anywhere else for the host in question. You could override these via extra-vars, via facts in your inventory file, etc. to specify different values for these variables.
Edit: Note that the above list is for Ansible 1.x. In Ansible 2.x the list has been expanded on. As always, the Ansible Documentation provides a detailed description of variable precedence for 2.x.
Role variables defined in var have a very high precedence - they can only be overwritten by passing them on the command line, in the specific task or in a block. Therefore, almost all your variables should be defined in defaults.
In the article "Variable Precedence - Where To Put Your Role Vars" the author gives one example of what to put in vars: System-specific constants that don't change much. So you can have vars/debian.yml and vars/centos.yml with the same variable names but different values and include them conditionally.
IMHO it is impractical and not sensible that Ansible places such high priority on configuration in vars of roles. Configuration in vars/main.yml and defaults/main.yml should be low and probably the same priority.
Are there any real life examples of cases where we want this type of behavior?
There are examples that we dont' want this.
The point to make here is that configuration in defaults/main.yml cannot be dynamic. Configuration in vars/main.yml can. So for example you can include configuration for specific OS and version dynamically as shown in geerlingguy.postgresql
But because precedence is so strange and impractical in Ansible geerlingguy needs to introduce pseudo variables as can be seen in variables.yml
- name: Define postgresql_packages.
set_fact:
postgresql_packages: "{{ __postgresql_packages | list }}"
when: postgresql_packages is not defined
This is a concrete real life example that demonstrates that the precedence is impractical.
Another point to make here is that we want roles to be configurable. Roles can be external, managed by someone else. As a general rule you don't want configuration in roles to have high priority.
Basically, anything that goes into “role defaults” (the defaults folder inside the role) is the most malleable and easily overridden. Anything in the vars directory of the role overrides previous versions of that variable in namespace. The idea here to follow is that the more explicit you get in scope, the more precedence it takes with command line -e extra vars always winning. Host and/or inventory variables can win over role defaults, but not explicit includes like the vars directory or an include_vars task.
doc
Variables and defaults walk hand in hand. here's an example
-name: install package
yum: name=xyz{{package_version}} state=present
in your defaults file you would have something like:
package_version: 123
What ansible will do is, it's gonna take the value of package_version and put it next to the package name so it will read somewhere as:
-name: install package
yum: name=xyz123 state=present
This way it will install xyz123 and not xyz123.4 or whatever is in the great repository of xyz's.
At the end it will do yum install -y xyz123
So basically the defaults are the values present, if you do not set a specific value for the variables, cause that space can't stay empty.

Ansible and inventory specific vars

Is it possible to declare variables the inventory level? For example I have a inventory for my development env, and another one for my production env.
I have templates that I need populated with certain variables that are specific to either environment.
At the moment it seems I can only set these variables at the group or host level, but I want it to apply for the entire inventory to avoid repeating myself for each group or host I have defined!
It turns out, my previous answer was not so right. There is an easier solution. Just define the vars for the all group. I would have expected ansible complaining there is no matching group definition, but it works, I just tested it:
[some_group]
some_host
[another_group]
another_host
[all:vars]
some_var=some_value
But in this case you really are forced to define the vars in the inventory file and can not define it in group_vars/all, but this should be obvious.
Leaving this here for reference, but check out my other answer, there is a simpler way to do it.
I believe this is not directly possible. You can define variables directly in the inventory, but they still have to be defined per host or group.
You could create a group which inherits from all other groups of that inventory file and then assign the variables to this group.
Inventory file myInventory:
[some_group]
some_host
[another_group]
another_host
[myInventory:children]
some_group
another_group
[myInventory:vars]
some_var=some_value
Separate inventories are a very good idea. The above answer's fully accurate, but I don't believe you'll be able to follow that approach if your groups are split up across inventory files, which I think your question implies they are.
Depending on your platform (the mode being AWS), it's effortless to group all your machines by environment, and it doesn't matter which inventory or inventories pick them up. I would just add an environment group variable file.
Please edit if group_vars are something you're trying to avoid categorically. If that's the case, then appropriate or create your own inventory scripts (adding a constant variable to the ec2 one is very very easy).

Resources