{"id":50175,"date":"2025-07-13T06:26:55","date_gmt":"2025-07-13T06:26:55","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=50175"},"modified":"2025-07-13T06:26:55","modified_gmt":"2025-07-13T06:26:55","slug":"the-complete-ansible-tutorial-concepts-architecture-playbooks-and-real-world-examples","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/the-complete-ansible-tutorial-concepts-architecture-playbooks-and-real-world-examples\/","title":{"rendered":"The Complete Ansible Tutorial: Concepts, Architecture, Playbooks, and Real-World Examples"},"content":{"rendered":"\n<p>Here is a <strong>comprehensive, professional-quality Ansible tutorial<\/strong> based on your notes, structured as a long-form guide. Every topic, term, and example is included, with deep explanations and practical demonstrations.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><\/h1>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. <strong>What is Ansible?<\/strong><\/h2>\n\n\n\n<p><strong>Ansible<\/strong> is an open-source configuration management, application deployment, and server orchestration tool created by Red Hat. It enables IT professionals and developers to <strong>automate the management of servers and software<\/strong>, ensuring consistent and repeatable outcomes.<\/p>\n\n\n\n<p><strong>Key Points:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Configuration Management Tool:<\/strong> Automates the setup, maintenance, and configuration of servers.<\/li>\n\n\n\n<li><strong>Server Management Tool:<\/strong> Efficiently manages large groups of servers from a single control point.<\/li>\n\n\n\n<li><strong>Written in Python:<\/strong> Ansible is built using Python, making it cross-platform and highly extensible.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Product Releases:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Ansible (CLI):<\/strong> Free command-line tool for automation.<\/li>\n\n\n\n<li><strong>Ansible Tower:<\/strong> Paid UI-based solution for enterprise teams (now called Red Hat Ansible Automation Platform).<\/li>\n\n\n\n<li><strong>AWX:<\/strong> Free, open-source UI for Ansible (community-supported version of Tower).<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. <strong>Why Do We Need Ansible?<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Common Use Cases:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>IT Teams:<\/strong> Deploy and configure system software, antivirus, company policies, etc.<\/li>\n\n\n\n<li><strong>Developers:<\/strong> Automate the deployment of application code and related services.<\/li>\n\n\n\n<li><strong>Any Resource Management:<\/strong> If you need to change files, directories, users, packages, or services across many servers, Ansible is ideal.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Benefits of Using Ansible:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Cost Savings:<\/strong> Apply changes to hundreds or thousands of servers at once, reducing manual effort.<\/li>\n\n\n\n<li><strong>Time Savings:<\/strong> Execute bulk operations quickly and repeatably.<\/li>\n\n\n\n<li><strong>Improved Quality:<\/strong> Achieve consistent, predictable changes on every server.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Typical Server Management Tasks:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Managing files and directories<\/li>\n\n\n\n<li>Installing or removing packages (<code>apt<\/code>, <code>yum<\/code>)<\/li>\n\n\n\n<li>Controlling services (start, stop, restart)<\/li>\n\n\n\n<li>Executing shell or command tasks<\/li>\n\n\n\n<li>Managing users and groups<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. <strong>Alternatives to Ansible<\/strong><\/h2>\n\n\n\n<p>Other popular configuration management tools include:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Tool<\/th><th>Language<\/th><th>Agent\/Agentless<\/th><th>Notable Use<\/th><\/tr><\/thead><tbody><tr><td><strong>Chef<\/strong><\/td><td>Ruby<\/td><td>Agent-based<\/td><td>Code-driven config mgmt.<\/td><\/tr><tr><td><strong>Puppet<\/strong><\/td><td>Ruby<\/td><td>Agent-based<\/td><td>Infrastructure as code<\/td><\/tr><tr><td><strong>CFEngine<\/strong><\/td><td>C<\/td><td>Agent-based<\/td><td>Lightweight, scalable<\/td><\/tr><tr><td><strong>SaltStack<\/strong><\/td><td>Python<\/td><td>Both<\/td><td>Fast remote execution<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Ansible\u2019s main advantage:<\/strong> <strong>Agentless<\/strong> operation\u2014no special software required on managed hosts.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. <strong>How Does Ansible Work? (Architecture Overview)<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Key Terminologies:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>ACS:<\/strong> <em>Ansible Control Server<\/em> (the machine from which you run Ansible).<\/li>\n\n\n\n<li><strong>ARS:<\/strong> <em>Ansible Remote Server<\/em> (the target machine(s) being managed).<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Architecture Flow:<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">Human (You)\n   |\n   V\nAnsible Control Server (ACS)\n   |\n   V\nAnsible Remote Server(s) (ARS)\n<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><strong>Requirements:<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Component<\/th><th>ACS (Control)<\/th><th>ARS (Remote)<\/th><\/tr><\/thead><tbody><tr><td>OS<\/td><td>Linux 64-bit<\/td><td>Linux\/Windows\/others<\/td><\/tr><tr><td>Python<\/td><td>Yes<\/td><td>Yes (Linux), .NET\/PS (Windows)<\/td><\/tr><tr><td>Ansible<\/td><td>Yes<\/td><td>No<\/td><\/tr><tr><td>Agent<\/td><td>No (agentless)<\/td><td>No (agentless)<\/td><\/tr><tr><td>Communication<\/td><td>N\/A<\/td><td>SSH (Linux), WinRM (Windows)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Agentless:<\/strong> No Ansible agent is installed on remote servers; communication is via <strong>SSH<\/strong> (Linux\/Unix) or <strong>WinRM<\/strong> (Windows).<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. <strong>Core Components of Ansible<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.1. Executables<\/strong><\/h3>\n\n\n\n<p>Main CLI tools found in <code>\/usr\/bin\/<\/code>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ansible<\/code>: Runs single tasks (ad-hoc).<\/li>\n\n\n\n<li><code>ansible-playbook<\/code>: Runs playbooks (multiple tasks).<\/li>\n\n\n\n<li><code>ansible-galaxy<\/code>: Manages Ansible roles and collections.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.2. Modules<\/strong><\/h3>\n\n\n\n<p>A <strong>module<\/strong> is a reusable, standalone script (written in Python) that performs a specific task. They reside on the ACS and run <em>remotely<\/em> on ARS.<\/p>\n\n\n\n<p><strong>Examples:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>copy<\/code> (copies files)<\/li>\n\n\n\n<li><code>apt<\/code>, <code>yum<\/code> (manage packages)<\/li>\n\n\n\n<li><code>service<\/code> (manage services)<\/li>\n\n\n\n<li><code>user<\/code> (manage users)<\/li>\n\n\n\n<li><a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/collections\/index_module.html\" target=\"_blank\" rel=\"noopener\">See full list here<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.3. Plugins<\/strong><\/h3>\n\n\n\n<p>A <strong>plugin<\/strong> is a piece of code that adds extra functionality to Ansible itself (runs on ACS). Types include connection plugins, callback plugins, lookup plugins, etc.<\/p>\n\n\n\n<p><a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/collections\/all_plugins.html\" target=\"_blank\" rel=\"noopener\">See all plugins here<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.4. Configuration File<\/strong><\/h3>\n\n\n\n<p>The main config file is:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\/etc\/ansible\/ansible.cfg<\/code><\/li>\n<\/ul>\n\n\n\n<p><a href=\"https:\/\/www.devopsschool.com\/blog\/ansible-tutorials-example-of-ansible-cfg\/\">Ansible config example and details<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.5. Inventory (Host File)<\/strong><\/h3>\n\n\n\n<p>The <strong>inventory<\/strong> is a file or script that lists the IPs\/hostnames of the ARS to be managed.<\/p>\n\n\n\n<p><strong>Inventory Example:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-attr\">&#91;web]<\/span>\n13<span class=\"hljs-selector-class\">.127<\/span><span class=\"hljs-selector-class\">.150<\/span><span class=\"hljs-selector-class\">.68<\/span>\n3<span class=\"hljs-selector-class\">.110<\/span><span class=\"hljs-selector-class\">.173<\/span><span class=\"hljs-selector-class\">.45<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<p>[db]<\/p>\n\n\n\n<p>1.2.43.5 1.2.43.6<\/p>\n\n\n\n<p>You can provide the inventory as a file, inline on the command line, or generate it dynamically.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.6. Playbook<\/strong><\/h3>\n\n\n\n<p>A <strong>playbook<\/strong> is a YAML file that defines a set of tasks to execute on remote hosts using Ansible modules.<\/p>\n\n\n\n<p><strong>Sample Playbook:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">---\n- name: Update web servers\n  <span class=\"hljs-attr\">hosts<\/span>: web\n  <span class=\"hljs-attr\">tasks<\/span>:\n    - name: Install Apache <span class=\"hljs-keyword\">in<\/span> ubuntu\n      ansible.builtin.apt:\n        name: <span class=\"hljs-string\">\"apache2\"<\/span>\n        <span class=\"hljs-attr\">state<\/span>: latest\n    - name: Copy index.html\n      ansible.builtin.copy:\n        src: index.html\n        <span class=\"hljs-attr\">dest<\/span>: <span class=\"hljs-regexp\">\/var\/<\/span>www\/html\/index.html\n    - name: Starting Apache Server\n      ansible.builtin.service:\n        name: <span class=\"hljs-string\">\"apache2\"<\/span>\n        <span class=\"hljs-attr\">state<\/span>: started\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.devopsschool.com\/blog\/ansible-playbook-example\/\">More playbook examples<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6. <strong>How to Write a Playbook<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step-by-Step:<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Define the Project:<\/strong> e.g., &#8220;Set up a web server&#8221;.<\/li>\n\n\n\n<li><strong>List Steps:<\/strong> What should happen? (install apache2, copy files, start service).<\/li>\n\n\n\n<li><strong>For Each Step:<\/strong> Find the relevant module and parameters.<\/li>\n<\/ol>\n\n\n\n<p><strong>Example Playbook for Webserver:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">---\n- name: Setup Webserver\n  <span class=\"hljs-attr\">hosts<\/span>: web\n  <span class=\"hljs-attr\">tasks<\/span>:\n    - name: Install Apache2\n      ansible.builtin.apt:\n        name: apache2\n        <span class=\"hljs-attr\">state<\/span>: latest\n    - name: Copy app file\n      ansible.builtin.copy:\n        src: app.html\n        <span class=\"hljs-attr\">dest<\/span>: <span class=\"hljs-regexp\">\/var\/<\/span>www\/html\/app.html\n    - name: Start apache2\n      ansible.builtin.service:\n        name: apache2\n        <span class=\"hljs-attr\">state<\/span>: started\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">7. <strong>Ansible Ad-Hoc Commands<\/strong><\/h2>\n\n\n\n<p><strong>Ad-hoc commands<\/strong> let you run one-off tasks without writing a playbook.<\/p>\n\n\n\n<p><strong>General Syntax:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">ansible <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">target<\/span>&gt;<\/span> -m <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">module<\/span>&gt;<\/span> -a \"<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">module<\/span> <span class=\"hljs-attr\">options<\/span>&gt;<\/span>\"\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><strong>Examples (local):<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># Install Apache2 package<\/span>\nansible localhost -m apt -a <span class=\"hljs-string\">\"name=apache2 state=latest\"<\/span>\n\n<span class=\"hljs-comment\"># Stop Apache2 service<\/span>\nansible localhost -m service -a <span class=\"hljs-string\">\"name=apache2 state=stopped\"<\/span>\n\n<span class=\"hljs-comment\"># Start Apache2 service<\/span>\nansible localhost -m service -a <span class=\"hljs-string\">\"name=apache2 state=started\"<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><strong>10 More Local Ad-Hoc Command Examples:<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># 1. Create a user<\/span>\nansible localhost -m user -a <span class=\"hljs-string\">\"name=raj password=abc123\"<\/span>\n\n<span class=\"hljs-comment\"># 2. Add group<\/span>\nansible localhost -m group -a <span class=\"hljs-string\">\"name=developers\"<\/span>\n\n<span class=\"hljs-comment\"># 3. Copy a file<\/span>\nansible localhost -m copy -a <span class=\"hljs-string\">\"src=\/tmp\/a.txt dest=\/tmp\/b.txt\"<\/span>\n\n<span class=\"hljs-comment\"># 4. Fetch a file<\/span>\nansible localhost -m fetch -a <span class=\"hljs-string\">\"src=\/tmp\/b.txt dest=\/tmp\/\"<\/span>\n\n<span class=\"hljs-comment\"># 5. Run a shell command<\/span>\nansible localhost -m shell -a <span class=\"hljs-string\">\"uptime\"<\/span>\n\n<span class=\"hljs-comment\"># 6. Change file permissions<\/span>\nansible localhost -m file -a <span class=\"hljs-string\">\"path=\/tmp\/b.txt mode=0644\"<\/span>\n\n<span class=\"hljs-comment\"># 7. Install multiple packages<\/span>\nansible localhost -m apt -a <span class=\"hljs-string\">\"name=git,curl state=present\"<\/span>\n\n<span class=\"hljs-comment\"># 8. Reboot the system<\/span>\nansible localhost -m reboot\n\n<span class=\"hljs-comment\"># 9. Get disk space info<\/span>\nansible localhost -m command -a <span class=\"hljs-string\">\"df -h\"<\/span>\n\n<span class=\"hljs-comment\"># 10. List all users<\/span>\nansible localhost -m shell -a <span class=\"hljs-string\">\"cat \/etc\/passwd\"<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Running Ad-Hoc Commands on Multiple Servers<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Using Inline Inventory:<\/strong><\/h4>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">ansible all -i <span class=\"hljs-number\">13.127<\/span><span class=\"hljs-number\">.150<\/span><span class=\"hljs-number\">.68<\/span>,<span class=\"hljs-number\">3.110<\/span><span class=\"hljs-number\">.173<\/span><span class=\"hljs-number\">.45<\/span>, -m apt -a <span class=\"hljs-string\">\"name=apache2 state=latest\"<\/span>\nansible all -i <span class=\"hljs-number\">13.127<\/span><span class=\"hljs-number\">.150<\/span><span class=\"hljs-number\">.68<\/span>,<span class=\"hljs-number\">3.110<\/span><span class=\"hljs-number\">.173<\/span><span class=\"hljs-number\">.45<\/span>, -m service -a <span class=\"hljs-string\">\"name=apache2 state=started\"<\/span> -u ubuntu -b --key-file=node.pem\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h4 class=\"wp-block-heading\"><strong>Using Inventory File:<\/strong><\/h4>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">ansible all -i inventory -m apt -a <span class=\"hljs-string\">\"name=apache2 state=latest\"<\/span> -u ubuntu -b --key-file=node.pem\nansible all -i inventory -m service -a <span class=\"hljs-string\">\"name=apache2 state=started\"<\/span> -u ubuntu -b --key-file=node.pem\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">8. <strong>Running Ansible Playbooks<\/strong><\/h2>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">ansible-playbook -i <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">inventory<\/span>&gt;<\/span> <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">playbook.yaml<\/span>&gt;<\/span> -u <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">user<\/span>&gt;<\/span> -b --key-file=<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">private-key<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Examples:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">ansible-playbook -i inventory web.yaml -u ubuntu -b --key-file=node.pem\nansible-playbook -i inventory db.yaml -u ubuntu -b --key-file=node.pem\nansible-playbook -i inventory master.yaml -u ubuntu -b --key-file=node.pem\n<\/code><\/span><\/pre>\n\n\n<p><strong>Inventory file example:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-attr\">&#91;web]<\/span>\n13<span class=\"hljs-selector-class\">.127<\/span><span class=\"hljs-selector-class\">.150<\/span><span class=\"hljs-selector-class\">.68<\/span>\n3<span class=\"hljs-selector-class\">.110<\/span><span class=\"hljs-selector-class\">.173<\/span><span class=\"hljs-selector-class\">.45<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<p>[db]<\/p>\n\n\n\n<p>1.2.43.5 1.2.43.6 1.2.43.8 1.2.43.9<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">9. <strong>Playbook Task Inclusion<\/strong><\/h2>\n\n\n\n<p>You can include other playbooks or tasks within your main playbook:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">- hosts: web\n  tasks:\n    - debug: msg=<span class=\"hljs-string\">\"task1\"<\/span>\n    - name: <span class=\"hljs-keyword\">Include<\/span> task <span class=\"hljs-keyword\">list<\/span> in play\n      <span class=\"hljs-keyword\">include<\/span>: web.yaml\n    - name: <span class=\"hljs-keyword\">Include<\/span> task <span class=\"hljs-keyword\">list<\/span> in play\n      <span class=\"hljs-keyword\">include<\/span>: db.yaml\n\n- hosts: db\n  tasks:\n    - debug: msg=<span class=\"hljs-string\">\"task1\"<\/span>\n    - name: <span class=\"hljs-keyword\">Include<\/span> task <span class=\"hljs-keyword\">list<\/span> in play\n      <span class=\"hljs-keyword\">include<\/span>: web.yaml\n    - name: <span class=\"hljs-keyword\">Include<\/span> task <span class=\"hljs-keyword\">list<\/span> in play\n      <span class=\"hljs-keyword\">include<\/span>: db.yaml\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">10. <strong>Ansible Variables<\/strong><\/h2>\n\n\n\n<p>Variables allow you to parametrize your playbooks for flexibility and reuse.<\/p>\n\n\n\n<p><strong>Variable Sources:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Inline:<\/strong> Defined directly in playbook under <code>vars:<\/code><\/li>\n\n\n\n<li><strong>Files:<\/strong> Separate YAML files loaded with <code>vars_files:<\/code><\/li>\n\n\n\n<li><strong>Prompt:<\/strong> Ask user input with <code>vars_prompt:<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">---\n- name: Update web servers\n  hosts: web\n  vars:\n    myname: <span class=\"hljs-string\">\"Rajeshkumar\"<\/span>\n    age: <span class=\"hljs-string\">\"18\"<\/span>\n    packagename: <span class=\"hljs-string\">\"apache2\"<\/span>\n    servicename: <span class=\"hljs-string\">\"apache2\"<\/span>\n  vars_files:\n    - <span class=\"hljs-string\">\"vars.yaml\"<\/span>\n  vars_prompt:\n    - name: <span class=\"hljs-string\">\"version\"<\/span>\n      prompt: <span class=\"hljs-string\">\"Which version Do you want to install?\"<\/span>\n      <span class=\"hljs-keyword\">private<\/span>: no\n\n  tasks:\n    - name: Install Apache in ubuntu\n      ansible.builtin.apt:\n        name: <span class=\"hljs-string\">\"{{ packagename }}\"<\/span>\n        state: latest\n    - name: Copy index.html\n      ansible.builtin.copy:\n        src: index.html\n        dest: \/<span class=\"hljs-keyword\">var<\/span>\/www\/html\/index.html\n    - name: Starting a Apache Server\n      ansible.builtin.service:\n        name: <span class=\"hljs-string\">\"{{ servicename }}\"<\/span>\n        state: started\n    - name: <span class=\"hljs-keyword\">Print<\/span> name <span class=\"hljs-keyword\">and<\/span> age\n      ansible.builtin.debug:\n        msg: <span class=\"hljs-string\">\"My Name is {{ myname }} and My age is {{ age }}\"<\/span>\n    - name: <span class=\"hljs-keyword\">Print<\/span> version from prompt\n      ansible.builtin.debug:\n        <span class=\"hljs-keyword\">var<\/span>: version\n    - name: Register <span class=\"hljs-keyword\">and<\/span> <span class=\"hljs-keyword\">print<\/span> file <span class=\"hljs-keyword\">list<\/span>\n      shell: <span class=\"hljs-string\">\"find *.txt\"<\/span>\n      args:\n        chdir: <span class=\"hljs-string\">\"\/root\/ansible\"<\/span>\n      register: find_output\n    - debug:\n        <span class=\"hljs-keyword\">var<\/span>: find_output\n    - debug:\n        <span class=\"hljs-keyword\">var<\/span>: find_output.stdout_lines\n    - debug:\n        <span class=\"hljs-keyword\">var<\/span>: find_output.stdout_lines&#91;<span class=\"hljs-number\">0<\/span>]\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Running Playbook:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">ansible-playbook -i inventory vars.yaml -u ubuntu -b --key-file=node.pem\n<\/code><\/span><\/pre>\n\n\n<p><a href=\"https:\/\/www.devopsschool.com\/blog\/example-code-of-ansible-variable-with-playbook\/\">More variable examples<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">11. <strong>Ansible Roles<\/strong><\/h2>\n\n\n\n<p>Roles provide a <strong>standard way to organize playbooks<\/strong> and make code reusable and shareable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Role Directory Structure:<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">roles\/\n  web\/\n    tasks\/\n      main.yaml\n    handlers\/\n      main.yaml\n    files\/\n    templates\/\n    vars\/\n      main.yaml\n    defaults\/\n      main.yaml\n<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><strong>Using a Role in a Playbook:<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">---\n- name: Update web servers\n  hosts: web\n  roles:\n    - web\n<\/code><\/span><\/pre>\n\n\n<p><strong>Run:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">ansible-playbook -i inventory site.yaml -u ubuntu -b --key-file=node.pem\n<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><strong>Key Concepts:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Tasks:<\/strong> Steps to be executed.<\/li>\n\n\n\n<li><strong>Vars:<\/strong> Variables specific to the role.<\/li>\n\n\n\n<li><strong>Templates:<\/strong> Jinja2 templates to render config files dynamically.<\/li>\n\n\n\n<li><strong>Handlers:<\/strong> Special tasks triggered by \u201cnotify\u201d for things like restarts.<\/li>\n\n\n\n<li><strong>Files:<\/strong> Static files to be copied to the remote host.<\/li>\n<\/ul>\n\n\n\n<p><strong>Benefits:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Standardization<\/li>\n\n\n\n<li>Shareability (e.g., via Ansible Galaxy)<\/li>\n\n\n\n<li>Cleaner code organization<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">12. <strong>Summary: Why Ansible is Essential<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Agentless:<\/strong> No extra software on managed hosts<\/li>\n\n\n\n<li><strong>Idempotent:<\/strong> Safe to run multiple times (won\u2019t break things)<\/li>\n\n\n\n<li><strong>Readable:<\/strong> Human-friendly YAML syntax<\/li>\n\n\n\n<li><strong>Extensible:<\/strong> Easily integrated with cloud, containers, CI\/CD, etc.<\/li>\n\n\n\n<li><strong>Community Powered:<\/strong> Thousands of modules and roles available<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Quick Reference Table<\/strong><\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Term<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td>ACS<\/td><td>Ansible Control Server (where you run Ansible)<\/td><\/tr><tr><td>ARS<\/td><td>Ansible Remote Server (where tasks are applied)<\/td><\/tr><tr><td>Module<\/td><td>Small, reusable unit of work (e.g., <code>apt<\/code>, <code>copy<\/code>, <code>service<\/code>)<\/td><\/tr><tr><td>Plugin<\/td><td>Extends Ansible (connection, lookup, callback, etc.)<\/td><\/tr><tr><td>Inventory<\/td><td>List of managed hosts<\/td><\/tr><tr><td>Playbook<\/td><td>Ordered set of tasks in YAML<\/td><\/tr><tr><td>Variable<\/td><td>Parameter for use in playbooks\/tasks<\/td><\/tr><tr><td>Role<\/td><td>Directory structure for reusability<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Further Resources<\/strong><\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/docs.ansible.com\/\" target=\"_blank\" rel=\"noopener\">Ansible Docs<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/ansible-playbook-example\/\">Ansible Playbook Examples<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/tutorial\/ansible\/\">DevOpsSchool Ansible Tutorials<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>If you need more detailed sections, live scenarios, or want to expand on topics like dynamic inventories, Ansible Galaxy, or advanced Jinja2 templating, just let me know!<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is a comprehensive, professional-quality Ansible tutorial based on your notes, structured as a long-form guide. Every topic, term, and example is included, with deep explanations and practical demonstrations. 1&#8230;. <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-50175","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/50175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=50175"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/50175\/revisions"}],"predecessor-version":[{"id":50176,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/50175\/revisions\/50176"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=50175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=50175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=50175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}