🚀 Welcome, Pythoholic tribe! Let's explore the world of `ansible error handling` so we can implement `ansible troubleshooting` techniques, and delve into `ansible debugging` to ensure smooth `automation`. This `ansible tutorial for beginners` will touch on `ansible best practices` and how they fit into the broader `devops` landscape.
Dive deeper into AWS with our FREE courses and playlists: 🎯 AWS Certified Developer Associate Course: https://tinyurl.com/dvac01 🎯 AWS Solutions Architect Associate Certification Playlist: https://tinyurl.com/y4lr8zu4
Support our mission: 💙 INSTAMOJO: https://instamojo.com/@pythoholic 💙 PAYPAL: paypal.me/pythoholic 💙 Check out the tech I use and recommend: www.amazon.in/shop/pythoholic
Connect and follow for more learning resources: 🔔 Podcast (Blabber Cloud): http://tinyurl.com/Pythoholic 🔔 YouTube: tinyurl.com/PythoholicYT
🚨Disclaimer: The content provided on this channel is independent and not affiliated with any organization. Our videos are based on our knowledge and understanding of the topic, and we recommend viewers conduct their own research to gain a fuller understanding. We're always seeking to improve, and your feedback helps us do that! Support the channel for more content like this in the future.
Now let's check out a very important
part that is error handling in anible
and I feel you would also want to know
more about this. Hey everyone before we
dive into today's anible session I have
something exciting to share. I am
launching a complete mastering anible
for beginners course very soon and
there's a link in the description to
join my waiting list and uh you will get
early access exclusive discounts and be
notified the moment it goes live.
Welcome to the error handling part in
anible. So let's see how anvil manages
unexpected events and ensures the
robustness of your playbooks. So when we
speak about error handling by default
anvil aborts the play if it is
encounters
an error like a nonzero return code from
a command. But in real world we need
flexibility.
What if we want to handle that? Let's
learn how we can do that. So consider a
scenario where a non-critical command
fails. We don't want the entire playbook
to stop. So this is where we can make
use of ignore errors statement or
director. So this is our example of
ignored failures. Ignoring failure
command where we use ignore errors and
continue execution despite task failure.
So in this example we have a task that
is intentionally failing. The command
/bin/f false always returns a non-zero
status. But with ignore error colon true
or ignore error true anible moves on as
if nothing happened and using ignore
errors we can keep the playbook running
smoothly preventing a halt due to a
non-critical tasks failure. So this is a
very useful operation I now let's handle
scenarios where a host might be
temporarily unavailable. So let's
suppose you're working on deployment and
that host might be unreachable. So it
might happen but what if you want to
continue operations here with ignore
unreachable equals true we can allow
task to proceed even if anible can't
connect to the host. It is very useful
in environments where network
instability is expected or during phased
rollouts. So there might be a temporary
outage which would recover on its own.
So let's take the example here. So we
have got the same command which would
fail. So /bin / true. So with ignore
unreachable set, Ansible will not fail
the play if the host is down. Instead,
it skips over and continues executing
subsequent tasks. Now what if you need
more control over what constitutes a
failure? So this is where condition
failed when comes to play. This
condition failed when uh in this given
example, we are running a custom
command. So using failed when we assert
failures only if a certain condition in
example result are met. So it's like
detecting specific error text in
standard errors. So that is why we say
if error in example result dot std er
standard error. Uh so in this condition
failed when it checks if there is a
string error in the example result dot
standard error. So here we saw three
types of use cases of handling errors.
So one is ignore errors. to continue
execution despite task failure. Ignore
reachable or unreachable.
So to continue execution even if the
host is unreachable. And the third one
is using failed when. So where we
customize when a task is considered as
failure or failed. In anible we can
dictate when a task has made a change
using the changed when directive.
So let's take an example here to
consider a custom command execution. So
here we capture its result in another
variable which is named another result.
So then we use changed when to check the
return code. If it's non zero anible
marks the target as changed or the task
is changed. So this is a feature that
lets us decide when we want the task to
be considered as changed. Also in this
task we are using user/pin/ another
command. So the changed when directive
checks if another result that is stored
as a register variable. So another
result RC or return code is not equal to
zero. So if this condition is true, it
is considered as a significant change.
Next is using blocks for error handling.
Uh so how do we do that? So a block
encapsulate tasks that may fail. And
with a rescue section, we define what to
do if any task within the block actually
fails. So here's a task that is expected
to fail because we have written /bin/f
false. So upon failure the rescue
section here kicks in. So logging an
error message that is an error
encountered. So and rescue blocks
specify tasks to run when an earlier
task in a block fails. So even though
there are multiple tasks in block the
rescue comes into action if any task in
a block fails. So this approach is
similar to exception handling in many
programming languages. You might have
already used plans only runs rescue
block after a task returns a failed
state. But bad task definitions and
unreachable host will not trigger the
rescue block. So that's something we
need to keep in mind. Now let's get back
to this and do a recap on what we have
learned so far with our use cases with
error handling with anible. So the first
one that we have here is by setting
ignore errors equals true. So we
instruct Ansible to continue execution
even when a command like dfenh/mount/
special when when a command like df on a
potentially unavailable mount fails or
when a ping to a backup server does not
execute or does not succeed. And the
second one that we have here is when we
encounter where the situation is like
where commands may fail or host could be
unreachable. So in anible we have the
option to proceed despite errors that is
with ignore unreachable as true. Now
let's move on to the custom failure
section with failed when.
So this allows us to consider a task as
failed based on a specific condition in
our commands output.
Similarly, changed when lets us declare
a task as having made changes. So this
is crucial for tasks that modify the
system like running a cleanup script.
When you look at the example playbook
here or the example play here in this
database backup task failed when is used
to check the output of a specific
failure message
that is backup failed. So if backup
failed is inside backup result std out
it is considered as a failed task.
Meanwhile, the cleanup task that you see
here uses changed when to confirm a
change was made using the same principle
of checking a string in the error
standard output to decide if it is
changed or not. Now the last one is
using blocks for error handling. So look
at this playbook or the play that we
have here. So this playbook is to update
and restart a service. And we have a
block with two tasks.
There's the first task update config
file. The second task is restart
service. The first one is basically to
update config file where we copy the new
configuration to etc/ service conf and
once it is copied uh that is if the task
is successful we want to restart the
service and if any task within the block
fails such as updating the config file
or restarting the service anible moves
to the rescue section when we where we
actually send an alert email with the
subject service update failed. build.
So I hope you're getting the point here.
We have discussed a lot of examples and
I'm sure you have learned a lot here. So
let's move on.
Now let's move on to something that is
used in most of the playbooks that is a
feature with an which helps in setting
remote environment. So this feature is
critical for tasks that rely on specific
environmental configurations. So let's
not just talk about definition but let's
consider a scenario where a package must
be installed via corporate proxy and for
this anible allows you to define an
environment directly at the task level.
So what is this environment and how we
are going to do this here is a sample
code.
So what we doing here? So we have a task
that installs some packages or some
package to use this the environment
directive that you see here is included
with the task specifying the necessary
proxy settings directly.
So this ensures that the task uses the
proxy configuration for its uh HTTP
request and for this we make use of the
environment directive or statement which
HTTP proxy parameter with the actual
company proxy value that is
HTTP/proxy.comp.com
8080. So this is just an example don't
take it literally I'm just giving an
example here. Next, we might have a
scenario when for multiple tasks that uh
require the same proxy configuration and
in that case it's efficient to define
environment variables at the play level.
So have a look at the playbook here. The
proxy envariable
is defined once and it is reused
across multiple tasks in task one and in
task two as well.
So by using proxy env ginder 2 syntax we
reference the defined variable
proxy env which actually anible resolves
at the runtime. So with the variable
proxy env which holds the proxy value
all the tasks use that same proxy value
but the advantage here is that we define
it once but use it multiple times in
environment variables.