- So we have already talked a little bit about register.
Register is particularly useful in combination
with when statements.
Let's go check it out.
So you can use register to store the output of a command
and address it as a variable.
And next, you can use the result of the command
in a conditional or in a loop,
and that's exactly what I want to show you
in the next couple of examples.
So the first example is command output test
and we are going to run that on ansible1.
So what are we going to do?
Well, the command that we are running is vgs centos.
So this is running vgs, that's a Linux command,
that checks if volume groups are available
and it is running vgs on centos.
So this command will give you some useful information
if the volume group exists,
and if it doesn't exist it'll give you other information.
In order to work with it,
we register the complete output of the command
in a variable, and this variable is vgout.
I do want this playbook to continue
even if there is no result
and that is why you can see ignore_errors is true.
Notice that ignore_errors can be set in a play header,
can also be set in a task header.
Here it has a task scope.
So only for this task, if it has an error, we continue.
If all the tasks have an error
then the playbook execution will stop
and produce a normal error behavior that we've seen before.
Next, I'm using show variable value.
I like using debug especially if you are using register.
Register can give you results
that you would never have expected before.
So it's a wise idea to go check out
what exactly register is giving you.
So we have debug var vgout.
It'll show you the values that are in the register variable.
And then, we are going to print a message
and we print a message.
Again, we see debug.
The message is vg does not exist
when "not found" in vgout.standard error.
So what's happening here?
We are looking for the string text "not found"
in the registered variable vgout.standard error.
A notice by the way that in a when statement,
you do not have to use the curly braces
to refer to the variables.
It's a little bit confusing, but it's just the rule
when using variables in when statements.
Let's go see how this is working.
So here we can see the fact gathering.
You can see that the test for the VG existence is failing.
And then, you can see that basically
everything that you see right here
is in a nicely printed format in the variable vgout.
So vgout is showing you the commands.
It is showing you the standard out.
Oh, there is no standard out.
It is showing you the standard error,
nicely formatted in lines
and the standard error all by itself.
And then, we get the message "vg does not exist".
Now, let's rewrite this playbook
to the situation where the volume group does exist.
So quick command on ansible1 -a vgs.
And there, we can see that the name
of the volume group is rl,
rl for Rocky Linux.
So this time the command is vgs rl.
There we go.
We run it again.
And, we can see this is kind of curious,
test for VG existence.
I'm learning a command
and nevertheless I'm getting changed,
that's one of the weird artifacts
of using the command model.
We'll talk about it later.
There's a change to when statement
that you can use to influence that.
Here we can see the command.
Here we can see the standard out
and the standard out actually does contain rl.
There we go.
We're looking for the string
or the partial string to appear in the standard out.
So the print methods can be skipped.
I have another example which is register_command.
So what are we doing here?
We are prompting for a username.
Oh, if we are prompting for a username
then definitely I want private to be set to no
because by default vars_prompt will not show the results
of what the user is typing,
that's not very user friendly
and there's no reason to make this username private.
So let's show it.
Then, we are using the shell command cat/etc/passwd
and we are register the output of that command
on passwd_contents.
Next, we have the debug module again on passwd_contents.
And then,
we are printing "passwd contains user {{username}}".
So that's the username that was created in vars_prompt.
And then, we have the when statement
and the when statement is a curious one,
passwd_content.stdout.
That is what we are looking for.
And next, we are using the Python function find username.
This is Python.
This is a Python command
that is used on the standard out on the register variable.
You can see that because username between braces,
that's one notation of variables we haven't seen before,
but the username between braces
that's the Python way to refer to a variable
and we look for non equals minus one.
Hey, what is this is minus one?
Well, you should know that if this Python find function
will find the username,
it'll give a byte offset
like 386 if the user was find starting at byte 386.
And if user was not found,
then it'll give a minus one.
We want the message to print "passwd contains user"
when we get a byte offset.
We don't know the byte offset beforehand
but we do know if the user was found,
we do not see minus one
and that is what's happening right here.
So let's run it.
Let's first run it on user, user ansible,
that's a user that does exist.
And there we can see, we get passwd contains user ansible.
Of course it contains user ansible.
Just for your convenience,
we can see the standard out,
nicely formatted in lines
which makes it a little bit more readable.
There we have the lines
and we can see all the other details
that is just interesting to check it up.
But this debug of the entire registered variable
obviously has no real function.
The only function is to clarify
to yourself what is happening.
Now, let me use it again with a user that does not exist
and there we get a skipping and a skipping.
It's still printing the entire variable
that is because we are looking at the register okay?
The register is just printing the output of etc/passwd
and we don't do anything conditional on that.
But here in this last task in the playbook,
let me show you again, in this last task in the playbook,
the when statement is find(username)
should not return a minus one.
Well, Caroline doesn't exist so we do get a minus one,
and that is why we get a skipping,
and that is how you can use register.
Register is an awesome command,
and you are likely going to use it a lot
when working with ansible.