{"id":8169,"date":"2018-12-14T19:16:37","date_gmt":"2018-12-14T16:16:37","guid":{"rendered":"https:\/\/www.howtoforge.com\/how-to-install-and-configure-ansible-on-ubuntu-1804\/"},"modified":"2018-12-14T19:16:37","modified_gmt":"2018-12-14T16:16:37","slug":"%d8%a2%d9%85%d9%88%d8%b2%d8%b4-%d9%86%d8%b5%d8%a8-ansible-%d8%af%d8%b1-ubuntu","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/%d8%a2%d9%85%d9%88%d8%b2%d8%b4-%d9%86%d8%b5%d8%a8-ansible-%d8%af%d8%b1-ubuntu\/","title":{"rendered":"\u0622\u0645\u0648\u0632\u0634 \u0646\u0635\u0628 Ansible \u062f\u0631 ubuntu"},"content":{"rendered":"<div id=\"\">\n<p dir=\"ltr\" style=\"text-align: left;\">\n<p dir=\"ltr\" style=\"text-align: left;\">Ansible is a free and open source software that can be used to automate software provisioning, configuration management, and application deployment. Unlike Puppet or Chef, you don&#8217;t have to set up a client-server environment before using Ansible. You can manage multiple hosts from a central location. This tool is very simple yet powerful to automate complex multi-tier IT application environments. Ansible communicates over normal SSH channels to retrieve information from remote machines and perform tasks.<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">In this tutorial, we will learn how to install and use Ansible on Ubuntu 18.04 server.<\/p>\n<h2 id=\"requirements\" dir=\"ltr\" style=\"text-align: left;\">Requirements<\/h2>\n<ul dir=\"ltr\" style=\"text-align: left;\">\n<li>Two Ubuntu 18.04 server system with OpenSSH server installed.<\/li>\n<li>A static IP address 192.168.0.101 is set up on the server system and 192.168.0.104 is set up on the client system.<\/li>\n<li>A non-root user with sudo privileges is set up on both systems.<\/li>\n<\/ul>\n<h2 id=\"getting-started\" dir=\"ltr\" style=\"text-align: left;\">Getting Started<\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\">Before starting, update your system with the latest version with the following command:<\/p>\n<pre class=\"command\" dir=\"ltr\"><code spellcheck=\"false\">sudo apt-get update -y\nsudo apt-get upgrade -y<\/code><\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">Once your system is up-to-date, restart your system to apply the changes.<\/p>\n<h2 id=\"install-and-configure-ansible\" dir=\"ltr\" style=\"text-align: left;\">Install and Configure Ansible<\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\">By default, The latest version of Ansible is not available in the Ubuntu 18.04 default repository. So you will need to add Ansible PPA to your server. You can do this by running the following command:<\/p>\n<pre class=\"command\" dir=\"ltr\"><code spellcheck=\"false\">sudo apt-add-repository ppa:ansible\/ansible<\/code><\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">Next, update the repository and install Ansible with the following command:<\/p>\n<pre class=\"command\" dir=\"ltr\"><code spellcheck=\"false\">sudo apt-get update -y\nsudo apt-get install ansible -y<\/code><\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">Once the installation is completed, you can check the Ansible version with the following command:<\/p>\n<pre class=\"command\" dir=\"ltr\"><code spellcheck=\"false\">sudo ansible --version<\/code><\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">Output:<\/p>\n<pre dir=\"ltr\">ansible 2.7.1\n config file = \/etc\/ansible\/ansible.cfg\n configured module search path = [u'\/root\/.ansible\/plugins\/modules', u'\/usr\/share\/ansible\/plugins\/modules']\n ansible python module location = \/usr\/lib\/python2.7\/dist-packages\/ansible\n executable location = \/usr\/bin\/ansible\n python version = 2.7.15rc1 (default, Nov 12 2018, 14:31:15) [GCC 7.3.0]\n<\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">Next, you will need to define your client system which you want to manage in Ansible hosts file. You can do this by editing \/etc\/ansible\/hosts file:<\/p>\n<pre class=\"command\" dir=\"ltr\"><code spellcheck=\"false\">sudo nano \/etc\/ansible\/hosts<\/code><\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">Add the following lines:<\/p>\n<pre dir=\"ltr\">[Client]\nnode1 ansible_ssh_host=192.168.0.104\n<\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">Save and close the file, when you are finished.<\/p>\n<h2 id=\"configure-ssh-keys-for-client-system\" dir=\"ltr\" style=\"text-align: left;\">Configure SSH Keys for Client System<\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\">Ansible uses SSH to communicate with Client host. So you will need to configure key-based ssh authentication for Client host.<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">First, generate an SSH key pair with the following command:<\/p>\n<pre class=\"command\" dir=\"ltr\"><code spellcheck=\"false\">ssh-keygen<\/code><\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">Output:<\/p>\n<pre dir=\"ltr\">Generating public\/private rsa key pair.\nEnter file in which to save the key (\/root\/.ssh\/id_rsa): \nEnter passphrase (empty for no passphrase): \nEnter same passphrase again: \nYour identification has been saved in \/root\/.ssh\/id_rsa.\nYour public key has been saved in \/root\/.ssh\/id_rsa.pub.\nThe key fingerprint is:\nSHA256:bTVjTCKqxD8rnIw7\/6mB6ZH9cnhzzOFg+\/+x4clSOow <a class=\"__cf_email__\" href=\"https:\/\/www.howtoforge.com\/cdn-cgi\/l\/email-protection\" data-cfemail=\"9ceef3f3e8dce9fee9f2e8e9ada4aca8\" target=\"_blank\" rel=\"noopener\">[email\u00a0protected]<\/a>\nThe key's randomart image is:\n+---[RSA 2048]----+\n| . . . |\n| . . . + |\n| o . * |\n| . o . o o |\n| . o S o |\n| O .oo.. . |\n| * Bo.* + oo |\n| ..oo+=.E =o = |\n| ooo*++...+* |\n+----[SHA256]-----+\n<\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">Next, copy this public key to the Client system with the following command:<\/p>\n<pre class=\"command\" dir=\"ltr\"><code spellcheck=\"false\">ssh-copy-id -i ~\/.ssh\/id_rsa.pub <a class=\"__cf_email__\" href=\"https:\/\/www.howtoforge.com\/cdn-cgi\/l\/email-protection\" data-cfemail=\"a2d0cdcdd6e2939b908c93949a8c928c939296\" target=\"_blank\" rel=\"noopener\">[email\u00a0protected]<\/a><\/code><\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">Output:<\/p>\n<pre dir=\"ltr\">\/usr\/bin\/ssh-copy-id: INFO: Source of key(s) to be installed: \"\/root\/.ssh\/id_rsa.pub\"\n\/usr\/bin\/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed\n\/usr\/bin\/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys\n<a class=\"__cf_email__\" href=\"https:\/\/www.howtoforge.com\/cdn-cgi\/l\/email-protection\" data-cfemail=\"1b6974746f5b2a2229352a2d23352b352a2b2f\" target=\"_blank\" rel=\"noopener\">[email\u00a0protected]<\/a>'s password: \n\nNumber of key(s) added: 1\n\nNow try logging into the machine, with: \"ssh '<a class=\"__cf_email__\" href=\"https:\/\/www.howtoforge.com\/cdn-cgi\/l\/email-protection\" data-cfemail=\"760419190236474f445847404e584658474642\" target=\"_blank\" rel=\"noopener\">[email\u00a0protected]<\/a>'\"\nand check to make sure that only the key(s) you wanted were added.\n<\/pre>\n<h2 id=\"working-with-ansible\" dir=\"ltr\" style=\"text-align: left;\">Working with Ansible<\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\">Ansible is now installed and configured. It&#8217;s time test Ansible.<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">On the Ansible server, try to ping your Client system using Ansible with the following command.<\/p>\n<pre class=\"command\" dir=\"ltr\"><code spellcheck=\"false\">ansible -m ping Client<\/code><\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">Output:<\/p>\n<pre dir=\"ltr\">node1 | SUCCESS =&gt; {\n \"changed\": false, \n \"ping\": \"pong\"\n}\n<\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">If you have multiple client configured in your Ansible hosts file. Then, you can ping all the Client system with the following command:<\/p>\n<pre class=\"command\" dir=\"ltr\"><code spellcheck=\"false\">ansible -m ping all<\/code><\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">You can check the status of the Apache web server on the Client system with the following command:<\/p>\n<pre class=\"command\" dir=\"ltr\"><code spellcheck=\"false\">ansible -m shell -a 'service apache2 status' Client<\/code><\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">Output:<\/p>\n<pre dir=\"ltr\">node1 | CHANGED | rc=0 &gt;&gt;\n * apache2 is running\n<\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">To check the partition size of the Client system, run the following command:<\/p>\n<pre class=\"command\" dir=\"ltr\"><code spellcheck=\"false\">ansible -m shell -a 'df -h' Client<\/code><\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">Output:<\/p>\n<pre dir=\"ltr\">node1 | CHANGED | rc=0 &gt;&gt;\nFilesystem Size Used Avail Use% Mounted on\n\/dev\/sda1 138G 48G 83G 37% \/\nnone 4.0K 0 4.0K 0% \/sys\/fs\/cgroup\nudev 1.9G 4.0K 1.9G 1% \/dev\ntmpfs 384M 1.2M 383M 1% \/run\nnone 5.0M 0 5.0M 0% \/run\/lock\nnone 1.9G 67M 1.9G 4% \/run\/shm\nnone 100M 36K 100M 1% \/run\/user\n\/dev\/sda5 225G 35G 180G 16% \/Data\n<\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">Congratulations! you have successfully installed and configured Ansible on Ubuntu 18.04 server. You can now easily use Ansible to execute simple tasks remotely<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Ansible is a free and open source software that can be used to automate software provisioning, configuration management, and application deployment. Unlike Puppet or Chef, you don&#8217;t have to set up a client-server environment before using Ansible. You can manage multiple hosts from a central location. This tool is very simple yet powerful to automate [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-8169","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/8169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=8169"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/8169\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=8169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=8169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=8169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}