{"id":7493,"date":"2021-06-24T06:13:08","date_gmt":"2021-06-24T06:13:08","guid":{"rendered":"https:\/\/linuxways.net\/?p=7493"},"modified":"2021-06-24T06:13:08","modified_gmt":"2021-06-24T06:13:08","slug":"ansible-roles-and-how-to-use-them-in-playbooks","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/ansible-roles-and-how-to-use-them-in-playbooks\/","title":{"rendered":"Ansible Roles and How to Use them in Playbooks"},"content":{"rendered":"<p>In the course of writing <a href=\"https:\/\/linuxways.net\/de\/ubuntu\/how-to-create-and-run-ansible-playbook-file\/\">Ansible playbook<\/a> files, you may have noticed that you could reuse some of the code defined in your existing playbooks. For example, you could re-purpose the code for installing the MariaDB database server on one managed host with different hostnames, users, and passwords for another remote host. This saves a lot of time and energy which would have been used in writing new playbook files from scratch. And this is where the concept of Ansible roles comes in.<\/p>\n<p>An ansible role is a concept of repurposing tasks into individual files which are easier to manage and manipulate. Each role provides a set of tasks, variables, and handlers &#8211; to mention a few &#8211; that are required for its implementation. Roles allow users to reorganize long and complex playbook structures into simpler, shorter, and neater playbook files. As we mentioned earlier, roles are meant to be reusable, and invoking roles in the playbook file simplifies code &amp; eliminates duplication. Repetitive tasks such as installation, &amp; configuration of applications can be packaged in separate files and reused across various managed hosts.<\/p>\n<p>In this guide, you will learn how to create and use Ansible roles in playbooks. For demonstration, we will create a role that installs the Apache web server and configures the firewall to open port 80.<\/p>\n<h2><strong>How to create an Ansible role<\/strong><\/h2>\n<p>We will begin by creating a simple Ansible role. To create a role, simply use the command syntax below where <strong>myrole<\/strong> is the name of the role.<\/p>\n<pre><strong>$ ansible-galaxy init myrole<\/strong><\/pre>\n<p>For example, to create a role called <strong>test_role <\/strong>invoke the command.<\/p>\n<pre><strong>$ ansible-galaxy init <\/strong> <strong>test-role<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"857\" height=\"144\" class=\"wp-image-7501\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-654.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-654.png 857w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-654-300x50.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-654-768x129.png 768w\" sizes=\"auto, (max-width: 857px) 100vw, 857px\" \/><\/p>\n<p>From the output, we get that the command spawns a <strong>test-role <\/strong>directory and by default, it contains some default directories. You can use the <strong>tree<\/strong> command to list them as shown.<\/p>\n<pre><strong>$ tree test-role<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"818\" height=\"450\" class=\"wp-image-7502\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-655.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-655.png 818w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-655-300x165.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-655-768x422.png 768w\" sizes=\"auto, (max-width: 818px) 100vw, 818px\" \/><\/p>\n<p>Let\u2019s have a brief overview of what each directory contains<\/p>\n<p>The \u2018<strong>defaults<\/strong>\u2019 folder &#8211; This directory holds the default variables that will be required by the role. These variables have the lowest priority and are thus quite easy to override.<\/p>\n<p>The \u2018<strong>files<\/strong>\u2019 folder &#8211; This folder comprises files to be copied to the managed or remote host.<\/p>\n<p>The \u2018<strong>handlers<\/strong>\u2019 folder &#8211; The directory contains handlers that are usually evoked by the \u2018notify\u2019 directive. You can learn more about <a href=\"https:\/\/linuxways.net\/de\/ubuntu\/how-to-define-and-use-handlers-in-ansible-playbooks\/\">Ansible handlers<\/a>.<\/p>\n<p>The \u2018<strong>meta<\/strong>\u2019 folder &#8211; Consists of a role\u2019s metadata for instance author, dependencies, etc.<\/p>\n<p>The \u2018<strong>tasks<\/strong>\u2019 folder &#8211; It contains a YAML file that defines the list of tasks to be executed by the role itself. It contains the <strong>main.yml <\/strong> file.<\/p>\n<p>The \u2018<strong>templates<\/strong>\u2019 folder &#8211; The directory comprises template files that can be modified as deemed fit to configure the role.<\/p>\n<p>The \u2018<strong>tests<\/strong>\u2019 folder &#8211; Integrates testing with Ansible playbook files.<\/p>\n<p>The \u2018<strong>vars<\/strong>\u2019 folder &#8211; Comprises variables that will later be used by the role. role. These variables have a higher priority compared to those in the \u2018<strong>defaults<\/strong>\u2019 directory.<\/p>\n<p>We are going to create two roles for demonstration purposes:<\/p>\n<p>The <strong>git<\/strong> role &#8211; This will install the latest version of Git.<\/p>\n<p>The <strong>apache <\/strong>role- This will install the Apache webserver<\/p>\n<p>Create them as follows:<\/p>\n<pre><strong>$ sudo ansible-galaxy init git<\/strong><\/pre>\n<pre><strong>$ sudo ansible-galaxy init apache<\/strong><\/pre>\n<p>Thereafter, we need to define each role by editing the <strong>main.yml <\/strong>file in the <strong>\u2018tasks<\/strong>\u2019 folder in each role. Let\u2019s start by defining the <strong>git <\/strong>role.<\/p>\n<pre><strong>$ sudo vim git\/tasks\/main.yml<\/strong><\/pre>\n<p>Define the task for the git role as shown.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"817\" height=\"227\" class=\"wp-image-7503\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-656.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-656.png 817w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-656-300x83.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-656-768x213.png 768w\" sizes=\"auto, (max-width: 817px) 100vw, 817px\" \/><\/p>\n<p>Save the <strong>main.yml<\/strong> file and exit.<\/p>\n<p>Next, define the task for the Apache role.<\/p>\n<pre><strong>$ sudo vim apache\/tasks\/main.yml<\/strong><\/pre>\n<p>Specify the task that installs the Apache webserver.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"821\" height=\"222\" class=\"wp-image-7504\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-657.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-657.png 821w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-657-300x81.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-657-768x208.png 768w\" sizes=\"auto, (max-width: 821px) 100vw, 821px\" \/><\/strong><\/p>\n<p>Likewise, save the <strong>main.yml<\/strong> file and exit.<\/p>\n<h2><strong>Create a playbook file and call the roles<\/strong><\/h2>\n<p>Once the tasks for each role have been defined in the <strong>main.yml<\/strong> file for each role, create a playbook file and reference the roles as shown.<\/p>\n<pre><strong>$ sudo vim roles_demo.yml<\/strong><\/pre>\n<p>NOTE:<\/p>\n<p>You need to specify the full path to the role in the playbook.<\/p>\n<p><strong>roles:<\/strong><\/p>\n<ul>\n<li><strong> \/path\/to\/role <\/strong><\/li>\n<\/ul>\n<p>In this example, the roles reside in the same directory as the playbook file, and simply calling them by their names does the trick.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"809\" height=\"250\" class=\"wp-image-7505\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-658.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-658.png 809w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-658-300x93.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-658-768x237.png 768w\" sizes=\"auto, (max-width: 809px) 100vw, 809px\" \/><\/strong><\/p>\n<p>Then finally, run the playbook file.<\/p>\n<pre><strong>$ sudo ansible-playbook \/etc\/ansible\/roles_demo.yml<\/strong><\/pre>\n<p>The roles defined in the playbook file will be referenced and their respective tasks will be executed. Here, Ansible installs both git and Apache webserver.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"821\" height=\"370\" class=\"wp-image-7506\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-659.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-659.png 821w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-659-300x135.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-659-768x346.png 768w\" sizes=\"auto, (max-width: 821px) 100vw, 821px\" \/><\/p>\n<p>You can verify the installation by running the commands shown.<\/p>\n<pre><strong>$ git --version<\/strong><\/pre>\n<pre><strong>$ apachectl -v<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"820\" height=\"496\" class=\"wp-image-7507\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-660.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-660.png 820w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-660-300x181.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-660-768x465.png 768w\" sizes=\"auto, (max-width: 820px) 100vw, 820px\" \/><\/p>\n<p>As you have noted, the playbook file is brief and easy to follow along since the tasks have been referenced by the roles specified in the playbook file.<\/p>\n<h2><strong>Summing up<\/strong><\/h2>\n<p>Ansible roles help in the organization of playbook files and help avoid code duplication. They can be reused and referenced by other playbook files to execute similar tasks on different nodes thereby saving time and energy which could have been used in writing fresh playbooks. Overall, roles make work easier.<\/p>","protected":false},"excerpt":{"rendered":"<p>In the course of writing Ansible playbook files, you may have noticed that you could reuse some of the code defined in your existing playbooks. For example, you&hellip;<\/p>","protected":false},"author":1,"featured_media":7656,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,5,83,165,2],"tags":[230],"class_list":["post-7493","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-debian","category-mint","category-opensuse","category-red-hat","category-ubuntu","tag-ansible"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/7493","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=7493"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/7493\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/7656"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=7493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=7493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=7493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}