{"id":11794,"date":"2019-03-17T10:36:23","date_gmt":"2019-03-17T10:36:23","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=11794"},"modified":"2019-03-17T10:36:23","modified_gmt":"2019-03-17T10:36:23","slug":"how-to-install-and-configure-ansible-automation-tool-for-it-management","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/17\/how-to-install-and-configure-ansible-automation-tool-for-it-management\/","title":{"rendered":"How to Install and Configure \u2018Ansible\u2019 Automation Tool for IT Management"},"content":{"rendered":"<p><b>Ansible<\/b>\u00a0is an open source, powerful automation software for configuring, managing and deploying software applications on the nodes without any downtime just by using SSH. Today, most of the IT Automation tools runs as a agent in remote host, but ansible just need a SSH connection and Python (2.4 or later) to be installed on the remote nodes to perform it\u2019s action.<\/p>\n<div id=\"attachment_11098\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Install-Ansible-in-Linux1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-11098\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Install-Ansible-in-Linux1-620x293.jpg\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Install-Ansible-in-Linux1-620x293.jpg 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Install-Ansible-in-Linux1-520x245.jpg 520w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Install-Ansible-in-Linux1.jpg 720w\" alt=\"Install Ansible in Linux\" width=\"620\" height=\"293\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Install Ansible in Linux<\/p>\n<\/div>\n<h4>How Ansible Works?<\/h4>\n<p>There are many similar automation tools available like Puppet, Capistrano, Chef, Salt, Space Walk etc, but Ansible categorize into two types of server: controlling machines and nodes.<\/p>\n<p>The controlling machine, where Ansible is installed and Nodes are managed by this controlling machine over SSH. The location of nodes are specified by controlling machine through its inventory.<\/p>\n<p>The controlling machine (Ansible) deploys modules to nodes using SSH protocol and these modules are stored temporarily on remote nodes and communicate with the Ansible machine through a JSON connection over the standard output.<\/p>\n<p><center>Ansible is agent-less, that means no need of any agent installation on remote nodes, so it means there are no any background daemons or programs are executing for Ansible, when it\u2019s not managing any nodes.<\/center>Ansible can handle 100\u2019s of nodes from a single system over SSH connection and the entire operation can be handled and executed by one single command \u2018ansible\u2019. But, in some cases, where you required to execute multiple commands for a deployment, here we can build playbooks.<\/p>\n<p>Playbooks are bunch of commands which can perform multiple tasks and each playbooks are in YAML file format.<\/p>\n<h4>What\u2019s the Use of Ansible<\/h4>\n<p><strong>Ansible<\/strong>\u00a0can be used in IT infrastructure to manage and deploy software applications to remote nodes. For example, let\u2019s say you need to deploy a single software or multiple software to 100\u2019s of nodes by a single command, here ansible comes into picture, with the help of Ansible you can deploy as many as applications to many nodes with one single command, but you must have a little programming knowledge for understanding the ansible scripts.<\/p>\n<p>We\u2019ve compiled a series on Ansible, title \u2018<strong>Preparation for the Deployment of your IT Infrastructure with Ansible IT Automation Tool<\/strong>\u2018, through parts 1-4 and covers the following topics.<\/p>\n<div id=\"exam_announcement\"><b>Part 1<\/b>:\u00a0<b>How to Install and Configure Ansible for IT Management in Linux<\/b><\/div>\n<div id=\"exam_announcement\"><b>Part 2<\/b>:\u00a0<a href=\"https:\/\/www.tecmint.com\/use-anisble-playbooks-to-automate-complex-tasks-on-multiple-linux-servers\/\" target=\"_blank\" rel=\"noopener\">How to Use Anisble Playbooks to Automate Complex Tasks on Multiple Remote Servers<\/a><\/div>\n<div id=\"exam_announcement\"><b>Part 3<\/b>:\u00a0<a href=\"https:\/\/www.tecmint.com\/automate-wordpress-installations-in-multiple-linux-servers-using-ansible\/\" target=\"_blank\" rel=\"noopener\">How to Automate Simultaneous WordPress Deployments in Multiple Linux Servers Using Ansible<\/a><\/div>\n<div id=\"exam_announcement\"><b>Part 4<\/b>:\u00a0<b>Managing Encrypted YAMAL data with Ansible-Vault<\/b><\/div>\n<p>In this article, we will show you how to install \u2018Ansible\u2019 on RHEL\/CentOS 7\/6, Fedora 21-19, Ubuntu 14.10-13.04 and Debian 7\/6 systems and also we will go through some basics on how how to manage a server by installing packages, applying updates and much more from basic to pro.<\/p>\n<h4>Prerequisites<\/h4>\n<ol>\n<li><b>Operating System<\/b>: RHEL\/CentOS\/Fedora and Ubuntu\/Debian\/Linux Mint<\/li>\n<li><b>Jinja2<\/b>: A modern, fast and easy to use stand-alone template engine for Python.<\/li>\n<li><b>PyYAML<\/b>: A YAML parser and emitter for the Python programming language.<\/li>\n<li><b>parmiko<\/b>: A native Python SSHv2 channel library.<\/li>\n<li><b>httplib2<\/b>: A comprehensive HTTP client library.<\/li>\n<li><b>sshpass<\/b>: A non-interactive ssh password authentication.<\/li>\n<\/ol>\n<h4>My Environment Setup<\/h4>\n<h5>Controlling Machine \u2013 Ansible<\/h5>\n<pre>Operating System :\tLinux Mint 17.1 Rebecca\r\nIP Address\t :\t192.168.0.254\r\nHost-name\t :\ttecmint.instrcutor.com\r\nUser\t\t :\ttecmint\r\n<\/pre>\n<h5>Remote Nodes<\/h5>\n<pre>Node 1: 192.168.0.112\r\nNode 2: 192.168.0.113\r\nNode 3: 192.168.0.114\r\n<\/pre>\n<h3>Step 1: Installing Controlling Machine \u2013 Ansible<\/h3>\n<p><strong>1.<\/strong>\u00a0Before installing \u2018<strong>Ansible<\/strong>\u2018 on the server, let\u2019s first verify the details of the server like hostname and IP Address. Login into server as a root user and execute the below command to confirm system settings that we\u2019re going to use for this setup.<\/p>\n<pre># sudo ifconfig | grep inet\r\n<\/pre>\n<div id=\"attachment_11080\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Verify-System-Details.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-11080\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Verify-System-Details-620x164.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Verify-System-Details-620x164.png 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Verify-System-Details.png 622w\" alt=\"Verify System Details\" width=\"620\" height=\"164\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Verify System Details<\/p>\n<\/div>\n<p><strong>2.<\/strong>\u00a0Once you confirm your system settings, it\u2019s time to install \u2018Ansible\u2019 software on the system.<\/p>\n<h5>On Ubuntu\/Debian\/Linux Mint<\/h5>\n<p>Here we are going to use official Ansible PPA repository on the system, just run the below commands to add the repository.<\/p>\n<pre>$ sudo apt-add-repository ppa:ansible\/ansible -y\r\n$ sudo apt-get update &amp;&amp; sudo apt-get install ansible -y\r\n<\/pre>\n<div id=\"attachment_11084\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Add-Ansible-PPA3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-11084\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Add-Ansible-PPA3-620x212.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Add-Ansible-PPA3-620x212.png 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Add-Ansible-PPA3.png 934w\" alt=\"Add Ansible PPA\" width=\"620\" height=\"212\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Add Ansible PPA<\/p>\n<\/div>\n<div id=\"attachment_11085\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Install-Ansible-in-Ubuntu1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-11085\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Install-Ansible-in-Ubuntu1.png\" alt=\"Install Ansible in Ubuntu\" width=\"535\" height=\"153\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Install Ansible in Ubuntu<\/p>\n<\/div>\n<h5>On RHEL\/CentOS\/Fedora<\/h5>\n<p>Unfortunately, there are no official Ansible repository for RedHat based clones, but we can install Ansible by enabling epel repository under RHEL\/CentOS 6, 7 and currently supported fedora distributions.<\/p>\n<p>Fedora users can directly install Ansible through default repository, but if you are using RHEL\/CentOS 6, 7, you have to\u00a0<a href=\"https:\/\/www.tecmint.com\/how-to-enable-epel-repository-for-rhel-centos-6-5\/\" target=\"_blank\" rel=\"noopener\">enable EPEL repo<\/a>.<\/p>\n<p>After configuring epel repository, you can install Ansible using following command.<\/p>\n<pre>$ sudo yum install ansible -y\r\n<\/pre>\n<p>After installed successfully, you can verify the version by executing below command.<\/p>\n<pre># ansible --version\r\n<\/pre>\n<div id=\"attachment_11086\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Verify-Ansible-Version1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-11086\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Verify-Ansible-Version1.png\" alt=\"Verify Ansible Version\" width=\"404\" height=\"123\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Verify Ansible Version<\/p>\n<\/div>\n<h3>Step 2: Preparing SSH Keys to Remote Hosts<\/h3>\n<p><strong>4.<\/strong>\u00a0To perform any deployment or management from the localhost to remote host first we need to create and copy the ssh keys to the remote host. In every remote host there will be a user account\u00a0<strong>tecmint<\/strong>\u00a0(in your case may be different user).<\/p>\n<p>First let we create a SSH key using below command and copy the key to remote hosts.<\/p>\n<pre># ssh-keygen -t rsa -b 4096 -C \"admin@tecmintlocal.com\"\r\n<\/pre>\n<div id=\"attachment_11087\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Create-SSH-Key1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-11087\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Create-SSH-Key1-620x426.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Create-SSH-Key1-620x426.png 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Create-SSH-Key1.png 622w\" alt=\"Create SSH Key\" width=\"620\" height=\"426\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Create SSH Key<\/p>\n<\/div>\n<p><strong>5.<\/strong>\u00a0After creating SSH Key successfully, now copy the created key to all three remote server\u2019s.<\/p>\n<pre># ssh-copy-id tecmint@192.168.0.112\r\n# ssh-copy-id tecmint@192.168.0.113\r\n# ssh-copy-id tecmint@192.168.0.114\r\n<\/pre>\n<div id=\"attachment_11088\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Copy-SSH-Key1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-11088\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Copy-SSH-Key1-620x308.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Copy-SSH-Key1-620x308.png 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Copy-SSH-Key1.png 655w\" alt=\"Copy SSH Key Remote Server\" width=\"620\" height=\"308\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Copy SSH Key Remote Server<\/p>\n<\/div>\n<div id=\"attachment_11089\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Copy-SSH-Keys1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-11089\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Copy-SSH-Keys1-620x301.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Copy-SSH-Keys1-620x301.png 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Copy-SSH-Keys1.png 654w\" alt=\"Copy SSH Key Second Remote Host\" width=\"620\" height=\"301\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Copy SSH Key Second Remote Host<\/p>\n<\/div>\n<p><strong>6.<\/strong>\u00a0After copying all SSH Keys to remote host, now perform a ssh key authentication on all remote hosts to check whether authentication working or not.<\/p>\n<pre>$ ssh tecmint@192.168.0.112\r\n$ ssh tecmint@192.168.0.113\r\n$ ssh tecmint@192.168.0.114\r\n<\/pre>\n<div id=\"attachment_11090\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/SSH-Key-Authentication1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-11090\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/SSH-Key-Authentication1.png\" alt=\"SSH Key Authentication\" width=\"430\" height=\"225\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">SSH Key Authentication<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<h3>Step 3: Creating Inventory File for Remote Hosts<\/h3>\n<p><b>Inventory<\/b>\u00a0file, This file hold the host information\u2019s like which host we need to get connect from local to remote. Default inventory file will be under\u00a0<b>\/etc\/ansible\/hosts<\/b>.<\/p>\n<p><strong>7.<\/strong>\u00a0Now let\u2019s add these three hosts to inventory file. Open and edit file using your favourite editor, Here I use vim.<\/p>\n<pre># sudo vim \/etc\/ansible\/hosts\r\n<\/pre>\n<p>Add the following three hosts IP address..<\/p>\n<pre>[web-servers]\r\n192.168.0.112\r\n192.168.0.113\r\n192.168.0.114\r\n<\/pre>\n<p><strong>Note<\/strong>: The \u2018<strong>web-servers<\/strong>\u2018 in the brackets indicates as group names, it is used in classifying systems and deciding which systems you are going to controlling at what times and for what reason.<\/p>\n<div id=\"attachment_11091\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Create-Ansible-Inventory-File1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-11091\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Create-Ansible-Inventory-File1.png\" alt=\"Create Ansible Inventory File\" width=\"422\" height=\"122\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Create Ansible Inventory File<\/p>\n<p><strong>8.<\/strong>\u00a0Now time to check our all 3 server by just doing a ping from my localhost. To perform the action we need to use the command \u2018<b>ansible<\/b>\u2018 with options \u2018<strong>-m<\/strong>\u2018 (module) and \u2018<strong>-all<\/strong>\u2018 (group of servers).<\/p>\n<pre># ansible -m ping web-servers\r\n\r\nOR\r\n\r\n# ansible -m ping -all\r\n\r\n<\/pre>\n<div id=\"attachment_11092\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Ping-Remote-Hosts1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-11092\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Ping-Remote-Hosts1.png\" alt=\"Ping Remote Hosts\" width=\"422\" height=\"346\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Ping Remote Hosts<\/p>\n<\/div>\n<p>In the above example, we\u2019ve used ping module with Ansible command to ping all remote hosts at ones, the same way there are various modules can be used with Ansible, you can find available modules from ansible Official site\u00a0<a href=\"https:\/\/docs.ansible.com\/list_of_all_modules.html\" target=\"_blank\" rel=\"nofollow noopener\">here<\/a>.<\/p>\n<p><strong>9.<\/strong>\u00a0Now, here we are using another module called \u2018<strong>command<\/strong>\u2018, which is used to execute list of commands (like, df, free, uptim, etc.) on all selected remote hosts at one go, for example watch out few examples shown below.<\/p>\n<p><strong>a.<\/strong>\u00a0To check the partitions on all remote hosts<\/p>\n<pre># ansible -m command -a \"df -h\" web-servers\r\n<\/pre>\n<div id=\"attachment_11093\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Check-Disk-Space-on-all-Hosts1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-11093\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Check-Disk-Space-on-all-Hosts1.png\" alt=\"Check Disk Space on all Hosts\" width=\"550\" height=\"390\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Check Disk Space on all Hosts<\/p>\n<\/div>\n<p><strong>b.<\/strong>\u00a0Check memory usage on all remote hosts.<\/p>\n<pre># ansible -m command -a \"free -mt\" web-servers\r\n<\/pre>\n<div id=\"attachment_11094\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Check-Memory-on-all-Hosts1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-11094\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Check-Memory-on-all-Hosts1-620x429.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Check-Memory-on-all-Hosts1-620x429.png 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Check-Memory-on-all-Hosts1.png 639w\" alt=\"Check Memory on all Hosts\" width=\"620\" height=\"429\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Check Memory on all Hosts<\/p>\n<\/div>\n<p><strong>c.<\/strong>\u00a0Checking Uptime for all 3 servers.<\/p>\n<pre># ansible -m command -a \"uptime\" web-servers\r\n<\/pre>\n<div id=\"attachment_11095\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Check-uptime-on-all-Hosts1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-11095\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Check-uptime-on-all-Hosts1.png\" alt=\"Check uptime on all Hosts\" width=\"553\" height=\"238\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Check uptime on all Hosts<\/p>\n<\/div>\n<p><strong>d.<\/strong>\u00a0Check for hostname and Architecture.<\/p>\n<pre># ansible -m command -a \"arch\" web-servers\r\n# ansible -m shell -a \"hostname\" web-servers\r\n<\/pre>\n<div id=\"attachment_11096\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Check-hostname-on-all-Hosts1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-11096\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Check-hostname-on-all-Hosts1.png\" alt=\"Check hostname on all Hosts\" width=\"531\" height=\"430\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Check hostname on all Hosts<\/p>\n<\/div>\n<p><strong>e.<\/strong>\u00a0If we need the output to any file we can redirect as below.<\/p>\n<pre># ansible -m command -a \"df -h\" web-servers &gt; \/tmp\/df_outpur.txt\r\n<\/pre>\n<div id=\"attachment_11097\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Redirect-Output-to-File1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-11097\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Redirect-Output-to-File1-620x408.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Redirect-Output-to-File1-620x408.png 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/01\/Redirect-Output-to-File1.png 653w\" alt=\"Redirect Output to File\" width=\"620\" height=\"408\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Redirect Output to File<\/p>\n<\/div>\n<p>Like this way, we can run many shell commands using ansible as what we have run the above steps.<\/p>\n<h3>Conclusion<\/h3>\n<p>Okay, We can see how to in next article.<\/p>\n<p>Ansible is a Powerful IT automation tool which is must every sysadmins for deploying applications and managing server\u2019s at one go. Among any other automation tool such as puppet, Capistrano, salt, Ansible is quit very interesting and very easy to setup for production environment. Capistrano oh no i feel headache please leave me alone :p this what i used to say.<\/p>\n<p>Ansible use only SSH as there agent. We don\u2019t have to install and run any agent in the remote servers. Hope this article will be interesting one for you too. In our next article, I will show you how to setup the directory structure for Ansible deployment and creating playbooks and working with it.<\/p>\n<p>Till then keep on tracking us to get updated articles and don\u2019t forget to tell us your opinions on the Ansible and also tell us do you use any other automation tool which is more powerful than Ansible\u2026.<\/p>\n<h3>Reference Links<\/h3>\n<p><a href=\"https:\/\/www.ansible.com\/get-started\" target=\"_blank\" rel=\"noopener\">http:\/\/www.ansible.com\/get-started<\/a><br \/>\n<a href=\"https:\/\/docs.ansible.com\/\" target=\"_blank\" rel=\"noopener\">http:\/\/docs.ansible.com\/<\/a><\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<p>In the\u00a0<a href=\"https:\/\/www.tecmint.com\/install-and-configure-ansible-automation-tool-in-linux\/\" target=\"_blank\" rel=\"noopener\">previous article of this Ansible series<\/a>, we explained that Ansible is an agent-less tool that allows you to quickly and efficiently manage multiple machines (also known as nodes \u2013 and perform deployments to them as well) from a single system.<\/p>\n<div id=\"attachment_16234\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Ansible-Automate-Linux-Tasks.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16234\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Ansible-Automate-Linux-Tasks.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Ansible-Automate-Linux-Tasks.png 720w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Ansible-Automate-Linux-Tasks-620x297.png 620w\" alt=\"Use Ansible Playbooks to Automate Complex Tasks on Linux\" width=\"620\" height=\"297\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Use Ansible Playbooks to Automate Complex Tasks on Linux \u2013 Part 2<\/p>\n<\/div>\n<p>After installing the software in the controller machine, creating the\u00a0<a href=\"https:\/\/www.tecmint.com\/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps\/\" target=\"_blank\" rel=\"noopener\">keys for passwordless login<\/a>\u00a0and copying them to the nodes, it\u2019s time to learn how to optimize the process of managing such remote systems using\u00a0<strong>Ansible<\/strong>.<\/p>\n<h4>Ansible Testing Environment<\/h4>\n<p>Throughout this article, as well as the next one, we will use the following test environment. All hosts are\u00a0<strong>CentOS 7<\/strong>\u00a0boxes:<\/p>\n<pre>Controller machine (where Ansible is installed): <strong>192.168.0.19<\/strong>\r\nNode1: <strong>192.168.0.29<\/strong>\r\nNode2: <strong>192.168.0.30<\/strong>\r\n<\/pre>\n<p>In addition, please note that both nodes have been added in the webservers section of the local\u00a0<strong>\/etc\/ansible\/hosts<\/strong>\u00a0file:<\/p>\n<div id=\"attachment_16085\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Ansible-Host-File.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-16085\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Ansible-Host-File.png\" alt=\"Ansible Host File\" width=\"536\" height=\"76\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Ansible Host File<\/p>\n<p>That said, let\u2019s get started with the topic at hand.<\/p>\n<h3>Introducing Ansible Playbooks<\/h3>\n<p>As described in the previous guide, you can use the\u00a0<strong>ansible<\/strong>\u00a0utility to run commands in remote nodes as follows:<\/p>\n<pre># ansible -a \"\/bin\/hostnamectl --static\" webservers\r\n<\/pre>\n<div id=\"attachment_16086\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Ansible-Run-Commands-on-Remote-Linux.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-16086\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Ansible-Run-Commands-on-Remote-Linux.png\" alt=\"Ansible: Run Commands on Remote Linux\" width=\"547\" height=\"148\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Ansible: Run Commands on Remote Linux<\/p>\n<\/div>\n<p>In the example above, we ran\u00a0<code>hostnamectl --static<\/code>\u00a0on\u00a0<strong>node1<\/strong>\u00a0and\u00a0<strong>node2<\/strong>. It doesn\u2019t take long for one to realize that this method of running tasks on remote computers works fine for short commands but can quickly become burdensome or messy for more complex tasks that require further well-structured configuration parameters or interactions with other services<\/p>\n<p>For example, setting up and configuring\u00a0<strong>WordPress<\/strong>\u00a0on multiple hosts \u2013 which we will cover in the next article of this series). This is where\u00a0<strong>Playbooks<\/strong>\u00a0come into scene.<\/p>\n<p>Simply put,\u00a0<strong>Playbooks<\/strong>\u00a0are plain text files written in the\u00a0<strong>YAML<\/strong>\u00a0format, and contain a list with items with one or more key\/value pairs (also known as a \u201c<strong>hash<\/strong>\u201d or a \u201c<strong>dictionary<\/strong>\u201d).<\/p>\n<p>Inside each Playbook you will find one or more group of hosts (each one of these groups is also called a\u00a0<strong>play<\/strong>) where the desired tasks are to be performed.<\/p>\n<p>An example from the official docs will help us to illustrate:<\/p>\n<p><strong>1.<\/strong>\u00a0<strong>hosts<\/strong>: this is a list of machines (as per\u00a0<strong>\/etc\/ansible\/hosts<\/strong>) where the following tasks will be performed.<\/p>\n<p><strong>2.<\/strong>\u00a0<strong>remote_user<\/strong>: remote account that will be used to perform the tasks.<\/p>\n<p><strong>3.<\/strong>\u00a0<strong>vars<\/strong>: variables used to modify the behavior of the remote system(s).<\/p>\n<p><strong>4.<\/strong>\u00a0tasks are executed in order, one at a time, against all machines that match hosts. Within a play, all hosts are going to get the same task directives.<\/p>\n<p>If you need to execute a different set of associated tasks for a specific host, create another play in the current\u00a0<strong>Playbook<\/strong>\u00a0(in other words, the purpose of a play is to map a specific selection of hosts to well-defined tasks).<\/p>\n<p>In that case, start a new play by adding the hosts directive at the bottom and starting over:<\/p>\n<pre>---\r\n- hosts: webservers\r\n  remote_user: root\r\n  vars:\r\n    variable1: value1\r\n    variable2: value2\r\n  remote_user: root\r\n  tasks:\r\n  - name: description for task1\r\n    task1: parameter1=value_for_parameter1 parameter2=value_for_parameter2\r\n  - name: description for task1\r\n    task2: parameter1=value_for_parameter1 parameter2=value_for_parameter2\r\n  handlers:\r\n    - name: description for handler 1\r\n      service: name=name_of_service state=service_status\r\n- hosts: dbservers\r\n  remote_user: root\r\n  vars:\r\n    variable1: value1\r\n    variable2: value2\r\n\u2026\r\n<\/pre>\n<p><strong>5.<\/strong>\u00a0handlers are actions that are triggered at the end of the tasks section in each play, and are mostly used to restart services or trigger reboots in the remote systems.<\/p>\n<pre># mkdir \/etc\/ansible\/playbooks\r\n<\/pre>\n<p>And a file named\u00a0<strong>apache.yml<\/strong>\u00a0inside of there with the following contents:<\/p>\n<pre>---\r\n- hosts: webservers\r\n  vars:\r\n    http_port: 80\r\n    max_clients: 200\r\n  remote_user: root\r\n  tasks:\r\n  - name: ensure apache is at the latest version\r\n    yum: pkg=httpd state=latest\r\n  - name: replace default index.html file\r\n    copy: src=\/static_files\/index.html dest=\/var\/www\/html\/ mode=0644\r\n    notify:\r\n    - restart apache\r\n  - name: ensure apache is running (and enable it at boot)\r\n    service: name=httpd state=started enabled=yes\r\n  handlers:\r\n    - name: restart apache\r\n      service: name=httpd state=restarted\r\n<\/pre>\n<p>Second, create a directory \/static_files:<\/p>\n<pre># mkdir \/static_files\r\n<\/pre>\n<p>where you will store the custom\u00a0<strong>index.html<\/strong>\u00a0file:<\/p>\n<pre>&lt;!DOCTYPE html&gt;\r\n &lt;html lang=\"en\"&gt;\r\n &lt;head&gt;\r\n &lt;meta charset=\"utf-8\"\/&gt;\r\n &lt;\/script&gt;\r\n &lt;\/head&gt;\r\n &lt;body&gt;\r\n &lt;h1&gt;Apache was started in this host via Ansible&lt;\/h1&gt;&lt;br&gt;\r\n&lt;h2&gt;Brought to you by Tecmint.com&lt;\/h2&gt;\r\n &lt;\/body&gt;\r\n &lt;\/html&gt;\r\n<\/pre>\n<p>That said, now it\u2019s time to use this playbook to perform the tasks mentioned earlier. You will note that Ansible will go through each task by host, one at a time, and will report on the status of such tasks:<\/p>\n<pre># ansible-playbook \/etc\/ansible\/playbooks\/apache.yml\r\n<\/pre>\n<div id=\"attachment_16088\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Linux-Automate-Tasks-with-Ansible.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16088\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Linux-Automate-Tasks-with-Ansible-572x450.png\" sizes=\"auto, (max-width: 572px) 100vw, 572px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Linux-Automate-Tasks-with-Ansible-572x450.png 572w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Linux-Automate-Tasks-with-Ansible.png 631w\" alt=\"Ansible: Automate Tasks in Linux\" width=\"572\" height=\"450\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Ansible: Automate Tasks in Linux<\/p>\n<\/div>\n<p>Now let\u2019s see what happens when we open a browser and point it to\u00a0<strong>192.168.0.29<\/strong>\u00a0and\u00a0<strong>192.168.0.30<\/strong>:<\/p>\n<div id=\"attachment_16089\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Confirm-Ansible-Automated-Tasks.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16089\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Confirm-Ansible-Automated-Tasks-620x359.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Confirm-Ansible-Automated-Tasks-620x359.png 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Confirm-Ansible-Automated-Tasks.png 641w\" alt=\"Ansible: Confirm Automated Tasks\" width=\"620\" height=\"359\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Ansible: Confirm Automated Tasks<\/p>\n<\/div>\n<p>Let\u2019s go one step further and manually stop and disable Apache on\u00a0<strong>node1<\/strong>\u00a0and\u00a0<strong>node2<\/strong>:<\/p>\n<pre># systemctl stop httpd\r\n# systemctl disable httpd\r\n# systemctl is-active httpd\r\n# systemctl is-enabled httpd\r\n<\/pre>\n<div id=\"attachment_16090\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Stop-Apache-Service.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-16090\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Stop-Apache-Service.png\" alt=\"Stop and Disable Apache Service\" width=\"564\" height=\"275\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Stop and Disable Apache Service<\/p>\n<\/div>\n<p>Then run again,<\/p>\n<pre># ansible-playbook \/etc\/ansible\/playbooks\/apache.yml\r\n<\/pre>\n<p>This time, the task reports that the Apache web server was started and enabled on each host:<\/p>\n<div id=\"attachment_16091\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Confirm-Ansible-Tasks.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16091\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Confirm-Ansible-Tasks-620x410.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Confirm-Ansible-Tasks-620x410.png 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Confirm-Ansible-Tasks.png 641w\" alt=\"Ansible: Start Web Server \" width=\"620\" height=\"410\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Ansible: Start Web Server<\/p>\n<\/div>\n<p>Please consider the above example as a glimpse of the power of Ansible. While these are relatively easy tasks when performed on a small number of servers, it can become very tedious and time-consuming if you need to do the same in several (perhaps hundreds) of machines.<\/p>\n<h3>Summary<\/h3>\n<p>In this article we have described how to run commands and execute complex tasks on several remote hosts simultaneously using Ansible. The\u00a0<a href=\"https:\/\/docs.ansible.com\/ansible\/index.html\" target=\"_blank\" rel=\"noopener\">official documentation<\/a>\u00a0and the\u00a0<a href=\"https:\/\/github.com\/ansible\/ansible\" target=\"_blank\" rel=\"noopener\">GitHub repository<\/a>\u00a0provide a lot of examples and guides on how to use Ansible to achieve almost any imaginable task.<\/p>\n<p>As you start learning how to\u00a0<a href=\"https:\/\/www.tecmint.com\/using-shell-script-to-automate-linux-system-maintenance-tasks\/\" target=\"_blank\" rel=\"noopener\">automate tasks on remote Linux hosts<\/a>\u00a0using Ansible, we would like to hear your thoughts. Questions, comments, and suggestions are also always welcome, so feel free to contact us using the form below any time.<\/p>\n<p>In the previous two articles of this\u00a0<strong>Ansible<\/strong>\u00a0series, we explained how to install and configure\u00a0<strong>Ansible<\/strong>\u00a0to run commands and perform complex tasks in several remote servers simultaneously.<\/p>\n<div id=\"attachment_16227\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Automate-Multiple-WordPress-Installations-using-Ansible1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16227\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Automate-Multiple-WordPress-Installations-using-Ansible1.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Automate-Multiple-WordPress-Installations-using-Ansible1.png 720w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Automate-Multiple-WordPress-Installations-using-Ansible1-620x297.png 620w\" alt=\"Automate Multiple WordPress Installations using Ansible\" width=\"620\" height=\"297\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Automate Multiple WordPress Installations using Ansible \u2013 Part 3<\/p>\n<\/div>\n<p>In the current tutorial we will explain how to set up\u00a0<strong>WordPress<\/strong>\u00a0in the same remote servers:<\/p>\n<pre><strong>node1: 192.168.0.29<\/strong>\r\n<strong>node2: 192.168.0.30<\/strong>\r\n<\/pre>\n<p>where we installed, enabled, and started Apache (you probably know by now why we chose to work with a web server as an initial example in the last tutorial).<\/p>\n<p>I highly encourage you to read\u00a0<strong>Part 1<\/strong>\u00a0and\u00a0<strong>Part 2<\/strong>\u00a0before proceeding further in order to make sure you\u2019re familiar with the concepts associated with Ansible.<\/p>\n<p><i class=\"fa fa-check\"><\/i>\u00a0<a href=\"https:\/\/www.tecmint.com\/install-and-configure-ansible-automation-tool-in-linux\/\" target=\"_blank\" rel=\"noopener\">How to Install and Configure \u2018Ansible\u2019 Automation Tool for IT Management \u2013 Part 1<\/a><\/p>\n<p><i class=\"fa fa-check\"><\/i>\u00a0<a href=\"https:\/\/www.tecmint.com\/use-anisble-playbooks-to-automate-complex-tasks-on-multiple-linux-servers\/\" target=\"_blank\" rel=\"noopener\">How to Use Anisble Playbooks to Automate Complex Tasks on Multiple Remote Servers \u2013 Part 2<\/a><\/p>\n<h3>Step 1: Introducing Ansible Roles<\/h3>\n<p>As you start adding more and more tasks to plays, your\u00a0<strong>Playbooks<\/strong>\u00a0can become increasingly difficult to handle. For that reason, the recommended approach in those situations (actually, in all cases) is to use a directory structure that contains the directives for each group of tasks in distinct files.<\/p>\n<p>This approach allows us to re-use these configuration files in separate projects further down the road. Each of these files define what is called in the\u00a0<strong>Ansible ecosystem<\/strong>\u00a0a role.<\/p>\n<p>In our case, we will create two roles. One of them (called\u00a0<strong>wp-dependencies<\/strong>) will be used to install the WordPress dependencies (<strong>PHP<\/strong>\u00a0and\u00a0<strong>MariaDB<\/strong>\u00a0\u2013 no need to install\u00a0<strong>Apache<\/strong>\u00a0as it\u2019s already installed).<\/p>\n<p>The other role (named\u00a0<strong>wp-install-config<\/strong>) will include all the necessary tasks associated with the installation and configuration of WordPress.<\/p>\n<h3>Step 2: Creating Ansible Roles<\/h3>\n<p>Ansible comes with an utility called\u00a0<strong>ansible-galaxy<\/strong>\u00a0that will help us to create the directory structure for our roles. We will do this in\u00a0<strong>\/etc\/ansible\/playbooks<\/strong>\u00a0(which we created in\u00a0<strong>Part 2<\/strong>) but in theory you can set it up in another directory if you want.<\/p>\n<pre># cd \/etc\/ansible\/playbooks\r\n# ansible-galaxy init wp-dependencies\r\n# ansible-galaxy init wp-install-config\r\n<\/pre>\n<div id=\"attachment_16142\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Create-Ansible-WordPress-Directory-Structure.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-16142\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Create-Ansible-WordPress-Directory-Structure.png\" alt=\"Ansible: Create WordPress Roles\" width=\"513\" height=\"91\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Ansible: Create WordPress Roles<\/p>\n<\/div>\n<p>Next confirms the newly created roles.<\/p>\n<pre># ls -R \/etc\/ansible\/playbooks\r\n<\/pre>\n<div id=\"attachment_16143\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Verify-Ansible-WordPress-Directories.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-16143\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Verify-Ansible-WordPress-Directories.png\" alt=\"Ansible: Confirm WordPress Directory Structure\" width=\"536\" height=\"319\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Ansible: Confirm WordPress Directory Structure<\/p>\n<\/div>\n<p>In the above image we can see that\u00a0<strong>ansible-galaxy<\/strong>\u00a0created two directories with the same name as our roles, and other subdirectories (<strong>defaults<\/strong>,\u00a0<strong>files<\/strong>,\u00a0<strong>handlers<\/strong>,\u00a0<strong>meta<\/strong>,\u00a0<strong>tasks<\/strong>,\u00a0<strong>templates<\/strong>, and\u00a0<strong>vars<\/strong>) and a\u00a0<strong>README.md<\/strong>\u00a0file inside each of them.<\/p>\n<p>In addition, a YAML file named\u00a0<strong>main.yml<\/strong>\u00a0was created inside all of the directories listed earlier, with the exception of files and templates.<\/p>\n<p>We will begin by editing the following configuration files as indicated:<\/p>\n<p><strong>1.<\/strong>\u00a0<strong>\/etc\/ansible\/playbooks\/wp-dependencies\/tasks\/main.yml<\/strong>. Note that we are including\u00a0<strong>httpd<\/strong>\u00a0in case you have not followed along with the previous tutorials of this series.<\/p>\n<div class=\"code-label\" title=\"Ansible WordPress Directory Configuration\">main.yml<\/div>\n<pre>---\r\n# tasks file for wp-dependencies\r\n- name: Update packages (this is equivalent to yum update -y)\r\n  yum: name=* state=latest\r\n\r\n- name: Install dependencies for WordPress\r\n  yum: name={{ item }} state=present\r\n  with_items:\r\n        - httpd\r\n        - mariadb-server \r\n        - mariadb\r\n        - php \r\n        - php-mysql\r\n        - MySQL-python\r\n\r\n- name: Ensure MariaDB is running (and enable it at boot)\r\n  service: name=mariadb state=started enabled=yes\r\n\r\n- name: Copy ~\/.my.cnf to nodes\r\n  copy: src=\/root\/.my.cnf dest=\/root\/.my.cnf\r\n\r\n- name: Create MariaDB database\r\n  mysql_db: name={{ wp_mysql_db }} state=present\r\n\r\n- name: Create MariaDB username and password\r\n  mysql_user:\r\n        login_user=root\r\n        login_password=YourMariaDBRootPasswordHere\r\n        name={{ wp_mysql_user }}\r\n        password={{ wp_mysql_password }}\r\n        priv=*.*:ALL\r\n<\/pre>\n<p><strong>2.<\/strong>\u00a0<strong>\/etc\/ansible\/playbooks\/wp-dependencies\/defaults\/main.yml<\/strong><\/p>\n<div class=\"code-label\" title=\"Ansible WordPress Database Configuration\">main.yml<\/div>\n<pre>---\r\n# defaults file for wp-dependencies\r\n  wp_mysql_db: MyWP\r\n  wp_mysql_user: wpUser\r\n  wp_mysql_password: wpP4ss\r\n<\/pre>\n<p><strong>3.<\/strong>\u00a0<strong>\/etc\/ansible\/playbooks\/wp-install-config\/tasks\/main.yml<\/strong>:<\/p>\n<div class=\"code-label\" title=\"Ansible WordPress Installation and Configuration\">main.yml<\/div>\n<pre>---\r\n# tasks file for wp-install-config\r\n- name: Create directory to download WordPress\r\n  command: mkdir -p \/opt\/source\/wordpress\r\n\r\n- name: Download WordPress\r\n  get_url: url=https:\/\/www.wordpress.org\/latest.tar.gz dest=\/opt\/source\/wordpress\/wordpress.tar.gz validate_certs=no\r\n\r\n- name: Extract WordPress\r\n  command: \"tar xzf \/opt\/source\/wordpress\/wordpress.tar.gz -C \/var\/www\/html --strip-components 1\"\r\n\r\n- name: Send config file\r\n  copy: src=\/root\/wp-config-sample.php dest=\/var\/www\/html\/wp-config.php mode=0644\r\n<\/pre>\n<p><strong>4.<\/strong>\u00a0<strong>wp-config-sample.php<\/strong>\u00a0(provided in this\u00a0<a href=\"https:\/\/pastebin.com\/LX77e2W8\" target=\"_blank\" rel=\"nofollow noopener\">Pastebin<\/a>) as follows and save it to your Ansible controller machine (as you can see in the last copy directive above, I downloaded it to the home directory of the superuser (<strong>\/root\/wp-config-sample.php<\/strong>).<\/p>\n<p><strong>Important<\/strong>: Please note that the value for variables\u00a0<strong>DB_NAME<\/strong>,\u00a0<strong>DB_USER<\/strong>, and\u00a0<strong>DB_PASSWORD<\/strong>\u00a0are the same as in\u00a0<strong>\/etc\/ansible\/playbooks\/wp-dependencies\/defaults\/main.yml<\/strong>:<\/p>\n<div class=\"code-label\" title=\"Ansible WordPress Configuration\">wp-config-sample.php<\/div>\n<pre>\u2026\r\n\/** The name of the database for WordPress *\/\r\ndefine('DB_NAME', 'MyWP');\r\n\r\n\/** MySQL database username *\/\r\ndefine('DB_USER', 'wpUser');\r\n\r\n\/** MySQL database password *\/\r\ndefine('DB_PASSWORD', 'wpP4ss');\r\n\u2026\r\n<\/pre>\n<p><strong>5.<\/strong>\u00a0For new database server installations where the root password is empty, such as in this case, unfortunately we need to setup the password for user root individually in every machine through\u00a0<strong>mysql_secure_installation<\/strong>.<\/p>\n<p>As far as I know, there is no available workaround that will allow you to set up the root password via\u00a0<strong>Ansible<\/strong>\u00a0in the same step where you create the administrative database account for WordPress.<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<p>Make sure you use the same password in all hosts, then copy the credentials in\u00a0<strong>\/root\/.my.cnf<\/strong>\u00a0(the actual location may differ in your case, but in all instances it needs to match the value of the\u00a0<strong>src<\/strong>\u00a0parameter for the task\u00a0<strong>Copy ~\/.my.cnf<\/strong>\u00a0to nodes in\u00a0<strong>\/etc\/ansible\/playbooks\/wp-dependencies\/tasks\/main.yml<\/strong>).<\/p>\n<p>In that file (see above) we\u2019ve assumed that the password for root is\u00a0<strong>YourMariaDBRootPassword<\/strong>.<\/p>\n<div id=\"attachment_16145\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Ansible-Database-Password.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-16145\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Ansible-Database-Password.png\" alt=\"Ansible Database Password\" width=\"501\" height=\"161\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Ansible Database Password<\/p>\n<\/div>\n<p><strong>6.<\/strong>\u00a0Next, our playbook (<strong>\/etc\/ansible\/playbooks\/playbook.yml<\/strong>) will look much more organized and simple when compared to the previous tutorial:<\/p>\n<pre># cat playbook.yml\r\n<\/pre>\n<div id=\"attachment_16146\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Ansible-WordPress-Playbooks.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16146\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Ansible-WordPress-Playbooks-620x198.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Ansible-WordPress-Playbooks-620x198.png 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Ansible-WordPress-Playbooks.png 642w\" alt=\"Ansible WordPress Playbooks\" width=\"620\" height=\"198\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Ansible WordPress Playbooks<\/p>\n<\/div>\n<pre>- hosts: webservers\r\n  roles:\r\n        - wp-dependencies\r\n        - wp-install-config\r\n<\/pre>\n<p>Finally, it\u2019s time to run these tasks by invoking our playbook:<\/p>\n<pre># ansible-playbook playbook.yml\r\n<\/pre>\n<p>Now let\u2019s check if we can access the WordPress Admin page using the IP addresses of\u00a0<strong>node1 192.168.0.29<\/strong>\u00a0and\u00a0<strong>node2 192.168.0.30<\/strong>:<\/p>\n<div id=\"attachment_16147\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Ansible-WordPress-Installation.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16147\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Ansible-WordPress-Installation-462x450.png\" sizes=\"auto, (max-width: 462px) 100vw, 462px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Ansible-WordPress-Installation-462x450.png 462w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Ansible-WordPress-Installation.png 494w\" alt=\"Ansible WordPress Installation\" width=\"462\" height=\"450\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Ansible WordPress Installation<\/p>\n<\/div>\n<p>You can view the last two steps in the following screencast:<\/p>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/wA6kPTZGo4c\" width=\"780\" height=\"439\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>As you can see, you can set up multiple WordPress installations with little to no effort using\u00a0<strong>Ansible<\/strong>. Then you can use the respective\u00a0<strong>Admin<\/strong>\u00a0user interface to configure each site separately.<\/p>\n<h3>Final considerations<\/h3>\n<p>If you are using another distribution to deploy\u00a0<strong>WordPress<\/strong>, the packages name may vary, but it comes down to installing the Apache web server, the MariaDB database server, and the Python MySQL module. If that is the case, use your distribution\u2019s software management system to search for the exact package name that you need to install.<\/p>\n<h3>Summary<\/h3>\n<p>In this series we have explained how to use\u00a0<strong>Ansible<\/strong>\u00a0to\u00a0<a href=\"https:\/\/www.tecmint.com\/use-anisble-playbooks-to-automate-complex-tasks-on-multiple-linux-servers\/\" target=\"_blank\" rel=\"noopener\">run commands and execute complex tasks<\/a>\u00a0in several Linux machines simultaneously.<\/p>\n<p>One of such examples is setting up\u00a0<strong>WordPress<\/strong>, as we have discussed in this guide. Whether you are a system administrator or a blogger, I hope you have found the concepts and examples in this tutorial useful.<\/p>\n<p>Best of luck and do not hesitate to drop us a line if you need help or have any comments or suggestions!<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/install-and-configure-ansible-automation-tool-in-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ansible\u00a0is an open source, powerful automation software for configuring, managing and deploying software applications on the nodes without any downtime just by using SSH. Today, most of the IT Automation tools runs as a agent in remote host, but ansible just need a SSH connection and Python (2.4 or later) to be installed on the &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/17\/how-to-install-and-configure-ansible-automation-tool-for-it-management\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Install and Configure \u2018Ansible\u2019 Automation Tool for IT Management&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11794","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11794","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=11794"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11794\/revisions"}],"predecessor-version":[{"id":11795,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11794\/revisions\/11795"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=11794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=11794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=11794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}