Book Image

Learning SaltStack

By : Colton Myers
Book Image

Learning SaltStack

By: Colton Myers

Overview of this book

Table of Contents (15 chapters)

A game of ping pong

Here's our first command:

# sudo salt '*'

Was that a bit underwhelming?

Don't worry. We're going to get to the more impressive stuff soon enough. The command we just ran was called a remote execution command. Basically, we sent a message to all (one) of our minions and told them to run a function from one of the execution modules that is built into Salt. In this case, we just told our minion to return True. It's a good way to check which of our minions are alive. We will explore the various parts of this command in more detail in the next chapter.

The test module actually has a few other useful functions. To find out about them, we're actually going to use another module, called sys, as follows:

# sudo salt 'myminion' sys.list_functions test
    - test.arg
    - test.arg_repr
    - test.arg_type
    - test.collatz
    - test.conf_test
    - test.cross_test
    - test.echo
    - test.exception
    - test.fib
    - test.get_opts
    - test.kwarg
    - test.not_loaded
    - test.opts_pkg
    - test.outputter
    - test.provider
    - test.providers
    - test.rand_sleep
    - test.rand_str
    - test.retcode
    - test.sleep
    - test.stack
    - test.tty
    - test.version
    - test.versions_information
    - test.versions_report

Let's try one of the other functions on the list. Maybe test.fib:

# sudo salt '*' test.fib
    TypeError encountered executing test.fib: fib() takes exactly 1 argument (0 given). See debug log for more info.  Possibly a missing arguments issue:  ArgSpec(args=['num'], varargs=None, keywords=None, defaults=None)

Well, that didn't work. To find out more information about a function, including examples of how to use it, we can use the sys.doc function, as follows:

# sudo salt '*' sys.doc test.fib

    Return a Fibonacci sequence up to the passed number, and the timeit took to compute in seconds. Used for performance tests

    CLI Example:

    salt '*' test.fib 3

Aha! We need to give it a number to which it should calculate the Fibonacci sequence, as follows:

# sudo salt '*' test.fib 30
      - 0
      - 1
      - 1
      - 2
      - 3
      - 5
      - 8
      - 13
      - 21
    - 1.09672546387e-05

As it turns out, the Fibonacci sequence is not very hard for computers to calculate quickly.

Note that you can actually use sys.doc to retrieve the documentation for a whole module's worth of functions at a time, as follows:

# sudo salt '*' sys.doc test

I didn't include the output as it is lengthy.

The sys module is going to be one of the most useful modules in your quest to learn Salt. Keep it handy and turn to it any time you want to learn more about something you're working with. Remember that the sys module can target itself. The following code shows you how to use the sys module:

# sudo salt '*' sys.list_functions sys
    - sys.argspec
    - sys.doc
    - sys.list_functions
    - sys.list_modules
    - sys.list_returner_functions
    - sys.list_returners
    - sys.list_runner_functions
    - sys.list_runners
    - sys.list_state_functions
    - sys.list_state_modules
    - sys.reload_modules
    - sys.returner_doc
    - sys.runner_doc
    - sys.state_doc

We are going to discuss remote execution and the execution modules in much greater detail in the next chapter.