{"id":283,"date":"2019-02-16T08:55:52","date_gmt":"2019-02-16T13:55:52","guid":{"rendered":"https:\/\/chasberndt.com\/?p=283"},"modified":"2019-02-16T08:55:52","modified_gmt":"2019-02-16T13:55:52","slug":"ansible-roles","status":"publish","type":"post","link":"https:\/\/chasberndt.com\/?p=283","title":{"rendered":"Ansible Roles"},"content":{"rendered":"\n<p>I consider a role to be a reusable, (mostly) standalone component to be consumed within a playbook. For instance, we have several different tools which require JDK, so we have a single role called install-oracle-jdk which downloads the JDK, installs it, updates the cacerts with some of our internal certificates, and makes sure that Java is available in the path for all users.<\/p>\n\n\n\n<p>In the most simple case, a role is broken down like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\n__roles\/\n  |__role-name\/\n    |__tasks\/\n      |__main.yml\n    |__vars\/\n      |__main.yml<\/code><\/pre>\n\n\n\n<p>In side of the role-name directory are two directories, the tasks\/ directory and the vars\/ directory. Inside of those two directories is a file called main.yml. vars\/main.yml contains the default values of the variables needed for a given role and tasks\/main.yml contains the steps required to apply that role. The intent is that if a role is run within a playbook without any variables overridden (more on that later), it will use the default variables. For a role like install-oracle-jdk, there might be a need to specify a different version of JDK. The other benefit of this approach is that when we&#8217;ve gone from 8_181 to 8_191 to 8_201 (current at time of writing), we only need to change it in one place and the next time we run the playbook, we&#8217;re able to confirm that the same\/latest version of Java is installed across all machines in the inventory file which consume that role.<\/p>\n\n\n\n<p>We have other roles where we&#8217;d need to pipe in additional variables. When executing a playbook, we generally use the a defaults file which we keep up in the top level directory of our Ansible repository. In the next post, I&#8217;ll cover how\/when\/why we use these defaults files.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I consider a role to be a reusable, (mostly) standalone component to be consumed within a playbook. For instance, we have several different tools which require JDK, so we have a single role called install-oracle-jdk which downloads the JDK, installs it, updates the cacerts with some of our internal certificates, and makes sure that Java <a class=\"read-more\" href=\"https:\/\/chasberndt.com\/?p=283\">&hellip;&nbsp;<span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-283","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/chasberndt.com\/index.php?rest_route=\/wp\/v2\/posts\/283","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chasberndt.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chasberndt.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chasberndt.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chasberndt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=283"}],"version-history":[{"count":1,"href":"https:\/\/chasberndt.com\/index.php?rest_route=\/wp\/v2\/posts\/283\/revisions"}],"predecessor-version":[{"id":284,"href":"https:\/\/chasberndt.com\/index.php?rest_route=\/wp\/v2\/posts\/283\/revisions\/284"}],"wp:attachment":[{"href":"https:\/\/chasberndt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chasberndt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chasberndt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}