{"id":51765,"date":"2020-01-15T15:19:03","date_gmt":"2020-01-15T20:19:03","guid":{"rendered":"https:\/\/www.inmotionhosting.com\/support\/?p=51765"},"modified":"2020-04-30T11:52:03","modified_gmt":"2020-04-30T15:52:03","slug":"create-an-ansible-playbook","status":"publish","type":"post","link":"https:\/\/www.inmotionhosting.com\/support\/edu\/ansible\/create-an-ansible-playbook\/","title":{"rendered":"How to Create an Ansible Playbook"},"content":{"rendered":"\n<p>Now that we know more about how <a href=\"https:\/\/www.inmotionhosting.com\/ansible\">Ansible<\/a> works behind the scenes, let\u2019s create our first Ansible Playbook! The examples in this section were performed on a Linux Distribution, though the steps should be nearly the same on most Operating Systems.<\/p>\n\n\n\n<p>The first step in creating your first Ansible Playbook is to create a working directory to house it, and entering the directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ mkdir my-playbook\n$ cd my-playbook<\/code><\/pre>\n\n\n\n<p>We recommend that you use some type of version control software (VCS), though this is optional. For our example, we will use Git to track revision history:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git init\nInitialized empty Git repository in \/home\/&lt;user>\/my-playbook\/.git\/<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Creating an Ansible Inventory<\/h2>\n\n\n\n<p>Now we can start to define our inventory for this playbook. There are multiple strategies for managing an Ansible Inventory, though we\u2019ll use a single static inventory file for simplicity. If you are managing an inventory that changes frequently, you may want to consider using a Dynamic Inventory instead.<\/p>\n\n\n\n<p>Using your preferred text editor, create a file named \u2018inventory\u2019 in the \u2018my-playbook\u2018 directory we created earlier and add the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># inventory.yml\nlamp:\n    www1:<\/code><\/pre>\n\n\n\n<p>In the above example of an inventory file, we are deploying to a single host that we are referring to by its IP address or domain name.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a Task<\/h2>\n\n\n\n<p>Create a site.yml file and put the following in it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># site.yml\n- hosts: all\n\n  tasks:\n    - name: Install packages\n      package:\n        name: '{{ packages }}'\n        state: present<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Defining Variables<\/h2>\n\n\n\n<p>Ansible allows for configuring provided variables as well as defining your own. The following sources expand on the possible configuration options, as well as defining your own variables:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Configuration Reference | <a href=\"https:\/\/ansible.com\/\">ansible.com<\/a><\/li><li>Using Variables | <a href=\"https:\/\/ansible.com\/\">ansible.com<\/a><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Group Variables (group_vars)<\/h3>\n\n\n\n<p>In the above example of a plaintext inventory, the hosts are within a group, one of which is \u2018[webservers]\u2019. If you have a need to define a common set of variables for any host that is in a certain group, you may assign variables by creating the \u2018group_vars\/\u2019 directory, and within this directory you create the \u2018webservers\u2019 file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># group_vars\/webservers.yml\n\nansible_connection: \/bin\/python3<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Host Variables (host_vars)<\/h3>\n\n\n\n<p>Following our example of an inventory file, not only can we set group-specific variables, though we can set host-specific variables. This allows for us to set additional variables for a specific host that compliment or modify the variables for the group the host is assigned to.<\/p>\n\n\n\n<p>You may assign variables to hosts by creating the \u2018host_vars\/\u2019 directory, and within this directory you create the \u2018<strong>www1.example.com<\/strong>\u2019 file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># host_vars\/www1.yml\nansible_host: domain.tld\nansible_user: my_user<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Running your first playbook<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ansible-playbook main.yml -i inventory<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Now that we know more about how Ansible works behind the scenes, let\u2019s create our first Ansible Playbook! The examples in this section were performed on a Linux Distribution, though the steps should be nearly the same on most Operating Systems. The first step in creating your first Ansible Playbook is to create a working<a class=\"moretag\" href=\"https:\/\/www.inmotionhosting.com\/support\/edu\/ansible\/create-an-ansible-playbook\/\"> Read More ><\/a><\/p>\n","protected":false},"author":57014,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4349],"tags":[],"class_list":["post-51765","post","type-post","status-publish","format-standard","hentry","category-ansible"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Create an Ansible Playbook | InMotion Hosting<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.inmotionhosting.com\/support\/edu\/ansible\/create-an-ansible-playbook\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Create an Ansible Playbook | InMotion Hosting\" \/>\n<meta property=\"og:description\" content=\"Now that we know more about how Ansible works behind the scenes, let\u2019s create our first Ansible Playbook! The examples in this section were performed on a Linux Distribution, though the steps should be nearly the same on most Operating Systems. The first step in creating your first Ansible Playbook is to create a working Read More &gt;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.inmotionhosting.com\/support\/edu\/ansible\/create-an-ansible-playbook\/\" \/>\n<meta property=\"og:site_name\" content=\"InMotion Hosting Support Center\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/inmotionhosting\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-15T20:19:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-04-30T15:52:03+00:00\" \/>\n<meta name=\"author\" content=\"InMotion Hosting Contributor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/InMotionHosting\" \/>\n<meta name=\"twitter:site\" content=\"@InMotionHosting\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"InMotion Hosting Contributor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/ansible\/create-an-ansible-playbook\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/ansible\/create-an-ansible-playbook\/\"},\"author\":{\"name\":\"InMotion Hosting Contributor\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/f9a4fc454cd1df128ee8e898d30d4644\"},\"headline\":\"How to Create an Ansible Playbook\",\"datePublished\":\"2020-01-15T20:19:03+00:00\",\"dateModified\":\"2020-04-30T15:52:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/ansible\/create-an-ansible-playbook\/\"},\"wordCount\":391,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"articleSection\":[\"Ansible Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/edu\/ansible\/create-an-ansible-playbook\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/ansible\/create-an-ansible-playbook\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/ansible\/create-an-ansible-playbook\/\",\"name\":\"How to Create an Ansible Playbook | InMotion Hosting\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\"},\"datePublished\":\"2020-01-15T20:19:03+00:00\",\"dateModified\":\"2020-04-30T15:52:03+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/ansible\/create-an-ansible-playbook\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/edu\/ansible\/create-an-ansible-playbook\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/ansible\/create-an-ansible-playbook\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.inmotionhosting.com\/support\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Create an Ansible Playbook\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/\",\"name\":\"InMotion Hosting Support Center\",\"description\":\"Web Hosting Support &amp; Tutorials\",\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.inmotionhosting.com\/support\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\",\"name\":\"InMotion Hosting\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg\",\"contentUrl\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg\",\"width\":696,\"height\":696,\"caption\":\"InMotion Hosting\"},\"image\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/inmotionhosting\/\",\"https:\/\/x.com\/InMotionHosting\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/f9a4fc454cd1df128ee8e898d30d4644\",\"name\":\"InMotion Hosting Contributor\",\"description\":\"InMotion Hosting contributors are highly knowledgeable individuals who create relevant content on new trends and troubleshooting techniques to help you achieve your online goals!\",\"sameAs\":[\"https:\/\/www.linkedin.com\/company\/inmotion-hosting\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/InMotionHosting\"],\"url\":\"https:\/\/www.inmotionhosting.com\/support\/author\/inmotion-hosting-contributor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Create an Ansible Playbook | InMotion Hosting","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.inmotionhosting.com\/support\/edu\/ansible\/create-an-ansible-playbook\/","og_locale":"en_US","og_type":"article","og_title":"How to Create an Ansible Playbook | InMotion Hosting","og_description":"Now that we know more about how Ansible works behind the scenes, let\u2019s create our first Ansible Playbook! The examples in this section were performed on a Linux Distribution, though the steps should be nearly the same on most Operating Systems. The first step in creating your first Ansible Playbook is to create a working Read More >","og_url":"https:\/\/www.inmotionhosting.com\/support\/edu\/ansible\/create-an-ansible-playbook\/","og_site_name":"InMotion Hosting Support Center","article_publisher":"https:\/\/www.facebook.com\/inmotionhosting\/","article_published_time":"2020-01-15T20:19:03+00:00","article_modified_time":"2020-04-30T15:52:03+00:00","author":"InMotion Hosting Contributor","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/InMotionHosting","twitter_site":"@InMotionHosting","twitter_misc":{"Written by":"InMotion Hosting Contributor","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/ansible\/create-an-ansible-playbook\/#article","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/ansible\/create-an-ansible-playbook\/"},"author":{"name":"InMotion Hosting Contributor","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/f9a4fc454cd1df128ee8e898d30d4644"},"headline":"How to Create an Ansible Playbook","datePublished":"2020-01-15T20:19:03+00:00","dateModified":"2020-04-30T15:52:03+00:00","mainEntityOfPage":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/ansible\/create-an-ansible-playbook\/"},"wordCount":391,"commentCount":0,"publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"articleSection":["Ansible Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.inmotionhosting.com\/support\/edu\/ansible\/create-an-ansible-playbook\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/ansible\/create-an-ansible-playbook\/","url":"https:\/\/www.inmotionhosting.com\/support\/edu\/ansible\/create-an-ansible-playbook\/","name":"How to Create an Ansible Playbook | InMotion Hosting","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#website"},"datePublished":"2020-01-15T20:19:03+00:00","dateModified":"2020-04-30T15:52:03+00:00","breadcrumb":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/ansible\/create-an-ansible-playbook\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.inmotionhosting.com\/support\/edu\/ansible\/create-an-ansible-playbook\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/ansible\/create-an-ansible-playbook\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.inmotionhosting.com\/support\/"},{"@type":"ListItem","position":2,"name":"How to Create an Ansible Playbook"}]},{"@type":"WebSite","@id":"https:\/\/www.inmotionhosting.com\/support\/#website","url":"https:\/\/www.inmotionhosting.com\/support\/","name":"InMotion Hosting Support Center","description":"Web Hosting Support &amp; Tutorials","publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.inmotionhosting.com\/support\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.inmotionhosting.com\/support\/#organization","name":"InMotion Hosting","url":"https:\/\/www.inmotionhosting.com\/support\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/","url":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg","contentUrl":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg","width":696,"height":696,"caption":"InMotion Hosting"},"image":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/inmotionhosting\/","https:\/\/x.com\/InMotionHosting"]},{"@type":"Person","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/f9a4fc454cd1df128ee8e898d30d4644","name":"InMotion Hosting Contributor","description":"InMotion Hosting contributors are highly knowledgeable individuals who create relevant content on new trends and troubleshooting techniques to help you achieve your online goals!","sameAs":["https:\/\/www.linkedin.com\/company\/inmotion-hosting\/","https:\/\/x.com\/https:\/\/twitter.com\/InMotionHosting"],"url":"https:\/\/www.inmotionhosting.com\/support\/author\/inmotion-hosting-contributor\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"primary_category":null,"_links":{"self":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/51765","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/users\/57014"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/comments?post=51765"}],"version-history":[{"count":8,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/51765\/revisions"}],"predecessor-version":[{"id":55594,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/51765\/revisions\/55594"}],"wp:attachment":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/media?parent=51765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/categories?post=51765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/tags?post=51765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}