From the course: Ansible: From Basics to Guru by Pearson

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Writing custom roles

Writing custom roles

OK, now that you know about the default organization of a role, let's talk about custom roles. Let me demonstrate. So the question is, how do you organize your own role? Well, let's go check it out. I like the example that is commonly found in the documentation as well to create a role for MOTD. So Ansible Galaxy role init MOTD. Doing that from the roles path, which is important when we later start using the playbook. So tree on MOTD showing all that is going on. And what are we going to do? Well, I'm going to keep it very simple. I want a default variable, I want a main task and I want a template as well. So in the main task, we are going to copy the template. Let's start with the template. So vim on MOTD templates, MOTD.j2. And there we are using welcome to Ansible hostname. So that is using the Ansible fact to write the name of the host. This host is managed by AnsibleManager. Good, so we have an Ansible fact. We also have AnsibleManager, which is not a fact. So we need to put the…

Contents