{"id":6096,"date":"2024-06-10T11:09:57","date_gmt":"2024-06-10T11:09:57","guid":{"rendered":"https:\/\/natsav.com\/blog\/?p=6096"},"modified":"2024-07-15T12:55:56","modified_gmt":"2024-07-15T12:55:56","slug":"automate-cloud-server-provisioning-using-ansible","status":"publish","type":"post","link":"https:\/\/natsav.com\/blog\/automate-cloud-server-provisioning-using-ansible\/","title":{"rendered":"How to automate Cloud Server provisioning using Ansible"},"content":{"rendered":"\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_68_1 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/natsav.com\/blog\/automate-cloud-server-provisioning-using-ansible\/#Introduction_How_to_automate_Cloud_Server_provisioning_using_Ansible\" title=\"Introduction (How to automate Cloud Server provisioning using Ansible)\">Introduction (How to automate Cloud Server provisioning using Ansible)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/natsav.com\/blog\/automate-cloud-server-provisioning-using-ansible\/#Mechanisms_of_Ansible_Automate_Cloud_Server_provisioning_using_Ansible\" title=\"Mechanisms of Ansible (Automate Cloud Server provisioning using Ansible)\">Mechanisms of Ansible (Automate Cloud Server provisioning using Ansible)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/natsav.com\/blog\/automate-cloud-server-provisioning-using-ansible\/#Understanding_Playbooks\" title=\"Understanding Playbooks\">Understanding Playbooks<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/natsav.com\/blog\/automate-cloud-server-provisioning-using-ansible\/#Installing_Ansible_Automate_Cloud_Server_provisioning_using_Ansible\" title=\"Installing Ansible (Automate Cloud Server provisioning using Ansible)\">Installing Ansible (Automate Cloud Server provisioning using Ansible)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/natsav.com\/blog\/automate-cloud-server-provisioning-using-ansible\/#Creating_Your_First_Playbook\" title=\"Creating Your First Playbook\">Creating Your First Playbook<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/natsav.com\/blog\/automate-cloud-server-provisioning-using-ansible\/#Running_Your_Playbook\" title=\"Running Your Playbook\">Running Your Playbook<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/natsav.com\/blog\/automate-cloud-server-provisioning-using-ansible\/#Integrating_Ansible_with_Terraform\" title=\"Integrating Ansible with Terraform\">Integrating Ansible with Terraform<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/natsav.com\/blog\/automate-cloud-server-provisioning-using-ansible\/#Conclusion\" title=\"Conclusion\">Conclusion<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Introduction_How_to_automate_Cloud_Server_provisioning_using_Ansible\"><\/span>Introduction (How to automate Cloud Server provisioning using Ansible)<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>How to automate Cloud Server provisioning using Ansible, an automation tool sans agents, streamlines the remote provisioning of Cloud Servers with ease. Engineered for multi-tier deployments, Ansible adeptly manages configuration, application deployment, intra-service orchestration, and sundry IT necessities.<\/p>\n\n\n\n<p>Gone are the days of singular system management. Ansible allows you to model your cloud infrastructure by delineating the interrelationships of your systems. Utilizing the trusted SSH protocol, it eliminates the need for agents or additional custom security frameworks.<\/p>\n\n\n\n<p><strong><a href=\"https:\/\/natsav.com\/blog\/how-do-i-install-wordpress-themes-on-localhost-2\/\">How do I Install WordPress Themes on Localhost?<\/a><\/strong><\/p>\n\n\n\n<p>Automate Cloud Server provisioning using Ansible In this guide, we elucidate the fundamentals of Ansible and the initial steps to configure your cloud infrastructure. <\/p>\n\n\n\n<p>Ansible supports numerous operating systems compatible with Python, making the steps in this guide applicable to almost any Linux distribution, with minor adaptations for other operating systems.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Mechanisms_of_Ansible_Automate_Cloud_Server_provisioning_using_Ansible\"><\/span>Mechanisms of Ansible (Automate Cloud Server provisioning using Ansible)<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Ansible&#8217;s primary objective is to install and configure deployment-required software, whether on a single server or a global cluster. It connects to your nodes and deploys succinct programs known as \u201cAnsible modules.\u201d<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"400\" src=\"https:\/\/i0.wp.com\/natsav.com\/blog\/wp-content\/uploads\/2024\/07\/How-to-automate-Cloud-Server-provisioning-using-Ansible-natsav.com_.png?resize=600%2C400&#038;ssl=1\" alt=\"How to automate Cloud Server provisioning using Ansible\" class=\"wp-image-6914\" srcset=\"https:\/\/i0.wp.com\/natsav.com\/blog\/wp-content\/uploads\/2024\/07\/How-to-automate-Cloud-Server-provisioning-using-Ansible-natsav.com_.png?w=600&amp;ssl=1 600w, https:\/\/i0.wp.com\/natsav.com\/blog\/wp-content\/uploads\/2024\/07\/How-to-automate-Cloud-Server-provisioning-using-Ansible-natsav.com_.png?resize=300%2C200&amp;ssl=1 300w, https:\/\/i0.wp.com\/natsav.com\/blog\/wp-content\/uploads\/2024\/07\/How-to-automate-Cloud-Server-provisioning-using-Ansible-natsav.com_.png?resize=150%2C100&amp;ssl=1 150w\" sizes=\"(max-width: 600px) 100vw, 600px\" data-recalc-dims=\"1\" \/><\/figure><\/div>\n\n\n<p>These modules model the desired state of system resources. Ansible maintains a vast library of common modules to manage the majority of popular software provisioning. Users can also craft their own modules, which can reside on any machine. No servers, daemons, or database requirements exist.<\/p>\n\n\n\n<p>Once connected, Ansible executes the modules to achieve the desired state and performs cleanup to remove used modules, leaving a pristine installation.<\/p>\n\n\n\n<p>Getting started with Ansible is straightforward; you only need a terminal program and a text editor. Tools like version control systems can also help manage configuration changes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Understanding_Playbooks\"><\/span>Understanding Playbooks<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Ansible\u2019s modularity is further enhanced by Playbooks, which define tasks and processes in easily readable configuration files using YAML. This simple markup language makes configurations readable and comprehensible, even years later.<\/p>\n\n\n\n<p>Playbooks offer granular control for orchestrating your cloud infrastructure. Each module can target numerous servers, granting you control over the number of machines managed simultaneously. This flexibility makes Ansible particularly compelling.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Installing_Ansible_Automate_Cloud_Server_provisioning_using_Ansible\"><\/span>Installing Ansible (Automate Cloud Server provisioning using Ansible)<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Ansible operates as a simple command-line tool and can be installed on virtually any machine. The most universal installation method uses Pip with Python.<\/p>\n\n\n\n<p>If pip is not present on your system, install it using the commands below. Ensure Python is installed first, likely available via your operating system\u2019s package manager.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>shCopy code<code>sudo apt update<br>sudo apt install python3-pip<br><\/code><\/strong><\/pre>\n\n\n\n<p>Once pip is installed, proceed to install Ansible:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>shCopy code<code>pip3 install ansible<\/code><\/strong><code><br><\/code><\/pre>\n\n\n\n<p>This setup allows Ansible to manage an entire fleet of Cloud Servers from a single control point, without the need for databases or daemons.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_Your_First_Playbook\"><\/span>Creating Your First Playbook<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>With Ansible installed (Automate Cloud Server provisioning using Ansible), you can create your first playbook. Below is an example of a simple LAMP stack installation on an Ubuntu 20.04 Cloud Server. When executed, it performs the following operations:<\/p>\n\n\n\n<p><strong><a href=\"https:\/\/natsav.com\/blog\/what-is-a-managed-vps-in-2024\/\">What is a Managed VPS? Discover Benefits &amp; Features<\/a><\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"755\" height=\"374\" src=\"https:\/\/i0.wp.com\/natsav.com\/blog\/wp-content\/uploads\/2024\/07\/How-to-automate-Cloud-Server-provisioning-using-Ansible-natsav.com1_.png?resize=755%2C374&#038;ssl=1\" alt=\"\" class=\"wp-image-6916\" srcset=\"https:\/\/i0.wp.com\/natsav.com\/blog\/wp-content\/uploads\/2024\/07\/How-to-automate-Cloud-Server-provisioning-using-Ansible-natsav.com1_.png?w=755&amp;ssl=1 755w, https:\/\/i0.wp.com\/natsav.com\/blog\/wp-content\/uploads\/2024\/07\/How-to-automate-Cloud-Server-provisioning-using-Ansible-natsav.com1_.png?resize=300%2C149&amp;ssl=1 300w, https:\/\/i0.wp.com\/natsav.com\/blog\/wp-content\/uploads\/2024\/07\/How-to-automate-Cloud-Server-provisioning-using-Ansible-natsav.com1_.png?resize=150%2C74&amp;ssl=1 150w\" sizes=\"(max-width: 755px) 100vw, 755px\" data-recalc-dims=\"1\" \/><\/figure><\/div>\n\n\n<ol>\n<li>Update and upgrade all system packages.<\/li>\n\n\n\n<li>Install the latest versions of Apache2, MariaDB, PHP, and PHP-MySQL.<\/li>\n\n\n\n<li>Enable and start the Apache2 web server.<\/li>\n\n\n\n<li>Enable and start the MariaDB database service.<\/li>\n\n\n\n<li>Fetch a test PHP index page from a remote source.<\/li>\n\n\n\n<li>Confirm the website&#8217;s accessibility.<\/li>\n<\/ol>\n\n\n\n<p>Create a new file called <code>lamp-stack.yml<\/code> and add the following configuration:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">yamlCopy code<code>---<br>- name: Install LAMP stack on Ubuntu<br>  hosts: webservers<br>  become: yes<br>  tasks:<br>    - name: Update and upgrade apt packages<br>      apt:<br>        update_cache: yes<br>        upgrade: dist<br><br>    - name: Install required software<br>      apt:<br>        name:<br>          - apache2<br>          - mariadb-server<br>          - php<br>          - php-mysql<br>        state: present<br><br>    - name: Enable and start Apache2<br>      service:<br>        name: apache2<br>        state: started<br>        enabled: yes<br><br>    - name: Enable and start MariaDB<br>      service:<br>        name: mariadb<br>        state: started<br>        enabled: yes<br><br>    - name: Fetch test PHP page<br>      get_url:<br>        url: http:\/\/example.com\/index.php<br>        dest: \/var\/www\/html\/index.php<br><br>    - name: Confirm website accessibility<br>      uri:<br>        url: http:\/\/localhost\/index.php<br>        status_code: 200<br><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Running_Your_Playbook\"><\/span>Running Your Playbook<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To provision your servers, you must define the inventory, which Ansible searches for at <code>\/etc\/ansible\/hosts<\/code> by default. For multiple lists, specify the inventory file at the command line with the <code>-i<\/code> option.<\/p>\n\n\n\n<p>Create an inventory file called <code>hosts.yml<\/code> with the following contents:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">yamlCopy code<code>webservers:<br>  hosts:<br>    192.168.1.10:<br>      ansible_user: your_user<br>      ansible_ssh_private_key_file: \/path\/to\/private\/key<br><\/code><\/pre>\n\n\n\n<p>Test the connection with the ping module:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">shCopy code<code>ansible -i hosts.yml webservers -m ping\n<\/code><\/pre>\n\n\n\n<p>If successful, run the playbook:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">shCopy code<code>ansible-playbook -i hosts.yml lamp-stack.yml\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Integrating_Ansible_with_Terraform\"><\/span>Integrating Ansible with Terraform<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Ansible excels at provisioning servers post-deployment, while Terraform, an open-source infrastructure-as-code tool, initiates the deployment. Terraform uses a human-readable language to manage cloud infrastructure, codifying APIs into configuration files. Once deployed, Terraform can call Ansible for provisioning.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"550\" height=\"362\" src=\"https:\/\/i0.wp.com\/natsav.com\/blog\/wp-content\/uploads\/2024\/07\/How-to-automate-Cloud-Server-provisioning-using-Ansible-natsav.com2_.png?resize=550%2C362&#038;ssl=1\" alt=\"\" class=\"wp-image-6918\" srcset=\"https:\/\/i0.wp.com\/natsav.com\/blog\/wp-content\/uploads\/2024\/07\/How-to-automate-Cloud-Server-provisioning-using-Ansible-natsav.com2_.png?w=550&amp;ssl=1 550w, https:\/\/i0.wp.com\/natsav.com\/blog\/wp-content\/uploads\/2024\/07\/How-to-automate-Cloud-Server-provisioning-using-Ansible-natsav.com2_.png?resize=300%2C197&amp;ssl=1 300w, https:\/\/i0.wp.com\/natsav.com\/blog\/wp-content\/uploads\/2024\/07\/How-to-automate-Cloud-Server-provisioning-using-Ansible-natsav.com2_.png?resize=150%2C99&amp;ssl=1 150w\" sizes=\"(max-width: 550px) 100vw, 550px\" data-recalc-dims=\"1\" \/><\/figure><\/div>\n\n\n<p>Use the following code in your Terraform configuration:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">hclCopy code<code>resource \"null_resource\" \"ansible_provision\" {\n  connection {\n    type        = \"ssh\"\n    user        = \"your_user\"\n    private_key = file(\"\/path\/to\/private\/key\")\n    host        = \"${aws_instance.your_instance.public_ip}\"\n  }\n\n  provisioner \"remote-exec\" {\n    inline = [\n      \"echo 'Server is ready for provisioning'\",\n    ]\n  }\n\n  provisioner \"local-exec\" {\n    command = \"ansible-playbook -i ${self.connection.host}, lamp-stack.yml\"\n  }\n}\n<\/code><\/pre>\n\n\n\n<p>Deploy and provision a new Cloud Server from start to finish with a single command, allowing automation to manage your deployment seamlessly.<\/p>\n\n\n\n<p><strong><a href=\"https:\/\/natsav.com\/blog\/how-to-resolve-upload-error-in-cyberpanel\/\" target=\"_blank\" rel=\"noreferrer noopener\">How To Resolve Upload Error In Cyberpanel<\/a><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Automate Cloud Server provisioning using Ansible, Congratulations! You now possess the knowledge to automate your Cloud Server deployment and provisioning with Ansible and Terraform. This combination simplifies managing large cloud infrastructures, saving you time and money.<\/p>\n\n\n\n<p>Explore more playbook examples on Ansible\u2019s GitHub repository, and delve into our tutorial on automating Terraform deployments to further enhance your skills.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction (How to automate Cloud Server provisioning using Ansible) How to automate Cloud Server provisioning using Ansible, an automation tool sans agents, streamlines the remote provisioning of Cloud Servers with ease. Engineered for multi-tier deployments, Ansible adeptly manages configuration, application deployment, intra-service orchestration, and sundry IT necessities. Gone are the days of singular system management. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":6097,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[267],"tags":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/i0.wp.com\/natsav.com\/blog\/wp-content\/uploads\/2024\/04\/Screenshot_9.png?fit=615%2C343&ssl=1","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/natsav.com\/blog\/wp-json\/wp\/v2\/posts\/6096"}],"collection":[{"href":"https:\/\/natsav.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/natsav.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/natsav.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/natsav.com\/blog\/wp-json\/wp\/v2\/comments?post=6096"}],"version-history":[{"count":8,"href":"https:\/\/natsav.com\/blog\/wp-json\/wp\/v2\/posts\/6096\/revisions"}],"predecessor-version":[{"id":6925,"href":"https:\/\/natsav.com\/blog\/wp-json\/wp\/v2\/posts\/6096\/revisions\/6925"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/natsav.com\/blog\/wp-json\/wp\/v2\/media\/6097"}],"wp:attachment":[{"href":"https:\/\/natsav.com\/blog\/wp-json\/wp\/v2\/media?parent=6096"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/natsav.com\/blog\/wp-json\/wp\/v2\/categories?post=6096"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/natsav.com\/blog\/wp-json\/wp\/v2\/tags?post=6096"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}