{"id":7153,"date":"2021-06-12T13:07:38","date_gmt":"2021-06-12T13:07:38","guid":{"rendered":"https:\/\/linuxways.net\/?p=7153"},"modified":"2021-06-12T13:07:38","modified_gmt":"2021-06-12T13:07:38","slug":"how-to-create-and-run-ansible-playbook-file","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-to-create-and-run-ansible-playbook-file\/","title":{"rendered":"How to Create and Run Ansible Playbook File"},"content":{"rendered":"<p>Ansible is a popular server configuration management tool that lets users manage and monitor remote systems from a single control node. With Ansible, you can install software packages, deploy services, and make configurations on multiple hosts from a single node instead of logging into each of the nodes. We already have a guide on <a href=\"https:\/\/linuxways.net\/de\/ubuntu\/how-to-install-ansible-on-ubuntu-20-04-lts\/\">how to install and set up Ansible on Ubuntu 20.04<\/a>. This will give you an introduction and a likely head start as you go over this guide. In this guide, we will narrow the focus to what playbooks are, how to create them, and use them to deploy services.<\/p>\n<h2><strong>Lab setup<\/strong><\/h2>\n<p>We already have a home lab as provided below. To get the most out of this guide you can replicate it or have a similar lab environment on a virtualized platform:<\/p>\n<p><strong>Ansible control node<\/strong> IP: 192.168.2.106<\/p>\n<p><strong>Managed host <\/strong> IP: 192.168.2.108<\/p>\n<p>With the setup in check, let\u2019s get started.<\/p>\n<h2><strong>What is a playbook file?<\/strong><\/h2>\n<p>Ansible, just like Terraform, falls under the Infrastructure as a Code. What does this mean? Infrastructure as a Code (IaC) is described as a mechanism of provisioning and managing hosts using machine-readable configuration files as opposed to physically logging in and making the configurations. In Ansible, a playbook is one such configuration file.<\/p>\n<p>A playbook is a file in YAML that contains one or more plays. What is a play? A play is an ordered task that automates a task or process on the managed host such as deploying an application such as a web server or making configurations. A playbook can have one or multiple plays, each performing different tasks.<\/p>\n<p>Plays make use of modules which are special functions to specify the changes required on the remote host. Each module is special and defines a particular task.<\/p>\n<p>A playbook file is saved with a <strong>.yml<\/strong> or <strong>.yaml<\/strong> file extension.<\/p>\n<h2><strong>Creating a playbook file<\/strong><\/h2>\n<p>Let\u2019s now create a playbook file. In this demonstration, we will create a playbook file called <strong>greetings.yml<\/strong> in the Ansible directory path <strong>\/etc\/ansible<\/strong> as shown.<\/p>\n<pre><strong>$ sudo vim \/etc\/ansible\/greetings.yml<\/strong><\/pre>\n<p>Add the following configuration. This is a simple playbook that prints a message to stdout on the remote server. Take careful note of the indentation of the modules.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"958\" height=\"231\" class=\"wp-image-7154\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-374.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-374.png 958w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-374-300x72.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-374-768x185.png 768w\" sizes=\"auto, (max-width: 958px) 100vw, 958px\" \/><\/p>\n<p>The Ansible Playbook file begins with three hyphens ( <strong>&#8212; <\/strong>) to indicate that it is a YAML file. The<strong> \u2018hosts\u2019<\/strong> parameter specifies the remote host or group of hosts defined in the inventory file, which by default is located in <strong>\/etc\/ansible\/hosts.<\/strong> Here, <strong>staging <\/strong>is the host group for which the remote host of IP <strong>192.168.2.108 <\/strong>is defined.<\/p>\n<p>The remote host is defined under the host group called <strong>staging <\/strong>with the following entries.<\/p>\n<pre><strong>[staging]<\/strong>\r\n\r\n<strong>192.168.2.108 ansible_ssh_pass=xxxxxxxx ansible_ssh_user=jack<\/strong><\/pre>\n<p>The <strong>ansible_ssh_pass <\/strong>specifies the SSH password of the remote user while <strong>ansible_ssh_use <\/strong>specifies the user name on the remote host.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"306\" class=\"wp-image-7155\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-375.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-375.png 960w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-375-300x96.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-375-768x245.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p>Next, we have the name of the play \u201c<strong>Print a simple message<\/strong>\u201d followed by the <strong>debug <\/strong>module that prints out the message defined by the <strong>msg<\/strong> module.<\/p>\n<h2><strong>Executing the playbook file<\/strong><\/h2>\n<p>To execute the playbook, simply use the <strong>ansible-playbook <\/strong>command in the syntax provided below.<\/p>\n<pre><strong>$ ansible-playbook \/path\/to\/playbook-file<\/strong><\/pre>\n<p>In our case, this is going to be:<\/p>\n<pre><strong>$ ansible-playbook \/etc\/ansible\/greetings.yml<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"962\" height=\"411\" class=\"wp-image-7156\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-376.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-376.png 962w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-376-300x128.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-376-768x328.png 768w\" sizes=\"auto, (max-width: 962px) 100vw, 962px\" \/><\/strong><\/p>\n<p>During play execution, Ansible first prints out the name of the host group or remote host on which the play will be executed -in our case the <strong>staging <\/strong>group. Ansible then retrieves information about the play, referred to as <strong>Facts,<\/strong> and finally performs the action specified in the playbook. Here, the simple message is printed.<\/p>\n<p>Let\u2019s take yet another example of a playbook file called <strong>install_apache_and_git.yml<\/strong> as shown below. Here, we have two plays. The first play installs the Apache webserver while the second play installs git on the remote system. The <strong>become: true <\/strong>parameter executes the command as an elevated user or sudo user on the remote user as is expected.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"958\" height=\"381\" class=\"wp-image-7157\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-377.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-377.png 958w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-377-300x119.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-377-768x305.png 768w\" sizes=\"auto, (max-width: 958px) 100vw, 958px\" \/><\/p>\n<p>When the playbook is executed, all the plays are listed in order of execution from the first to the last. The playbook first installs the Apache web server before installing git. The <strong>&#8211;ask-become-pass<\/strong> directive prompts for the sudo user in order to carry out the tasks defined in the plays.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"962\" height=\"432\" class=\"wp-image-7158\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-378.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-378.png 962w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-378-300x135.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-378-768x345.png 768w\" sizes=\"auto, (max-width: 962px) 100vw, 962px\" \/><\/p>\n<h2><strong>Wrapping up<\/strong><\/h2>\n<p>And that\u2019s how you can create a simple playbook file and execute it. We hope that this provided a basic level understanding of an Ansible playbook file, its structure, and how you use it to carry out tasks on remote hosts.<\/p>","protected":false},"excerpt":{"rendered":"<p>Ansible is a popular server configuration management tool that lets users manage and monitor remote systems from a single control node. With Ansible, you can install software packages,&hellip;<\/p>","protected":false},"author":1,"featured_media":7239,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[230,100],"class_list":["post-7153","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu","tag-ansible","tag-ubuntu-20-04"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/7153","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=7153"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/7153\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/7239"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=7153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=7153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=7153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}