{"id":5239,"date":"2021-03-22T14:18:31","date_gmt":"2021-03-22T14:18:31","guid":{"rendered":"https:\/\/linuxways.net\/?p=5239"},"modified":"2021-03-22T14:18:31","modified_gmt":"2021-03-22T14:18:31","slug":"how-to-install-ansible-on-ubuntu-20-04-lts","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-to-install-ansible-on-ubuntu-20-04-lts\/","title":{"rendered":"How to Install Ansible on Ubuntu 20.04 LTS"},"content":{"rendered":"<p>Configuring and deploying applications on a single server is usually a simple task. However, in a complex IT environment with multiple servers, this can be a daunting and time-consuming task. Developed and maintained by RedHat, Ansible is an open-source server configuration and software provisioning tool that simplifies application deployment and configuration management. It lifts the weight off of IT operation teams and automates the configuration of servers and the deployment of applications in a simple and effective manner.<\/p>\n<p>Compared to its counterparts such as chef and puppet, Ansible is the most widely used tool since it doesn\u2019t require an agent to communicate with remote hosts. Ansible leverages the SSH protocol to communicate with remote targets, and in doing so, the CPU resources of the remote hosts remain unaffected.<\/p>\n<h2><strong>Ansible Lab setup<\/strong><\/h2>\n<p>For our test environment, we have two nodes: the control node where Ansible will be installed and the remote host that will be configured from the Ansible control node:<\/p>\n<p><strong>Ansible control node <\/strong> IP 192.168.2.101 Ubuntu 20.04<\/p>\n<p><strong>Remote host <\/strong> IP 192.168.2.104 Ubuntu 20.04<\/p>\n<p>Additionally, ensure that you have a <strong>sudo user<\/strong> configured on the Ansible control node and that the SSH daemon is running on both nodes.<\/p>\n<p>Without much further ado, let\u2019s touch base on how to install Ansible on Ubuntu 20.04<\/p>\n<h2><strong>Step 1: Install Ansible <\/strong><\/h2>\n<p>Right off the bat, log in to the Ansible master server and install Ansible as the sudo user as follows.<\/p>\n<pre><strong>$ sudo apt install ansible<\/strong><\/pre>\n<p>This installs Ansible alongside a host of Python3 packages that are essential for Ansible to run without issues.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"435\" class=\"wp-image-5240\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-312.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-312.png 726w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-312-300x180.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-312-501x300.png 501w\" sizes=\"auto, (max-width: 726px) 100vw, 726px\" \/><\/strong><\/p>\n<p>Once installed, you can confirm the Ansible version as shown.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"228\" class=\"wp-image-5241\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-313.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-313.png 739w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-313-300x93.png 300w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/strong><\/p>\n<p>The output provides some extensive information. It displays the version of Ansible &#8211; in this case, Ansible 2.9.6 &#8211; the module and Ansible executable path as well as the Python version installed on your system.<\/p>\n<p>The configuration file &#8211; <strong>ansible.cfg<\/strong> &#8211; is the Ansible configuration file located in the <strong>\/etc\/ansible <\/strong>directory. The settings determine how Ansible runs and interacts with remote clients. Additionally, the directory contains an inventory file called <strong>hosts. <\/strong>This is where remote or managed hosts are defined. As we plod along in this tutorial, we will define the remote host in this inventory file.<\/p>\n<h2><strong>Step 2: Set up passwordless SSH login<\/strong><\/h2>\n<p>The SSH protocol provides two methods of authentication: password and public\/private key authentication. Using SSH keys, we are going to set up a passwordless ssh login method to provide a safer and more convenient way of interacting with the remote host. The passwordless SSH login method provides a seamless way of interacting with the host since we won\u2019t be required to provide a password when running playbook files.<\/p>\n<p>To set up passwordless SSH login, we are first going to generate the SSH key pair as shown:<\/p>\n<pre><strong>$ ssh-keygen<\/strong><\/pre>\n<p>Press ENTER all the way to accept the default suggestions.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"718\" height=\"467\" class=\"wp-image-5242\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-314.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-314.png 718w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-314-300x195.png 300w\" sizes=\"auto, (max-width: 718px) 100vw, 718px\" \/><\/p>\n<p>The SSH key pair includes a private and public key. We have the <strong>id_rsa<\/strong> which is the private key. Also, we have the <strong>id_rsa.pub <\/strong> which is the public key.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"505\" height=\"111\" class=\"wp-image-5243\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-315.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-315.png 505w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-315-300x66.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-315-501x111.png 501w\" sizes=\"auto, (max-width: 505px) 100vw, 505px\" \/><\/strong><\/p>\n<p>Next, we are going to copy the public key to the remote host using the root user as the remote host\u2019s user.<\/p>\n<h3>NOTE<\/h3>\n<p>Before copying the ssh public key file, we will configure the remote user in the Ansible configuration file as shown.<\/p>\n<pre><strong>$ sudo vim \/etc\/ansible\/ansible.cfg<\/strong><\/pre>\n<p>Set the remote user to <strong>root.<\/strong><\/p>\n<pre><strong>remote_user=root<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"649\" height=\"171\" class=\"wp-image-5244\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-316.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-316.png 649w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-316-300x79.png 300w\" sizes=\"auto, (max-width: 649px) 100vw, 649px\" \/><\/p>\n<p>Save the file.<\/p>\n<p>Next, head over to the remote host and enable remote root login by editing the <strong>\/etc\/ssh\/ssh_config <\/strong>file.<\/p>\n<pre><strong>$ sudo vim \/etc\/ssh\/ssh_config<\/strong><\/pre>\n<p>Append the line below<\/p>\n<pre><strong>PermitRootLogin yes<\/strong><\/pre>\n<p>Once again, save and exit.<\/p>\n<p>Now we can proceed and copy the SSH public key to the remote host as shown.<\/p>\n<pre><strong>$ ssh-copy-id root@192.168.2.104<\/strong><\/pre>\n<p>If you are logging in for the first time as root user, you will get the prompt as shown in the image below. Simply type \u2018yes\u2019 to continue connecting. Once you provide the remote host\u2019s root password, the SSH key will be added to the root home directory.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"767\" height=\"363\" class=\"wp-image-5245\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-317.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-317.png 767w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-317-300x142.png 300w\" sizes=\"auto, (max-width: 767px) 100vw, 767px\" \/><\/strong><\/p>\n<p>For subsequent logins, you won\u2019t be required to provide a password. To just verify this, exit from the remote host and try logging in once more.<\/p>\n<pre><strong>$ ssh root@192.168.2.104<\/strong><\/pre>\n<p>This automatically takes you to the remote host\u2019s shell as shown.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"764\" height=\"476\" class=\"wp-image-5246\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-318.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-318.png 764w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-318-300x187.png 300w\" sizes=\"auto, (max-width: 764px) 100vw, 764px\" \/><\/strong><\/p>\n<p>Perfect! Our passwordless SSH login setup is complete. Now we will create a playbook file that will install the Apache webserver.<\/p>\n<h2><strong>Step 3: Add the remote host in the Ansible configuration file<\/strong><\/h2>\n<p>Next up, we will define our remote host in the Ansible inventory file which, by default, is the<strong> \/etc\/ansible\/hosts<\/strong> file. So, open the file.<\/p>\n<pre><strong> $ sudo vim \/etc\/ansible\/hosts<\/strong><\/pre>\n<p>The file <strong>hosts<\/strong> is structured in INI format. We are going to add the IP address of our host under the <strong>webservers<\/strong> group name.<\/p>\n<pre><strong>[webservers]<\/strong><\/pre>\n<pre><strong>192.168.2.104<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"607\" height=\"249\" class=\"wp-image-5247\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-319.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-319.png 607w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-319-300x123.png 300w\" sizes=\"auto, (max-width: 607px) 100vw, 607px\" \/><\/p>\n<p>Save the inventory file and close. To verify the hosts defined in the inventory file, execute:<\/p>\n<pre><strong> $ ansible webservers --list-hosts<\/strong><\/pre>\n<p>Alternatively, to check all hosts in all the host groups, execute:<\/p>\n<pre><strong> $ ansible all --list-hosts<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"744\" height=\"264\" class=\"wp-image-5248\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-320.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-320.png 744w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-320-300x106.png 300w\" sizes=\"auto, (max-width: 744px) 100vw, 744px\" \/><\/p>\n<p>Now we are going to test for the presence of the Ansible Python module on the remote host.<\/p>\n<pre><strong> $ ansible -m ping webservers<\/strong><\/pre>\n<h3>NOTE:<\/h3>\n<p>The <strong>ping <\/strong>module is not an ICMP ping command. It\u2019s just a trivial test to check if the Python3 module is installed and the presence of SSH.<\/p>\n<p>The output below shows that the command was successful. The output also displays the IP of the remote host defined under the <strong>webservers <\/strong>group. If you have multiple hosts defined under several groups, use the <strong>all <\/strong>option.<\/p>\n<pre><strong> $ ansible -m ping all<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"722\" height=\"430\" class=\"wp-image-5249\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-321.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-321.png 722w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-321-300x179.png 300w\" sizes=\"auto, (max-width: 722px) 100vw, 722px\" \/><\/p>\n<h2><strong>Step 4: Create a playbook file to configure the remote host<\/strong><\/h2>\n<p>With the remote host defined, we are now going to create a playbook file to install the Apache webserver. A playbook file is a YAML file in Ansible that stipulates the tasks to be carried out in the remote host.<\/p>\n<pre><strong> $ sudo vim \/etc\/ansible\/apache.yml<\/strong><\/pre>\n<p>The playbook file is defined below. Note that the file begins with 3 hyphens ( &#8212; ). Also, take note of the indentations.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"760\" height=\"286\" class=\"wp-image-5250\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-322.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-322.png 760w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-322-300x113.png 300w\" sizes=\"auto, (max-width: 760px) 100vw, 760px\" \/><\/strong><\/p>\n<p>Lastly, execute the playbook file as follows.<\/p>\n<pre><strong>$ ansible-playbook \/etc\/ansible\/apache.yml<\/strong><\/pre>\n<p>During runtime, the playbook carries out the tasks meticulously and displays every action on the terminal. It installs the Apache webserver and starts the service.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"830\" height=\"358\" class=\"wp-image-5251\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-323.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-323.png 830w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-323-300x129.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-323-768x331.png 768w\" sizes=\"auto, (max-width: 830px) 100vw, 830px\" \/><\/strong><\/p>\n<p>To confirm that the webserver was successfully installed, launch a browser and browse the remote host\u2019s IP as shown:<\/p>\n<pre><strong>http:\/\/server-ip\/<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"1185\" height=\"579\" class=\"wp-image-5252\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-324.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-324.png 1185w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-324-300x147.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-324-1024x500.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-324-768x375.png 768w\" sizes=\"auto, (max-width: 1185px) 100vw, 1185px\" \/><\/strong><\/p>\n<p><strong>Summary<\/strong><\/p>\n<p>Ansible is a widely used automation tool in DevOps and used by thousands of developers and sysadmins to configure and deploy applications on servers. It saves time and minimizes the effort required to handle a multitude of servers in a complex IT infrastructure. We do hope that you can comfortably install and configure Ansible on Ubuntu 20.04.<\/p>","protected":false},"excerpt":{"rendered":"<p>Configuring and deploying applications on a single server is usually a simple task. However, in a complex IT environment with multiple servers, this can be a daunting and&hellip;<\/p>","protected":false},"author":1,"featured_media":5323,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[230,100],"class_list":["post-5239","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\/5239","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=5239"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/5239\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/5323"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=5239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=5239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=5239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}