{"id":1072,"date":"2023-02-21T17:38:36","date_gmt":"2023-02-21T17:38:36","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=1072"},"modified":"2023-02-21T17:38:36","modified_gmt":"2023-02-21T17:38:36","slug":"ansible-playbook-examples","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=1072","title":{"rendered":"Ansible Playbook Examples [Boost Your Automation Skills]"},"content":{"rendered":"<p>In this guide, we will look at different Ansible playbook examples with different modules to boost your automation skills using Ansible.<\/p>\n<p>Ansible is a <a href=\"https:\/\/devopscube.com\/infrastructure-as-code-configuration-management\/\">configuration management tool<\/a> that allows you to automate application configuration on a server.<\/p>\n<h2 id=\"what-is-a-ansible-playbook\">What is a Ansible Playbook?<\/h2>\n<p>A playbook is a set of tasks and configurations that you define in YAML format. Playbooks are designed to be reusable and shareable across teams and organizations, and they allow you to perform a wide range of tasks, such as installing packages, managing services, and configuring configuration files in an idempotent way.<\/p>\n<blockquote><p><strong>Note<\/strong>: For project implementations, you should be use roles based on the requirements. So that it can be re-used in diffrent playbooks.<\/p><\/blockquote>\n<h2 id=\"ansible-playbook-example\">Ansible Playbook Example<\/h2>\n<p>Following is an example of an Ansible playbook.<\/p>\n<pre><code>---\n- name: Install and configure Nginx web server\n  hosts: webservers\n  become: true\n  gather_facts: yes\n\n  tasks:\n  - name: Display OS information\n    debug:\n      var: ansible_distribution\n  \n  - name: Update apt cache\n    apt:\n      update_cache: yes\n\n  - name: Install Nginx web server\n    apt:\n      name: nginx\n      state: latest\n<\/code><\/pre>\n<p>Here is the explanation of the playbook.<\/p>\n<ol>\n<li>The playbook starts by specifying a <strong><code>name<\/code><\/strong><\/li>\n<li><strong>hosts: webservers<\/strong> specified the \u00a0<code>webservers<\/code> label added to the inventory file.<\/li>\n<li><strong>become: true<\/strong> specifies that the playbook should run as sudo.<\/li>\n<li><strong>gather_facts: yes<\/strong> gathers all the node information during the playbook run.<\/li>\n<li>The tasks section contains the tasks to update the apt cache and then install Nginx.<\/li>\n<\/ol>\n<p>If you don&#8217;t have Ansible installed, check out the <a href=\"https:\/\/devopscube.com\/install-configure-ansible-server-nodes\/\" rel=\"noreferrer noopener\">Ansible installation blog<\/a> where all the Ansible configurations and inventory configurations are explained.<\/p>\n<p>Now, let&#8217;s look at different examples that will help you gain knowledge on different module use cases. You can also use these examples in your day-to-day ansible playbook implementation.<\/p>\n<h2 id=\"ansible-playbook-to-check-os-using-fact-variable\">Ansible Playbook To Check OS Using Fact Variable<\/h2>\n<p>Checking OS \u00a0in a playbook with a when clause is necessary to ensure that the playbook is compatible with the operating system and uses a module that is relevant to the operating system. We can do this using Ansible&#8217;s facts which gathers all information about the OS when you run the playbook.<\/p>\n<p>You can check out all the <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/playbook_guide\/playbooks_vars_facts.html?ref=devopscube.com\" rel=\"noreferrer noopener\">available fact variables from here<\/a>.<\/p>\n<p>Here is an example playbook that checks if the OS is Ubuntu or Centos and installs apache based on the platform. We specify <code>gather_facts: yes<\/code> to gather the system facts. Replace <strong><code>hosts<\/code><\/strong> parameter based on your needs.<\/p>\n<pre><code>---\n- name: Check operating system\n  hosts: all\n  gather_facts: yes\n\n  tasks:\n  - name: Display OS information\n    debug:\n      var: ansible_distribution\n\n  - name: Install a package based on OS\n    when: ansible_distribution == 'Ubuntu'\n    apt:\n      name: apache2\n      state: present\n    when: ansible_distribution == 'CentOS'\n    yum:\n      name: httpd\n      state: present\n<\/code><\/pre>\n<h2 id=\"ansible-playbook-to-install-applications-on-ubuntu\">Ansible Playbook To Install Applications on Ubuntu<\/h2>\n<p>The following playbook example installs multiple applications on Ubuntu. It installs Nginx, MySQL Client, and PHP extensions.<\/p>\n<p>Here it is worth noting <strong><code>with_items:<\/code><\/strong> parameter where it loops through multiple packages and applies one by one in the <strong><code>name: \"{{ item }}\"<\/code><\/strong> parameter.<\/p>\n<pre><code>---\n- name: Install Applications on Ubuntu\n  hosts: webservers\n  become: true\n\n  tasks:\n  - name: Install Nginx\n    apt:\n      name: nginx\n      state: present\n\n  - name: Install MySQL client\n    apt:\n      name: mysql-client\n      state: present\n\n  - name: Install PHP and required extensions\n    apt:\n      name: \"{{ item }}\"\n      state: present\n    with_items:\n      - php-fpm\n      - php-mysql\n      - php-curl\n      - php-gd\n<\/code><\/pre>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/ansible-playbook-examples\/\" target=\"_blank\" rel=\"noopener noreferrer\">Ansible Playbook Examples [Boost Your Automation Skills] \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/ansible-playbook-examples\/<\/p>\n","protected":false},"author":1,"featured_media":1073,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1072","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\/1072","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=1072"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/1072\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/1073"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}