Free AI web copilot to create summaries, insights and extended knowledge, download it at here
9612
Abstract
ass="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong">*
ok: [demo.example.com] => {
"msg": "harmful task"
}
ok: [demo2.example.com] => {
"msg": "harmful task"
}</span></pre></div><div id="c415"><pre>PLAY RECAP ****************************************************************************************
demo.example.com : <span class="hljs-attribute">ok</span>=2 <span class="hljs-attribute">changed</span>=0 <span class="hljs-attribute">unreachable</span>=0 <span class="hljs-attribute">failed</span>=0 <span class="hljs-attribute">skipped</span>=0 <span class="hljs-attribute">rescued</span>=0 <span class="hljs-attribute">ignored</span>=0
demo2.example.com : <span class="hljs-attribute">ok</span>=2 <span class="hljs-attribute">changed</span>=0 <span class="hljs-attribute">unreachable</span>=0 <span class="hljs-attribute">failed</span>=0 <span class="hljs-attribute">skipped</span>=0 <span class="hljs-attribute">rescued</span>=0 <span class="hljs-attribute">ignored</span>=0</pre></div><div id="b2db"><pre>ansible-pilot <span class="hljs-symbol"></span></pre></div><h1 id="595a">Ansible Playbook hosts</h1><ul><li>`<code>hosts: HOSTNAME</code>`</li><li>`<code>ansible-playbook PLAYBOOK</code>`</li></ul><p id="5d6e">Using the `<code>hosts</code>` statement in the Ansible Playbook allows you to specify a host or a group of hosts for the execution.
The advantage is that is more reliable than manually specifying the hostname than using the `<code>--limit</code>` parameter from the command line.
The drawback is that you need to remember to edit the Ansible Playbook code every time. If you don’t you’re going to execute the code on the specified host, still a potential manual issue.</p><h2 id="de8c">code</h2><ul><li>playbook2.yml</li></ul><div id="de3a"><pre><span class="hljs-meta">---</span>
<span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">harmful</span> <span class="hljs-string">playbook</span>
<span class="hljs-attr">hosts:</span> <span class="hljs-string">demo.example.com</span>
<span class="hljs-attr">tasks:</span>
<span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">harmful</span> <span class="hljs-string">task</span>
<span class="hljs-attr">ansible.builtin.debug:</span>
<span class="hljs-attr">msg:</span> <span class="hljs-string">"harmful task"</span></pre></div><h2 id="a555">execution</h2><div id="28c7"><pre>ansible-pilot ansible-playbook -i <span class="hljs-built_in">limit</span>/inventory <span class="hljs-built_in">limit</span>/playbook2.yml</pre></div><div id="6f88"><pre>PLAY [harmful playbook] <span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"><span class="hljs-emphasis"></span></span></pre></div><div id="27a9"><pre>TASK [Gathering Facts] <span class="hljs-strong"></span><span class="hljs-strong">**</span><span class="hljs-strong"></span><span class="hljs-strong">**</span><span class="hljs-strong"></span><span class="hljs-strong">**</span><span class="hljs-strong"></span><span class="hljs-strong">**</span><span class="hljs-strong"></span><span class="hljs-strong">**</span><span class="hljs-strong"></span><span class="hljs-strong">**</span><span class="hljs-strong"></span><span class="hljs-strong">**</span><span class="hljs-strong"></span><span class="hljs-strong">**</span><span class="hljs-strong"></span><span class="hljs-strong">**</span><span class="hljs-strong"></span>
ok: [demo.example.com]</pre></div><div id="c864"><pre>TASK [harmful task] <span class="hljs-strong">**</span><span class="hljs-strong"></span><span class="hljs-strong">**</span><span class="hljs-strong"></span><span class="hljs-strong">**</span><span class="hljs-strong"></span><span class="hljs-strong">**</span><span class="hljs-strong"></span><span class="hljs-strong">**</span><span class="hljs-strong"></span><span class="hljs-strong">**</span><span class="hljs-strong"></span><span class="hljs-strong">**</span><span class="hljs-strong"></span><span class="hljs-strong">**</span><span class="hljs-strong"></span><span class="hljs-strong">**</span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong">
ok: [demo.example.com] => {
"msg": "harmful task"
}</span></pre></div><div id="ce46"><pre>PLAY RECAP <span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span>
demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0</pre></div><div id="1ac1"><pre>ansible-pilot <span class="hljs-symbol"></span></pre></div><h1 id="acbe">Ansible Playbook hosts advanced</h1><ul><li>`<code>hosts: “{{ HOSTS }}”</code>`</li><li>`<code>ansible-playbook -e “HOSTS=demo.example.com” PLAYBOOK</code>`</li></ul><p id="a8e2">Using the `<code>hosts</code>` statement in the Ansible Playbook allows you to specify also a variable that you could populate with a host or a group of host for the execution.
Basically, if you blindly execute the Ansible Playbook code on the command line is doing nothing. The only way is to process is to populate the host variable via an extra variable via the console line.
The advantage is that is more reliable than manually specifying the hostname than using the `<code>--limit</code>` parameter from the command line.
This option combines the advantages of the previous option and my favorite as well.</p><h2 id="c797">code</h2><ul><li>playbook3.yml</li></ul><div id="73ec"><pre><span class="hljs-meta">---</span>
<span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">harmful</span> <span class="hljs-string">playbook</span>
<span class="hljs-attr">hosts:</span> <span class="hljs-string">"<span class="hljs-template-variable">{{ HOSTS }}</span>"</span>
<span class="hljs-attr">tasks:</span>
<span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">harmful</span> <span class="hljs-string">task</span>
<span class="hljs-attr">ansible.builtin.debug:</span>
<span class="hljs-attr">msg:</span> <span class="hljs-string">"harmful task"</span></pre></div><h2 id="ca0d">execution</h2><div id="80ca"><pre>ansible-pilot ansible-playbook -i <span class="hljs-built_in">limit</span>/inventory -e <span class="hljs-string">"HOSTS=demo.example.com"</span> <span class="hljs-built_in">limit</span>/playbook3.yml</pre></div><div id="ffda"><pre>PLAY [harmful playbook] <span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"><span class="hljs-emphasis"></span></span></pre></div><div id="1b1d"><pre>TASK [Gathering Facts] <span class="hljs-strong"></span><span class="hljs-strong">*</span><span class="hljs-strong">**</span><span class="hljs-s
Options
trong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span>
ok: [demo.example.com]</pre></div><div id="66ac"><pre>TASK [harmful task] <span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong">*
ok: [demo.example.com] => {
"msg": "harmful task"
}</span></pre></div><div id="d3ca"><pre>PLAY RECAP <span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span>
demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0</pre></div><div id="4a0e"><pre>ansible-pilot <span class="hljs-symbol"></span></pre></div><h2 id="58f9">wrong execution</h2><div id="6bab"><pre>ansible-pilot ansible-playbook -i <span class="hljs-built_in">limit</span>/inventory <span class="hljs-built_in">limit</span>/playbook3.yml
[WARNING]: Could not match supplied host pattern, ignoring: HOSTS</pre></div><div id="cbbe"><pre>PLAY [harmful playbook] <span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong">*
skipping: no hosts matched</span></pre></div><div id="c824"><pre>PLAY RECAP <span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span></pre></div><div id="709b"><pre>ansible-pilot <span class="hljs-symbol">$</span></pre></div><h1 id="896f">Recap</h1><p id="6d03">Now you know the Three options to Safely Limit Ansible Playbooks Execution to a Single Machine that you could apply to your everyday journey based on your use case.</p><p id="7853">Subscribe to the<a href="https://www.youtube.com/channel/UC5MNbTYRHSCu9vAki3z9SmA"> YouTube channel</a>,<a href="https://ansiblepilot.medium.com/"> Medium</a>, and<a href="https://www.ansiblepilot.com/"> Website</a> to not miss the next episode of the Ansible Pilot.</p><h2 id="7fd8">Video Course</h2><ul><li><a href="https://click.linksynergy.com/deeplink?id=euGmLrdj*Ec&mid=39197&murl=https%3A%2F%2Fwww.udemy.com%2Fcourse%2Fansible-by-examples-devops%2F%3FreferralCode%3D8E065F6D6F8622A3DEC8"><b>Learn Ansible Automation in 250+examples & practical lessons: Learn Ansible with some real-life examples of how to use the most common modules and Ansible Playbook</b></a></li></ul><h2 id="a033">Printed Book</h2><figure id="7621"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*eK7ZTcUkiSDTh6S7"><figcaption><a href="https://amzn.to/3XHeDLd">Ansible for VMware by Examples</a></figcaption></figure><figure id="b595"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*ct4RPFWWWeUP1m9S"><figcaption><a href="https://amzn.to/3NLCAMB">Ansible for Kubernetes by Example</a></figcaption></figure><figure id="4719"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*hvcRcNX06JcnqTTK"><figcaption><a href="https://amzn.to/43HEMuL">Hands-on Anasible Automation</a></figcaption></figure><h2 id="223c">eBooks</h2><ul><li><a href="https://leanpub.com/ansiblebyexamples"><b>Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps</b></a></li><li><a href="https://leanpub.com/ansibleforwindowsbyexamples"><b>Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps</b></a></li><li><a href="https://leanpub.com/ansibleforlinuxbyexamples"><b>Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps</b></a></li><li><a href="https://leanpub.com/linuxfileanddirectorybyansibleexamples"><b>Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure</b></a></li><li><a href="https://leanpub.com/ansible-for-kubernetes-by-examples"><b>Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift</b></a></li><li><a href="https://leanpub.com/ansibleforsecuritybyexamples"><b>Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure</b></a></li><li><a href="https://leanpub.com/ansible-tips-and-tricks"><b>Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks</b></a></li><li><a href="https://leanpub.com/ansiblelinuxusersandgroupsbyexamples"><b>Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure</b></a></li><li><a href="https://leanpub.com/ansible-for-postgresql-by-examples"><b>Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL Database</b></a></li><li><a href="https://leanpub.com/ansible-for-aws-by-examples"><b>Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure</b></a></li><li><a href="https://leanpub.com/Ansible-Automation-Platform/"><b>Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios</b></a></li></ul><h1 id="3063">Donate</h1><div id="c671" class="link-block">
<a href="https://patreon.com/lucaberton">
<div>
<div>
<h2>Luca Berton is creating Software Open Source for Ansible | Patreon</h2>
<div><h3>Become a patron of Luca Berton today: Get access to exclusive content and experiences on the world's largest membership…</h3></div>
<div><p>patreon.com</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*YpFcQ4mmKi09is62)"></div>
</div>
</div>
</a>
</div><div id="c681" class="link-block">
<a href="https://github.com/sponsors/lucab85">
<div>
<div>
<h2>Sponsor @lucab85 on GitHub Sponsors</h2>
<div><h3>I am an active Open Source contributor, involved in Ansible communities, though I am all over the place. @lucab85's…</h3></div>
<div><p>github.com</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*VNJTrIi-0nHm1mC1)"></div>
</div>
</div>
</a>
</div><figure id="56d7"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*qGzstR1pOn6sqYsO3Vxakg.png"><figcaption></figcaption></figure></article></body>
Three options to Safely Limit Ansible Playbooks Execution to a Single Machine.
Today we’re going to talk about the three options to limit the execution of a potentially harmful Ansible Playbook to only one host.
I’m Luca Berton and welcome to today’s episode of Ansible Pilot.
--limit` at runtimehosts: HOSTNAME` Ansible Playbookhosts: “{{ HOSTS }}”` Ansible PlaybookLet’s deep dive into our use case to Limit Ansible Playbook to only one HOSTNAME.
I’m going to show three different ways to achieve this result:
using the `--limit` parameter at runtime, limit the HOSTNAME in the Playbook code and the most advanced way is to define a variable in the Ansible Playbook that you could populate on-demand.
Let’s discuss the pros and cons of each option.
In the following demo scenarios, I’d like to execute my harmful Ansible Playbook ONLY against demo.example.com host.
This is my demo inventory file:
[linux]
demo.example.com
demo2.example.com[all:vars]
ansible_connection=ssh
ansible_user=devops
ansible_ssh_private_key_file=~/.ssh/id_rsa--limit`ansible-playbook — limit HOSTNAME PLAYBOOK`Using the `--limit` parameter of the `ansible-playbook` command is the easiest option to limit the execution of the code to only one host.
The advantage is that you don’t need to edit the Ansible Playbook code before executing to only one host.
The drawback is that you should remember every time you execute the command and sometimes humans are not so reliable.
---
- name: harmful playbook
hosts: all
tasks:
- name: harmful task
ansible.builtin.debug:
msg: "harmful task"ansible-pilot $ ansible-playbook --limit demo.example.com -i limit/inventory limit/playbook.ymlPLAY [harmful playbook] ***************************************************************************TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]TASK [harmful task] *******************************************************************************
ok: [demo.example.com] => {
"msg": "harmful task"
}PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0ansible-pilot $If we forgot the --limit option the result could be very harmful.
ansible-pilot $ ansible-playbook -i limit/inventory limit/playbook.ymlPLAY [harmful playbook] ***************************************************************************TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
ok: [demo2.example.com]TASK [harmful task] *******************************************************************************
ok: [demo.example.com] => {
"msg": "harmful task"
}
ok: [demo2.example.com] => {
"msg": "harmful task"
}PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
demo2.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0ansible-pilot $hosts: HOSTNAME`ansible-playbook PLAYBOOK`Using the `hosts` statement in the Ansible Playbook allows you to specify a host or a group of hosts for the execution.
The advantage is that is more reliable than manually specifying the hostname than using the `--limit` parameter from the command line.
The drawback is that you need to remember to edit the Ansible Playbook code every time. If you don’t you’re going to execute the code on the specified host, still a potential manual issue.
---
- name: harmful playbook
hosts: demo.example.com
tasks:
- name: harmful task
ansible.builtin.debug:
msg: "harmful task"ansible-pilot $ ansible-playbook -i limit/inventory limit/playbook2.ymlPLAY [harmful playbook] ***************************************************************************TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]TASK [harmful task] *******************************************************************************
ok: [demo.example.com] => {
"msg": "harmful task"
}PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0ansible-pilot $hosts: “{{ HOSTS }}”`ansible-playbook -e “HOSTS=demo.example.com” PLAYBOOK`Using the `hosts` statement in the Ansible Playbook allows you to specify also a variable that you could populate with a host or a group of host for the execution.
Basically, if you blindly execute the Ansible Playbook code on the command line is doing nothing. The only way is to process is to populate the host variable via an extra variable via the console line.
The advantage is that is more reliable than manually specifying the hostname than using the `--limit` parameter from the command line.
This option combines the advantages of the previous option and my favorite as well.
---
- name: harmful playbook
hosts: "{{ HOSTS }}"
tasks:
- name: harmful task
ansible.builtin.debug:
msg: "harmful task"ansible-pilot $ ansible-playbook -i limit/inventory -e "HOSTS=demo.example.com" limit/playbook3.ymlPLAY [harmful playbook] ***************************************************************************TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]TASK [harmful task] *******************************************************************************
ok: [demo.example.com] => {
"msg": "harmful task"
}PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0ansible-pilot $ansible-pilot $ ansible-playbook -i limit/inventory limit/playbook3.yml
[WARNING]: Could not match supplied host pattern, ignoring: HOSTSPLAY [harmful playbook] ***************************************************************************
skipping: no hosts matchedPLAY RECAP ****************************************************************************************ansible-pilot $Now you know the Three options to Safely Limit Ansible Playbooks Execution to a Single Machine that you could apply to your everyday journey based on your use case.
Subscribe to the YouTube channel, Medium, and Website to not miss the next episode of the Ansible Pilot.

Konstantinos PatronasIn this article i will show you how you can create self deleting files!, files that will be deleted them self after a defined number of…
Alexander Nguyen1-page. Well-formatted.
Vishal BarvaliyaIf you’ve spent any time writing SQL queries, you’ve probably seen both `COUNT(*)` and `COUNT(1)` used to count rows in a table. But what’s…
Axel Casas, PhD CandidateLearn programming faster and better
Ansible is an open-source automation and configuration management tool. It simplifies the process of deploying, configuring, and managing…
Omar ELFarsaouiPhoto by Uillian Vargas on Unsplash