So delegation allows you to run tasks on other hosts.
And in this demo,
we're going to be using to delegate too so we can run individual
tasks on different hosts without starting a new play.
So in this playbook,
we're going to install and start an enable httpd on host rocky,
and we'll install the package and then we'll test access.
So here we're going to delegate to localhost,
and notice that this is specified at the end of the playbook.
So when we run the playbook, the tasks are executed with success,
but there is just a failure at the, at the testing http access,
but this has nothing to do with Ansible,
it's a connection refused error.
If we look at the task,
you can actually see that the delegation process from
rocky1 to localhost has actually completed.
However,
let's go a little bit deeper into troubleshooting and
investigate further by adding some verbosity.
Let us investigate a little bit further,
and we can do this by running the playbook and adding some additional arguments
to let us know exactly what is happening in the background.
So I'm going to run this playbook again,
but I'm going to increase the verbosity level.
So let me scroll up just to the task that has failed and
this is where we're testing httpd access.
Well as you can see that the delegation has actually worked perfectly fine
and we're establishing a connection from the localhost we explicitly set in
the playbook with the delegate to command.
So I want to look at this from a different angle,
and I want to copy files with the new delegation
instead of localhost test services.
So in this example,
we're going to copy files and we're going to be
delegating this to one of the managed hosts,
which is rocky2, so rocky2 is now our delegation host and not the localhost.
So here you would also notice that there has been a failed task,
but if we scroll up to the task,
we'll see that the delegation has actually worked.
You may have recalled in this playbook that we're using the synchronized model,
but one of the requirements to use a synchronized model is
that we need to have rsync installed, which I don't have.
So as a quick fix just to finish off this demo,
let's change the playbook around a little bit,
and here we're going to use the copy module,
instead of the synchronization module,
which doesn't actually have the requirement to have rsync installed.
So here you can see that the delegation has worked perfectly fine,
and we have delegated from rocky1 to rocky2.
So just out of curiosity, let's run this playbook again and add some verbosity.
As you can see once we scroll up to the content,
we can see that this has been delegated to rocky2.
Now if you recall from the very start of the demo when we scrolled up,
we could see that this was delegated to localhost.