As we start moving into our state modules and state files,
we have a completely different set of modules that are used with our
state files compared to remote execution commands.
State files themselves allow for the desired state of the system to be declared.
We've seen this before. We've already mentioned our Ansible
Playbooks, our Puppet manifests. By default, the state files,
though, should be created in a certain location.
The default location,
whether we're talking about the server or in a masterless
environment on our minion, should be in /srv/salt.
The state files use the .sls extension.
When looking at creating our state file,
we can make sure that we've got the correct directory. Now, of
course, we've got to create that directory as root.
We can also set our modeline in our .vimrc file within our home directory.
The idea of this is that we can set configuration options for vim inside of our
state files if they are not recognized. To set our modeline,
we can set it as the first line of the common.sls.
We're creating this for the moment in our home directory. We'll
move it later. But the modeline can then specify how vim should
deal with the file. We're specifying that it should be the file
type of YAML. Of course,
a .sls extension is not going to be associated with YAML normally. TS=2 is
just setting our tab stop, so every time we hit the Tab key, we'll move 2
spaces. Auto indent, so when we hit Return,
we return to the last indent level. Sw, our shift width, is set to 2.
That's the same as our tab stop, and it is used by auto intent. Et for expand
tabs, so we store tabs as spaces. And then we've got our cursor line and
cursor column to highlight where we're working with. And all of that can be
echoed out and replacing the common.sls, which probably won't exist at the
moment, but will be set then as our first line. Once we've got that, we can
then go through and edit it.
When we look at editing it, we're going to be adding in a couple of states.
We've got our install_common_packages state where we're using pkg.installed.
And we've got a list of packages, and we're just putting here rsync,
vim‑data, and curl. We've got another state looking at something slightly
different using the timezone.system and setting then our hardware clock to
UTC time and making sure, though, the display time is from the time zone of
Europe and London. Obviously, you can set it to your own time zone.
Once we've configured that, we can then go ahead and
apply the state file. So we can copy across the
common.sls into our /srv/salt directory.
We can then use the state.sls remote execution module and function,
apply the name, but not the suffix of the file. And if we want to test,
we can just have test‑True so there is no operation.
If we're happy with the test,
we can go ahead and execute it. Now, this is where we really do need to make
sure that we can create these state files because these state files mean that
you can have repeatably correct configurations. Come on, let's go through and find a command line.