Role creation in Tower

Hello,

I am new to Ansible. If I were asked to build a role that provides some fields (database columns - server name and event name) to another playbook and get back data such as creation date and resolved date; i.e required to mock the other person’s code; what folders do I have to include?

I understand that Ansible code for role usually has 1 or more of 8 folders - tasks, handlers, templates, files, vars, defaults, meta, library, module_utils, lookup_plugins;

In that case, welcome to Ansible, and to the Forum.

Skipping the elided parts of your question (which I’ll get to shortly), you can leave out any part of a role that you aren’t using. The only ways to invoke a role that I know of expect at least one task file in the role’s tasks/ folder, so I’d guess the strict answer to your question is a tasks/ folder.

The more interesting part of your question is the “build a role that provide some [data] to another playbook and get [other data] back” bit. The “tech support” answer (technically accurate but totally useless) is, “You don’t.” That is, roles don’t invoke playbooks. However:

  1. Roles can invoke other roles through either:
  • declaration in a role’s dependencies: dictionary in that role’s meta/main.yml file
  • ansible.builtin.include_role and ansible.builtin.import_role modules, invoked through tasks.
  1. Playbooks can invoke roles:
  • statically in the playbook’s roles: section
  • statically by the ansible.builtin.import_role module or dynamically by the ansible.builtin.include_role module in the playbook’s tasks: section.
  1. And finally, playbooks can invoke other playbooks
  • by ansible.builtin.import_playbook module. Unlike the other modules above, import_playbook can’t appear in a tasks: list; it can only be used at the outermost playbook level.

But your topic title mentions “Tower”, a.k.a. “AWX”, in which case some other options are available to you. An AWX Project can have multiple job Templates, each of which defines a way to invoke one of the playbooks contained in the project. These AWX job templates are roughly equivalent to running ansible-playbook at the CLI.

But at a higher level, AWX also allows you to create Workflows – networks of job templates to be invoked serially or in parallel, and optionally dependent on the success of prior jobs in the workflow. (This sounds like what your question is about, what with having “Tower” in the topic title and mentioning passing data to other playbooks, but I expect this is far beyond what you actually need.)

So back to your question. It’s possible all you need are a couple of roles, and your first role invokes the second via ansible.builtin.include_role with some variables defined. The second role does its work and returns data either by register: on its task(s) or by ansible.builtin.set_fact:. All of this could live within the same Project. (I’m hopeful for your sake that’s the case, because you’ve got a lot to learn before you see progress otherwise.)

Alternatively, you may need to invoke playbooks from other projects (highly unlikely at this stage), in which case you would need AWX Workflows running playbooks from one or more Projects.

I didn’t mean to turn this into an Intro to Ansible -101 course. Maybe it gives you what you need to refine your questions a bit.

And we do look forward to your questions!
Again, welcome to Ansible!

1 Like