Puppet Boltを動かす

## 参考

* [Installing Bolt - Bolt latest | Puppet](https://puppet.com/docs/bolt/latest/bolt_installing.html)
* [Bolt Hands-on Lab | Bolt](https://puppetlabs.github.io/bolt/)

## インストール

```sh
yum install -y \
  "https://yum.puppet.com/puppet-tools-release-el-8.noarch.rpm"

yum install -y puppet-bolt
```

## boltの使い方

```go:コマンド一覧
$ bolt
Usage: bolt <subcommand> <action>

Available subcommands:
  bolt command run <command>       Run a command remotely
  bolt file upload <src> <dest>    Upload a local file or directory
  bolt script run <script>         Upload a local script and run it remotely
  bolt task show                   Show list of available tasks
  bolt task show <task>            Show documentation for task
  bolt task run <task> [params]    Run a Puppet task
  bolt plan convert <plan_path>    Convert a YAML plan to a Puppet plan
  bolt plan show                   Show list of available plans
  bolt plan show <plan>            Show details for plan
  bolt plan run <plan> [params]    Run a Puppet task plan
  bolt apply <manifest>            Apply Puppet manifest code
  bolt puppetfile install          Install modules from a Puppetfile into a Boltdir
  bolt puppetfile show-modules     List modules available to Bolt
  bolt secret createkeys           Create new encryption keys
  bolt secret encrypt <plaintext>  Encrypt a value
  bolt secret decrypt <encrypted>  Decrypt a value
  bolt inventory show              Show the list of targets an action would run on
  bolt group show                  Show the list of groups in the inventory
  bolt project init                Create a new Bolt project

Run `bolt <subcommand> --help` to view specific examples.

Available options are:

Global:
    -h, --help                       Display help
        --version                    Display the version
        --debug                      Display debug logging
```

### bolt task

```go:task一覧
$ bolt task show
aws_inventory::resolve_reference     Generate targets from AWS EC2 instances
azure_inventory::resolve_reference   Generate targets from Azure VMs
facts                                Gather system facts
package                              Manage and inspect the state of packages
puppet_agent::install                Install the Puppet agent package
puppet_agent::version                Get the version of the Puppet agent package installed. Returns nothing if none present.
puppet_conf                          Inspect puppet agent configuration settings
reboot                               Reboots a machine
reboot::last_boot_time               Gets the last boot time of a Linux or Windows system
service                              Manage and inspect the state of services
terraform::resolve_reference         Generate targets from Terraform Statefile
vault::resolve_reference             Set config values by accessing secrets in Hashicorp Vault

MODULEPATH:
/root/.puppetlabs/bolt/modules:/root/.puppetlabs/bolt/site-modules:/root/.puppetlabs/bolt/site

Use `bolt task show <task-name>` to view details and parameters for a specific task.
```

* /root/.puppetlabs/bolt/site-modules/ にtaskを置けば良いことがわかる。

```sh
mkdir -p /root/.puppetlabs/bolt/site-modules/exercise5/tasks/
```

```sh:/root/.puppetlabs/bolt/site-modules/exercise5/tasks/init.sh
#!/bin/sh

echo $(hostname) received the message: $PT_message
```

```sh:実行
# $PT_message には hello が入る。
bolt task run exercise5 message=hello --nodes localhost
```

### bolt script

```sh
bolt script run exercise5/tasks/init.sh --nodes localhost
```


### メモ: docker でも実行ができる

```sh
# コンテナ内の /etc/os-release が表示される例
docker run puppet/puppet-bolt \
 command run 'cat /etc/os-release' \
 -t localhost
```