How to send commands and states to Linux minions from the Salt controller

How to send commands and states to Linux minions from the Salt controller

Jack Wallen shows you SaltStack can be used to more easily manage the configuration of numerous servers.

system-admin-evgeniyshkolenko-copy.jpg

Image: iStock/EvgeniyShkolenko

Must-read cloud

In a previous how-to, I demonstrated how to get SaltStack installed on a controller and connect it to a minion (see: How to deploy the open-source SaltStack for automated server configuration and management). With SaltStack up and running, it is then possible to send commands to your minions. This can be used within your on-premise or cloud-hosted data center. 

For example, say you have several minions that will host websites and you need to get NGINX installed on them. With SaltStack you can install that web server on every connected minion with a single command.

For any administrator, having such power can seriously turn a ridiculously busy day into one that’s far more manageable. Instead of having to go around to every one of those servers and install NGINX manually, you can do it from a single terminal interface. That’s the power of SaltStack.

Let me show you how this is done.

SEE: Kubernetes: A cheat sheet (free PDF) (TechRepublic) 

What you’ll need

To make this work, you’ll need a SaltController and at least one minion configured. You can have as many minions as needed. Make sure you’ve read through the tutorial above to get the controller and minion(s) deployed first. 

How to check disk space usage

The first thing we’ll do is check disk space usage on all of our minions (in my case that’s only one). The way SaltStack works is that you run salt and instruct it to issue specific commands on the minions. Remember, in the previous article we tested the minion connection using ping like so:

sudo salt UBUNTUSERVER test.ping

In the above command, UBUNTUSERVER is the name of the minion, and test.ping is the command to run on the minion. 

Now that we know our controller and minion are still seeing one another, let’s find out what the disk space usage is on the minion with the command:

sudo salt UBUNTUSERVER disk.usage

The above command will report something like:

UBUNTUSERVER: ---------- /: ---------- 1K-blocks: 102684600 available: 90923280 capacity: 7% filesystem: /dev/sda2 used: 6502212 /dev: ---------- 1K-blocks: 1488292 available: 1488292 capacity: 0% filesystem: udev used: 0 /dev/shm: ---------- 1K-blocks: 1532172 available: 1531848 capacity: 1% filesystem: tmpfs used: 324 …

There will be considerably more output, but I’ve snipped it to save space. If you have more than one minion connected, you could view the disk space usage of every one of them with the command:

sudo salt '*' disk.usage

You can also send a compound command to your minions. These commands are in a comma-delimited list like so:

sudo salt '*' cmd.run,test.ping,test.echo 'cat /proc/cpuinfo',,"Hello TechRepublic!"

The above would run three commands:

cmd.run
test.ping
test.echo

Where it gets tricky is with input for each command. Both cmd.run and test.echo require input, whereas test.ping does not. The commands are listed first and then the input for each command is listed next. Each section is delineated by commas, which is why the input section appears to have two commas side by side. This is because:

  • ‘cat /proc/cpuinfo’ is the input for cmd.run

  • test.ping doesn’t require input

  • “Hello TechRepublic!” is the input for test.echo

How to install software on a minion

Now, let’s install NGINX on our minion. For this we use the pkg.install command like so:

sudo salt UBUNTUSERVER pkg.install nginx

Let’s say you have 100 minions connected to your controller, but only five of those minions will be web servers. You could issue the command:

sudo salt web1,web2,web3,web4,web5 pkg.install nginx

The output of the above command will include:

UBUNTUSERVER: ---------- … … nginx: ---------- new: 1.18.0-0ubuntu1 old: nginx-common: ---------- new: 1.18.0-0ubuntu1 old: nginx-core: ---------- new: 1.18.0-0ubuntu1 old:

You should now be able to point a web browser to the IP address of any one of those web servers and see the NGINX welcome page. Or, you could check it by issuing the command:

sudo salt web1,web2,web3,web4,web5 cmd.run 'ls -l /var/www/html'

The output of the above command will include:

index.nginx-debian.html

How to apply a Salt state

Let’s say, you want those NGINX servers to include rsync and curl. That means you want those servers to be in a certain state. To do that, you apply a salt state. For this, you create an .sls file that would include the packages to be installed. For our state, we’re going to make sure NGINX, rsync, and curl are installed. 

Before we do this, create a new directory with the command:

sudo mkdir /srv/salt

Now, create the .sls file with the command:

sudo nano /srv/salt/webserver_setup.sls

In the file, paste the following:

network_utilities: pkg.installed: - pkgs: - rsync - curl
nginx_pkg: pkg.installed: - name: nginx
nginx_service: service.running: - name: nginx - enable: True - require: - pkg: nginx_pkg

Save and close the file.

To apply the state to your web server minions, you’d issue the command:

sudo salt web1,web2,web3,web4,web5 state.apply webserver_setup

The state would be applied and five web servers would now have the required packages installed.

And that’s the gist of sending commands and applying states to Linux minions from the Salt controller. With this ability, your administration job is made exponentially easier.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

Also see

Source of Article