{"id":6195,"date":"2025-03-09T11:50:41","date_gmt":"2025-03-09T11:50:41","guid":{"rendered":"https:\/\/linuxbuz.com\/?p=6195"},"modified":"2025-07-11T04:36:54","modified_gmt":"2025-07-11T04:36:54","slug":"ansible-playbooks-examples","status":"publish","type":"post","link":"https:\/\/linuxbuz.com\/devops\/ansible-playbooks-examples","title":{"rendered":"7 Ansible Playbook Examples for Beginners"},"content":{"rendered":"<p style=\"text-align: justify;\"><a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/playbook_guide\/playbooks_intro.html\" target=\"_blank\" rel=\"noopener\"><strong>Ansible Playbooks<\/strong> <\/a>are the heart of Ansible automation. They provide a simple way to define automation workflows using YAML. Playbooks consist of instructions (tasks) that Ansible executes on remote machines. They are easy to read and manage, making them ideal for automating IT processes such as configuration management, software deployment, and task execution.<\/p>\n<p style=\"text-align: justify;\"><em>In this guide, you&#8217;ll learn how to create, manage, and optimize Ansible Playbooks with examples.<\/em><\/p>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#001a66;border-radius:3px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#004d99;color:#FFFFFF;border-top-left-radius:1px;border-top-right-radius:1px\">\ud83d\udca1 Recommended Reading<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:1px;border-bottom-right-radius:1px\">\n\ud83d\udc49 <a href=\"https:\/\/linuxbuz.com\/linuxhowto\/how-to-get-free-cloud-vps-server-step-by-step-guide\" target=\"_blank\" rel=\"noopener\">How to Get a Free Cloud Server for Testing<\/a><br \/>\n<\/div><\/div>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/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:\/\/linuxbuz.com\/devops\/ansible-playbooks-examples\/#What_is_an_Ansible_Playbook\" >What is an Ansible Playbook?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-playbooks-examples\/#Writing_Your_First_Ansible_Playbook\" >Writing Your First Ansible Playbook<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-playbooks-examples\/#Using_Loops_and_Conditional_in_Playbooks\" >Using Loops and Conditional in 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:\/\/linuxbuz.com\/devops\/ansible-playbooks-examples\/#Dynamic_Task_Execution_with_include_tasks\" >Dynamic Task Execution with include_tasks<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-playbooks-examples\/#Static_Task_Inclusion_with_import_tasks\" >Static Task Inclusion with import_tasks<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-playbooks-examples\/#Reusing_Tasks_Across_Playbooks\" >Reusing Tasks Across Playbooks<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-playbooks-examples\/#Using_Variables_and_Facts_in_Playbooks\" >Using Variables and Facts in Playbooks<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-playbooks-examples\/#Ansible_Playbook_Options_with_Examples\" >Ansible Playbook Options with Examples<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-playbooks-examples\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/linuxbuz.com\/devops\/ansible-playbooks-examples\/#FAQs\" >FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n<h2 data-pm-slice=\"1 1 []\"><span class=\"ez-toc-section\" id=\"What_is_an_Ansible_Playbook\"><\/span>What is an Ansible Playbook?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>An <strong>Ansible playbook<\/strong> is a YAML file that defines tasks to run on one or more remote hosts. These tasks are grouped into plays and executed in order.<\/p>\n<p><strong>Key Components of Playbooks:<\/strong><\/p>\n<ul>\n<li style=\"text-align: justify;\"><strong>Hosts:<\/strong> These define the target machines on which the tasks are executed. The hosts field specifies which group or machine to apply the playbook to.<\/li>\n<li style=\"text-align: justify;\"><strong>Tasks:<\/strong> These are the actual steps executed by Ansible, such as installing software or restarting services.<\/li>\n<li style=\"text-align: justify;\"><strong>Handlers:<\/strong> Special tasks triggered when a change occurs, such as <a href=\"https:\/\/linuxbuz.com\/devops\/ansible-systemd-module-examples#Example_3_Restart_a_Service\" target=\"_blank\" rel=\"noopener\"><strong>restarting a service<\/strong><\/a> after a configuration update.<\/li>\n<li style=\"text-align: justify;\"><strong>Variables:<\/strong> Dynamic values used in playbooks to make tasks reusable across multiple environments.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Writing_Your_First_Ansible_Playbook\"><\/span>Writing Your First Ansible Playbook<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Let\u2019s start with a simple playbook to install NGINX on an Ubuntu server.<\/p>\n<p>1. Create an inventory file <strong>inventory.yml:<\/strong><\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>[webservers]\r\nserver1 ansible_host=192.168.1.10 ansible_user=ubuntu ansible_ssh_private_key_file=~\/.ssh\/id_rsa<\/code><\/pre>\n<p>2. Now create a playbook <strong>nginx_install.yml:<\/strong><\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>---\r\n- name: Install NGINX on web server\r\n  hosts: webservers\r\n  become: yes\r\n  tasks:\r\n    - name: Update apt cache\r\n      apt:\r\n        update_cache: yes\r\n\r\n    - name: Install NGINX\r\n      apt:\r\n        name: nginx\r\n        state: present<\/code><\/pre>\n<p>3. Run the playbook:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>ansible-playbook -i inventory.yml nginx_install.yml<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Using_Loops_and_Conditional_in_Playbooks\"><\/span>Using Loops and Conditional in Playbooks<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">You can use <strong>loops<\/strong> to repeat tasks, and <strong>conditionals<\/strong> to execute tasks only when certain conditions are met.<\/p>\n<p style=\"text-align: justify;\">Here is a simple playbook that uses a <strong>loop<\/strong> to install <strong>multiple packages<\/strong> on target servers.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>---\r\n- name: Install multiple packages\r\n  hosts: webservers\r\n  tasks:\r\n    - name: Install necessary packages\r\n      apt:\r\n        name: \"{{ item }}\"\r\n        state: present\r\n      loop:\r\n        - nginx\r\n        - curl\r\n        - git<\/code><\/pre>\n<p>You can also use <a href=\"https:\/\/linuxbuz.com\/devops\/ansible-when-conditionals\" target=\"_blank\" rel=\"noopener\"><strong>when conditional<\/strong><\/a> in playbooks to <a href=\"https:\/\/linuxbuz.com\/devops\/ansible-apt-module-example\" target=\"_blank\" rel=\"noopener\"><strong>install packages<\/strong><\/a> based on <strong>operating systems.<\/strong><\/p>\n<p style=\"text-align: justify;\">The following playbook installs the Nginx package only if the target host runs a Debian-based operating system.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Install NGINX only if the host is Ubuntu\r\n  apt:\r\n    name: nginx\r\n    state: present\r\n  when: ansible_facts['os_family'] == \"Debian\"<\/code><\/pre>\n<h2 data-pm-slice=\"1 1 []\"><span class=\"ez-toc-section\" id=\"Dynamic_Task_Execution_with_include_tasks\"><\/span>Dynamic Task Execution with include_tasks<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\"><a href=\"https:\/\/linuxbuz.com\/devops\/ansible-import-include-playbooks#Example_2_Using_include_tasks\" target=\"_blank\" rel=\"noopener\"><strong>include_tasks<\/strong><\/a> allows you to include a separate task file dynamically. It\u2019s useful when you need flexibility, such as including tasks conditionally or within a <a href=\"https:\/\/linuxbuz.com\/devops\/ansible-loops-example\" target=\"_blank\" rel=\"noopener\"><strong>loop<\/strong><\/a>.<\/p>\n<h5>Example: Include Task Files Conditionally<\/h5>\n<p style=\"text-align: justify;\">Here, we will conditionally <a href=\"https:\/\/linuxbuz.com\/devops\/ansible-import-include-playbooks\" target=\"_blank\" rel=\"noopener\"><strong>include different task<\/strong><\/a> files based on the operating system.<\/p>\n<p><strong>Main Playbook (site.yml):<\/strong><\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>---\r\n- name: Configure Web Servers\r\n  hosts: webservers\r\n  tasks:\r\n    - name: Include tasks for Debian\r\n      include_tasks: debian.yml\r\n      when: ansible_facts['os_family'] == \"Debian\"\r\n\r\n    - name: Include tasks for RedHat\r\n      include_tasks: redhat.yml\r\n      when: ansible_facts['os_family'] == \"RedHat\"<\/code><\/pre>\n<p><strong>Debian Task File (debian.yml):<\/strong><\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>---\r\n- name: Install NGINX on Debian\r\n  apt:\r\n    name: nginx\r\n    state: present<\/code><\/pre>\n<p><strong>RedHat Task File (redhat.yml):<\/strong><\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>---\r\n- name: Install NGINX on RedHat\r\n  yum:\r\n    name: nginx\r\n    state: present<\/code><\/pre>\n<h2 data-pm-slice=\"1 1 []\"><span class=\"ez-toc-section\" id=\"Static_Task_Inclusion_with_import_tasks\"><\/span>Static Task Inclusion with import_tasks<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\"><strong>import_tasks<\/strong> is processed when the playbook starts, so it is static and executed unconditionally. It\u2019s better for simpler scenarios where tasks don\u2019t need to be dynamically included.<\/p>\n<h5>Import Task Files for Standard Tasks<\/h5>\n<p style=\"text-align: justify;\">In this example, we split common tasks into separate files and use <strong>import_tasks<\/strong> to include them.<\/p>\n<p><strong>Main Playbook (site.yml):<\/strong><\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>---\r\n- name: Common Server Configuration\r\n  hosts: all\r\n  tasks:\r\n    - name: Import common tasks\r\n      import_tasks: common_tasks.yml<\/code><\/pre>\n<p><strong>Common Task File (common_tasks.yml):<\/strong><\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>---\r\n- name: Update package cache\r\n  apt:\r\n    update_cache: yes\r\n\r\n- name: Ensure NGINX is installed\r\n  apt:\r\n    name: nginx\r\n    state: present<\/code><\/pre>\n<h2 data-pm-slice=\"1 1 []\"><span class=\"ez-toc-section\" id=\"Reusing_Tasks_Across_Playbooks\"><\/span>Reusing Tasks Across Playbooks<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">You can <strong>reuse<\/strong> the same set of tasks across multiple playbooks. This is useful for defining common actions, like setting up users or configuring security settings.<\/p>\n<h5>Reusing Tasks Across Playbooks<\/h5>\n<p><strong>First Playbook (playbook1.yml):<\/strong><\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>---\r\n- name: Web Server Setup\r\n  hosts: webservers\r\n  tasks:\r\n    - import_tasks: setup_users.yml<\/code><\/pre>\n<p><strong>Second Playbook (playbook2.yml):<\/strong><\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>---\r\n- name: Database Server Setup\r\n  hosts: dbservers\r\n  tasks:\r\n    - import_tasks: setup_users.yml<\/code><\/pre>\n<p><strong>Task File (setup_users.yml):<\/strong><\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>---\r\n- name: Create user John\r\n  user:\r\n    name: john\r\n    state: present\r\n\r\n- name: Add John to sudo group\r\n  user:\r\n    name: john\r\n    groups: sudo\r\n    append: yes<\/code><\/pre>\n<p style=\"text-align: justify;\">Now, both <strong>playbook1.yml<\/strong> and <strong>playbook2.yml<\/strong> can reuse the <strong>setup_users.yml<\/strong> task file.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Using_Variables_and_Facts_in_Playbooks\"><\/span>Using Variables and Facts in Playbooks<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p data-pm-slice=\"0 0 []\">Ansible allows you to define variables using YAML syntax. <strong>Variables<\/strong> allow you to define values that can be reused across tasks, which helps avoid hardcoding values in multiple places. This improves playbook maintainability.<\/p>\n<p style=\"text-align: justify;\"><a href=\"https:\/\/linuxbuz.com\/devops\/ansible-facts-example\" target=\"_blank\" rel=\"noopener\"><strong>Ansible facts<\/strong><\/a> automatically gather data about the target hosts, such as OS, IP address, or hardware details. You can use facts to make decisions dynamically during playbook execution,.<\/p>\n<p style=\"text-align: justify;\">Let&#8217;s look at an optimized playbook that installs different web servers <strong>(nginx<\/strong> or <strong>httpd)<\/strong> based on the host&#8217;s operating system. For flexibility, we&#8217;ll combine variables and <strong>Ansible facts<\/strong>.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Install web server based on OS\r\n  hosts: all\r\n  vars:\r\n    web_package_debian: nginx\r\n    web_package_redhat: httpd\r\n  tasks:\r\n    - name: Install web server on Debian-based systems\r\n      apt:\r\n        name: \"{{ web_package_debian }}\"\r\n        state: present\r\n      when: ansible_facts['os_family'] == \"Debian\"\r\n\r\n    - name: Install web server on RedHat-based systems\r\n      yum:\r\n        name: \"{{ web_package_redhat }}\"\r\n        state: present\r\n      when: ansible_facts['os_family'] == \"RedHat\"<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Ansible_Playbook_Options_with_Examples\"><\/span>Ansible Playbook Options with Examples<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">When running a playbook, you can use several command-line options to control how the playbook executes. Understanding these options will help you customize playbook execution for different environments and scenarios.<\/p>\n<p>Below are some frequently used options in the playbook.<\/p>\n<h3>1. Using &#8211;check Mode (Dry Run)<\/h3>\n<p style=\"text-align: justify;\">The <strong>&#8211;check<\/strong> option <a href=\"https:\/\/linuxbuz.com\/devops\/ansible-playbook-dry-run\"><strong>runs the playbook in &#8220;dry run&#8221; mode<\/strong><\/a>. It simulates the changes without applying them, allowing you to test the playbook&#8217;s logic and see what would change.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>ansible-playbook playbook.yml --check<\/code><\/pre>\n<p style=\"text-align: justify;\">Use this mode when you want to validate the playbook without making changes to the target system.<\/p>\n<h3>2. Using &#8211;diff for Configuration File Changes<\/h3>\n<p style=\"text-align: justify;\">The <strong>&#8211;diff<\/strong> option shows the differences between files before and after the playbook is applied. This is useful when you&#8217;re managing configuration files.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>ansible-playbook playbook.yml --diff<\/code><\/pre>\n<p style=\"text-align: justify;\">Use this when working with configuration files to see exactly what changes will be made.<\/p>\n<h3>3. Using &#8211;verbose for Detailed Output<\/h3>\n<p style=\"text-align: justify;\">Ansible provides verbosity levels through the <strong>-v, -vv, -vvv,<\/strong> or <strong>-vvvv<\/strong> flags. The more vs you use, the more detailed the output becomes. For example:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>ansible-playbook playbook.yml -vvv<\/code><\/pre>\n<p>Use this when you want to see detailed logs of each step in the playbook&#8217;s execution.<\/p>\n<h3>4. Using the debug Module<\/h3>\n<p style=\"text-align: justify;\">The <a href=\"https:\/\/linuxbuz.com\/devops\/ansible-debug-module-examples\" target=\"_blank\" rel=\"noopener\"><strong>debug module<\/strong><\/a> helps to print the value of variables or other information during playbook execution. This is useful for troubleshooting and ensuring that variables are set correctly.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>- name: Debug variable value\r\n  hosts: all\r\n  tasks:\r\n    - name: Print OS family\r\n      debug:\r\n        var: ansible_facts['os_family']\r\n\r\n    - name: Print a custom message\r\n      debug:\r\n        msg: \"Running on {{ inventory_hostname }} which is part of {{ ansible_facts['os_family'] }}\"<\/code><\/pre>\n<h3>5. Using &#8211;start-at-task to Resume from a Specific Task<\/h3>\n<p style=\"text-align: justify;\">If a playbook fails or you want to test specific tasks, you can use the <strong>&#8211;start-at-task<\/strong> option to begin playbook execution from a particular task without starting from the beginning.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>ansible-playbook playbook.yml --start-at-task=\"Install NGINX\"<\/code><\/pre>\n<p>Use this to save time when debugging a specific part of the playbook.<\/p>\n<h3>6. Validate Playbooks with ansible-lint<\/h3>\n<p style=\"text-align: justify;\"><a href=\"https:\/\/ansible.readthedocs.io\/projects\/lint\/\" target=\"_blank\" rel=\"noopener\"><strong>ansible-lint<\/strong><\/a> is a tool that checks your playbooks for best practices and potential issues. It helps ensure that your playbook follows coding standards and doesn&#8217;t have syntax or logic errors.<\/p>\n<p>First, install <strong>ansible-lint:<\/strong><\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>pip install ansible-lint<\/code><\/pre>\n<p>Then, run the linter:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>ansible-lint playbook.yml<\/code><\/pre>\n<p>Use Ansible-Lint regularly to validate your playbooks for common errors and best practices.<\/p>\n<h3>7. Using &#8211;limit to Limit Target Hosts<\/h3>\n<p>The <strong>-l<\/strong> option limits the playbook execution to specific hosts or groups.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>ansible-playbook playbook.yml -l webservers<\/code><\/pre>\n<p>This command limits the playbook execution to hosts in the webservers group.<\/p>\n<h3>8. Using &#8211;inventory to Specify the Inventory File<\/h3>\n<p style=\"text-align: justify;\">By default, Ansible uses the <a href=\"https:\/\/linuxbuz.com\/devops\/ansible-inventory-examples\" target=\"_blank\" rel=\"noopener\"><strong>inventory<\/strong><\/a> file specified in the default configuration. You can override it using the <strong>-i<\/strong> option.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>ansible-playbook my_playbook.yml -i \/path\/to\/inventory.ini<\/code><\/pre>\n<p>This command specifies a custom inventory file for the playbook.<\/p>\n<h3>9. Using &#8211;ask-become-pass to Prompt for Sudo Password<\/h3>\n<p>If your playbook requires sudo privileges, you can use <strong>&#8211;ask-become-pass<\/strong> to prompt for the password.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>ansible-playbook playbook.yml --ask-become-pass<\/code><\/pre>\n<p>This command prompts you to enter the sudo password for tasks that require elevated privileges.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\" data-pm-slice=\"1 1 []\">Ansible playbooks give you full control over your automation\u2014from package installation to service management and OS-specific behavior.<\/p>\n<p style=\"text-align: justify;\">\u2705 Start small with a basic playbook, and scale up using loops, variables, conditionals, and task includes.<\/p>\n<p>\ud83d\udd01 Reuse your playbooks and validate your logic with <code>ansible-lint<\/code> and <code>--check<\/code> mode.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"FAQs\"><\/span>FAQs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n        <section class=\"sc_fs_faq sc_card \">\n            <div>\n\t\t\t\t<h6>1. Can I use conditionals in an Ansible Playbook?<\/h6>                <div>\n\t\t\t\t\t                    <p>\n\t\t\t\t\t\tYes, conditionals can be applied using the when clause to run tasks only when specific conditions are met.                    <\/p>\n                <\/div>\n            <\/div>\n        <\/section>\n\t\t        <section class=\"sc_fs_faq sc_card \">\n            <div>\n\t\t\t\t<h6>2. What is the purpose of the hosts keyword in playbook?<\/h6>                <div>\n\t\t\t\t\t                    <p>\n\t\t\t\t\t\tThe hosts keyword specifies the target group of hosts on which the playbook tasks will be executed.                    <\/p>\n                <\/div>\n            <\/div>\n        <\/section>\n\t\t        <section class=\"sc_fs_faq sc_card \">\n            <div>\n\t\t\t\t<h6>3. How do you handle errors in Ansible Playbooks?<\/h6>                <div>\n\t\t\t\t\t                    <p>\n\t\t\t\t\t\tYou can handle errors using the ignore_errors directive or by using block and rescue sections for more advanced error handling.                    <\/p>\n                <\/div>\n            <\/div>\n        <\/section>\n\t\t        <section class=\"sc_fs_faq sc_card \">\n            <div>\n\t\t\t\t<h6>4. What is the use of the become directive in Ansible Playbooks?<\/h6>                <div>\n\t\t\t\t\t                    <p>\n\t\t\t\t\t\tThe become directive is used to escalate privileges, allowing tasks to run as a different user, such as root, when necessary.                    <\/p>\n                <\/div>\n            <\/div>\n        <\/section>\n\t\t\n<script type=\"application\/ld+json\">\n    {\n\t\t\"@context\": \"https:\/\/schema.org\",\n\t\t\"@type\": \"FAQPage\",\n\t\t\"mainEntity\": [\n\t\t\t\t{\n\t\t\t\t\"@type\": \"Question\",\n\t\t\t\t\"name\": \"1. Can I use conditionals in an Ansible Playbook?\",\n\t\t\t\t\"acceptedAnswer\": {\n\t\t\t\t\t\"@type\": \"Answer\",\n\t\t\t\t\t\"text\": \"Yes, conditionals can be applied using the when clause to run tasks only when specific conditions are met.\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t}\n\t\t\t,\t\t\t\t{\n\t\t\t\t\"@type\": \"Question\",\n\t\t\t\t\"name\": \"2. What is the purpose of the hosts keyword in playbook?\",\n\t\t\t\t\"acceptedAnswer\": {\n\t\t\t\t\t\"@type\": \"Answer\",\n\t\t\t\t\t\"text\": \"The hosts keyword specifies the target group of hosts on which the playbook tasks will be executed.\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t}\n\t\t\t,\t\t\t\t{\n\t\t\t\t\"@type\": \"Question\",\n\t\t\t\t\"name\": \"3. How do you handle errors in Ansible Playbooks?\",\n\t\t\t\t\"acceptedAnswer\": {\n\t\t\t\t\t\"@type\": \"Answer\",\n\t\t\t\t\t\"text\": \"You can handle errors using the ignore_errors directive or by using block and rescue sections for more advanced error handling.\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t}\n\t\t\t,\t\t\t\t{\n\t\t\t\t\"@type\": \"Question\",\n\t\t\t\t\"name\": \"4. What is the use of the become directive in Ansible Playbooks?\",\n\t\t\t\t\"acceptedAnswer\": {\n\t\t\t\t\t\"@type\": \"Answer\",\n\t\t\t\t\t\"text\": \"The become directive is used to escalate privileges, allowing tasks to run as a different user, such as root, when necessary.\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t}\n\t\t\t\t    ]\n}\n<\/script>\n\n","protected":false},"excerpt":{"rendered":"<p>Ansible Playbooks are the heart of Ansible automation. They provide a simple way to define automation workflows using YAML. Playbooks consist of instructions (tasks) that Ansible executes on remote machines. &hellip; <\/p>\n","protected":false},"author":1,"featured_media":6196,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"two_page_speed":[],"footnotes":""},"categories":[828],"tags":[],"class_list":["post-6195","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/posts\/6195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/comments?post=6195"}],"version-history":[{"count":0,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/posts\/6195\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/media\/6196"}],"wp:attachment":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/media?parent=6195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/categories?post=6195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/tags?post=6195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}