- In this video, I want to tell you about Register.
Register is a nice way to automatically define
your variables based on command result.
So the thing of Register is that you can use it
to store the result of a task, in a variable.
It provides a very nice way to perform specific tasks
only upon success or failure of another task.
That means that it'll become really useful
once we have talked about conditionals.
But, as Register is about a way to create variables,
I'd like to have a look at a quick example.
So here is register.yaml and what do we have?
Well, a very simple playbook.
In this playbook we are using
the shell command to run a command.
Notice that there's no real reason to use shell here,
could have used command as well.
What it's all about, is register.
Register stores the result of the shell module
in a variable with an passwd_contents
And next in debug, we are going to check
this passwd_contents value.
So ansible-playbook on register.yaml
is doing this, it's gathering the facts.
And there we go. Here we have the result of the register.
So register defines its variable on every single host.
So basically, it becomes a property of the host.
And in this host, we can see that it contains
detailed information about the success
or failure of the exam.
So we can see whether or not it changed.
We can see it's, whether or not it's failed.
We can see rc for the return code,
which is the result of the command.
We can see when it started.
The stdout put, the stdout put in lines.
And a nice thing is, you can address all of them
like passwd_contents.rc is going to print the return code.
You know what? Let's tweak this playbook a little bit.
Now let me show you debug, msg,
the status of the command is {{ passwd_contents.rc }}
Notice, by the way, that there's no real reason
why double quotes I used here.
You might as well omit them, then it'll also work.
So there we go, the status of the command is zero.
Note by the way, that I'm using passwd_contents.rc
and in case you are thinking, "Hey, (indistinct),
"is that the right way of doing it?"
Then I would say, "Good point!"
This is how you should be doing it.
Square bracket, single quote notation.
But the doted notation is so common
that you will find it still, every now and then.