{"id":800,"date":"2023-02-10T01:56:00","date_gmt":"2023-02-10T01:56:00","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=800"},"modified":"2023-02-10T01:56:00","modified_gmt":"2023-02-10T01:56:00","slug":"install-configure-ansible-server-nodes","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=800","title":{"rendered":"How to Install and Configure Ansible Control Node and Hosts"},"content":{"rendered":"<p>This guide will teach you to install and configure the Ansible control node and hosts. Also, we will look at how to configure inventory that has to be managed by the Ansible server.<\/p>\n<p>I would strongly suggest using <a href=\"https:\/\/devopscube.com\/vagrant-tutorial-beginners\/\">Vagrant<\/a> for all Ansible test purposes.<\/p>\n<h2 id=\"setup-prerequisites\">Setup Prerequisites<\/h2>\n<p>To follow this setup, you should have the following.<\/p>\n<ol>\n<li>One server for Ansible Control Node<\/li>\n<li>One or more ansible hosts to test ansible remote configurations and ad-hoc commands.<\/li>\n<\/ol>\n<h2 id=\"install-ansible-on-ubuntu\">Install  Ansible on Ubuntu<\/h2>\n<p>Choose the following methods for installing Ansible.<\/p>\n<h3 id=\"1-using-pip\">1. Using Pip<\/h3>\n<p>If you have python-pip installed in your system, use the following pip command.<\/p>\n<p>sudo pip install ansible<\/p>\n<h3 id=\"2-using-ubuntu-apt-repository\">2. Using Ubuntu apt repository<\/h3>\n<p>To install it from the apt repository, execute the following commands to install ansible.<\/p>\n<p>sudo apt-add-repository -y ppa:ansible\/ansible sudo apt-get update sudo apt-get install -y ansible<\/p>\n<p>To validate the installation, execute the following ansible command.<\/p>\n<pre><code>ansible --version<\/code><\/pre>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-10-41.png\" class=\"kg-image\" alt=\"verifying ansible installtion.\" loading=\"lazy\" width=\"574\" height=\"286\"><\/figure>\n<h2 id=\"configuring-ansible-inventory\">Configuring Ansible Inventory<\/h2>\n<p>All the servers that have to be managed using Ansible are called <strong>Ansible hosts<\/strong>.<\/p>\n<h3 id=\"configure-ansiblecfg\">Configure ansible.cfg<\/h3>\n<p>Ansible loads the configuration from the <strong><code>ansible.cfg<\/code><\/strong> file located in the following locations. The file precedence is as per the order given below.<\/p>\n<ol>\n<li>ANSIBLE_CONFIG env variable<\/li>\n<li>.\/ansible.cfg (current directory)<\/li>\n<li>~\/.ansible.cfg (home directory)<\/li>\n<li>\/etc\/ansible\/ansible.cfg<\/li>\n<\/ol>\n<p>Open <strong><code>\/etc\/ansible\/ansible.cfg<\/code><\/strong>  file and add the following.<\/p>\n<pre><code>[defaults]\ninventory = \/etc\/ansible\/inventory.ini\nhost_key_checking = False<\/code><\/pre>\n<p>Here we have mentioned two default configurations.<\/p>\n<ol>\n<li><strong>inventory<\/strong>: Location of the inventory file. Which we will discuss in the next section.<\/li>\n<li><strong>host_key_checking:<\/strong> It disables the ssh host key checking when ansible tries to ssh into the remote machine.<\/li>\n<\/ol>\n<h3 id=\"configure-ansible-inventory\">Configure Ansible Inventory<\/h3>\n<p>Ansible keeps track of the hosts using the inventory file ( A file with the list of servers). It has the host the IP address\/domain name with username, password, or <a href=\"https:\/\/devopscube.com\/generate-ssh-key-pair\/\">SSH key<\/a> information to connect to the node.<\/p>\n<p>In every environment the servers are segregated as web group, DB group, app group, etc.. we can have similar segregation in our inventory file using labels. It is a recommended way of managing servers.<\/p>\n<p>For instance, you might have dev, test, and prod servers. In this case, you can group servers from different environments under different labels.<\/p>\n<p>By default, the inventory file named hosts is present in <strong><code>\/etc\/ansible\/<\/code><\/strong> directory. If you open the <strong><code>\/etc\/ansible\/hosts<\/code><\/strong> you will find the sample host entries. Let&#8217;s keep this file as a backup and create our own host file.<\/p>\n<p>Let&#8217;s create our own inventory file named <code>inventory.ini <\/code>as we mentioned in the <code>ansible.cfg<\/code> file.<\/p>\n<pre><code>sudo vi \/etc\/ansible\/inventory.ini<\/code><\/pre>\n<p>My ansible host&#8217;s IP addresses are <strong><code>192.168.2.30<\/code><\/strong> and <strong><code>192.168.2.40<\/code><\/strong> and I will be using them throughout the example. Replace it with your host&#8217;s IP address. So I am adding it under the dev label. Modify the IP and label as per your requirements.<\/p>\n<pre><code>[dev]\n192.168.2.30\n192.168.2.40\n\n[dev:vars]\nansible_user=ubuntu\nansible_ssh_pass=vargrant \nansible_private_key_file=\/path\/to\/key.pem\n\n[all:vars]\nansible_user=ubuntu\nansible_ssh_pass=vargrant \nansible_private_key_file=\/path\/to\/key.pem\n\n[local]\n127.0.0.1<\/code><\/pre>\n<p><code>[<strong>dev:vars]<\/strong><\/code> parameters are applied to the servers under the dev label. As we know that Ansible uses ssh for connecting to hosts. So we need to specify the <strong>username<\/strong>, password, or ssh key of those hosts.<\/p>\n<p>If all the servers have the same username and password\/ssh-key, you can mention it in <strong><code>dev:vars<\/code><\/strong> label. <\/p>\n<p>If not you can specify it with the IP addresses separated by space as shown below.<\/p>\n<p>192.168.2.30 ansible_user=vagrant ansible_ssh_pass=vargrant<\/p>\n<p><strong><code>[all:vars]<\/code><\/strong> The parameters added under this label will be applicable to all the servers mentioned in the inventory file. It is the global variable for all the servers under different labels.<\/p>\n<p>The <strong>local label<\/strong> represents the Ansible server itself. So if you want to run a playbook on your ansible server, you can make use of the local label.<\/p>\n<h2 id=\"test-remote-connection-using-ansible-ad-hoc-command\">Test Remote Connection Using Ansible Ad-Hoc Command<\/h2>\n<p>Now we have every Configuration in place. Let&#8217;s test our configuration using the following command.<\/p>\n<p>ansible all -m ping<\/p>\n<p>You should get the following success message.<\/p>\n<pre><code>vagrant@acs:\/etc\/ansible$ ansible all -m ping\n192.168.2.30 | SUCCESS =&gt; {\n    \"changed\": false,\n    \"ping\": \"pong\"\n}\n192.168.2.40 | SUCCESS =&gt; {\n    \"changed\": false,\n    \"ping\": \"pong\"\n}<\/code><\/pre>\n<h2 id=\"important-ansible-commands\">Important Ansible Commands<\/h2>\n<p>You can check the configured inventory list using the following command.<\/p>\n<pre><code>ansible-inventory --list -y<\/code><\/pre>\n<figure class=\"kg-card kg-image-card kg-card-hascaption\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-11-36.png\" class=\"kg-image\" alt=\"ansible inventory list command.\" loading=\"lazy\" width=\"613\" height=\"399\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-11-36.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-11-36.png 613w\"><figcaption><span style=\"white-space: pre-wrap;\">Click to view in HD<\/span><\/figcaption><\/figure>\n<p>Run ad-hoc commands on all the servers in the inventory.<\/p>\n<pre><code>ansible all -b -m apt -a \"name=vim state=latest\" <\/code><\/pre>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>When it comes to <a href=\"https:\/\/devopscube.com\/infrastructure-as-code-configuration-management\/\">configuration management,<\/a> Ansible is one of the best options for <a href=\"https:\/\/devopscube.com\/become-devops-engineer\/\">devops engineers<\/a>. Also when it comes to <a href=\"https:\/\/devopscube.com\/immutable-infrastructure\/\">immutable infrastructure<\/a>, you can use ansible along with tools like packer to create deployable VM images.<\/p>\n<p>Wheater it is cloud or on-prem, Ansible provides good options for provisioning and configuration management. It can be easily integrated with the CI\/CD systems as well.<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/install-configure-ansible-server-nodes\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Install and Configure Ansible Control Node and Hosts \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/install-configure-ansible-server-nodes\/<\/p>\n","protected":false},"author":1,"featured_media":801,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-800","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/800","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=800"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/800\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/801"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}