てくなべ (tekunabe)

ansible / network automation / 学習メモ

[Bolt] エージェントレスなインフラ自動化ツール「Puppet Bolt」かんたんチュートリアル

■ はじめに

Puppet Bolt はエージェントレスなインフラ自動化ツール

Ansible や Chef などとよく比較される Puppet という構成管理ツールがあります。 Puppet といえばエージェント型のイメージがありますが、2018年10月にエージェントレス型の別ツール「Puppet Bolt(以下、Bolt)1.0」がリリースされました。

puppet.com

thinkit.co.jp

Bolt は、サーバーに対してパッケージのインストールやサービスの起動などの設定作業を自動化できます。エージェントレスという点では、Ansible に近いかも知れません。なお、ネットワーク機器に対応しているかどうかは調査できていません。

インストールは Linux にも Windows にもでき、管理対象としても Linux(SSH接続) にも Windows(WinRM接続) に対応していうようです。

この記事では、私が試したインストール方法や簡単な使い方や、補足情報をチュートリアルとしてご紹介します。

主な内容

  • インストール
  • 単純なリモートコマンドの実行(bolt command run
  • スクリプトを指定して実行する(bolt script run
  • 「タスク」を指定して実行する(bolt task run
  • 「プラン」を指定して実行する(bolt plan run

環境

  • Bolt 1.6.0
  • Bolt インストール先: CentOS 7.5.1804
  • 管理対象: CentOS 7.5.1804


■ 用語の整理

チュートリアルに入る前に、今後出てくる用語の整理です。 少し無理やりなところもありますが、Bolt の用語と Ansible の用語を対応させてみました。

Bolt Ansibe 説明
inventory invnetory 接続先のホスト情報を記載するもの
command ansible コマンドで shellcommand モジュールを利用する アドホックなコマンド実行
task module 特定の機能を持つもの
plan Playbook 一連処理、定義を記述したもの




■ インストール

各種パッケージマネージャーを利用してインストールします。

RHEL の場合

RHEL 6

sudo rpm -Uvh https://yum.puppet.com/puppet6/puppet6-release-el-6.noarch.rpm
sudo yum install puppet-bolt

RHEL 7

今回はこちらの方法を利用しました。

sudo rpm -Uvh https://yum.puppet.com/puppet6/puppet6-release-el-7.noarch.rpm
sudo yum install puppet-bolt

詳細は「Install Bolt on RHEL or SLES」を参照してください。

Windows の場合

Bolt は Windows にも対応していて、MSI や Chocolatey でインストールできます。 詳細は「install Bolt on Windows」を参照してください。

その他

その他、macOS や他の方法のインストールは「Installing Bolt」を参照してください。

他のプラットフォーム向けのインストール手順は以下のページに記載されています。 https://puppet.com/docs/bolt/1.x/bolt_installing.html

インストールできたことを確認するため、バージョンを表示してみます。

$ bolt --version
1.6.0

今回は 1.6.0 というバージョンがインストールされました。

bolt help を実行すると、ヘルプが表示されます。

$ bolt help
Usage: bolt <subcommand> <action> [options]

Available subcommands:
  bolt command run <command>       Run a command remotely
  bolt file upload <src> <dest>    Upload a local file
  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 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

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

where [options] are:
    -n, --nodes NODES                Identifies the nodes to target.
                                     Enter a comma-separated list of node URIs or group names.
                                     Or read a node list from an input file '@<file>' or stdin '-'.
                                     Example: --nodes localhost,node_group,ssh://nix.com:23,winrm://windows.puppet.com
                                     URI format is [protocol://]host[:port]
                                     SSH is the default protocol; may be ssh, winrm, pcp, local, docker, remote
                                     For Windows nodes, specify the winrm:// protocol if it has not be configured
                                     For SSH, port defaults to `22`
                                     For WinRM, port defaults to `5985` or `5986` based on the --[no-]ssl setting
    -q, --query QUERY                Query PuppetDB to determine the targets
        --noop                       Execute a task that supports it in noop mode
        --description DESCRIPTION    Description to use for the job
        --params PARAMETERS          Parameters to a task or plan as json, a json file '@<file>', or on stdin '-'
Authentication:
    -u, --user USER                  User to authenticate as
    -p, --password [PASSWORD]        Password to authenticate with. Omit the value to prompt for the password.
        --private-key KEY            Private ssh key to authenticate with
        --[no-]host-key-check        Check host keys with SSH
        --[no-]ssl                   Use SSL with WinRM
        --[no-]ssl-verify            Verify remote host SSL certificate with WinRM
Escalation:
        --run-as USER                User to run as using privilege escalation
        --sudo-password [PASSWORD]   Password for privilege escalation. Omit the value to prompt for the password.
Run context:
    -c, --concurrency CONCURRENCY    Maximum number of simultaneous connections (default: 100)
        --compile-concurrency CONCURRENCY
                                     Maximum number of simultaneous manifest block compiles (default: number of cores)
        --modulepath MODULES         List of directories containing modules, separated by ':'
        --boltdir FILEPATH           Specify what Boltdir to load config from (default: autodiscovered from current working dir)
        --configfile FILEPATH        Specify where to load config from (default: ~/.puppetlabs/bolt/bolt.yaml)
        --inventoryfile FILEPATH     Specify where to load inventory from (default: ~/.puppetlabs/bolt/inventory.yaml)
Transports:
        --transport TRANSPORT        Specify a default transport: ssh, winrm, pcp, local, docker, remote
        --connect-timeout TIMEOUT    Connection timeout (defaults vary)
        --[no-]tty                   Request a pseudo TTY on nodes that support it
        --tmpdir DIR                 The directory to upload and execute temporary files on the target
Display:
        --format FORMAT              Output format to use: human or json
        --[no-]color                 Whether to show output in color
    -h, --help                       Display help
        --verbose                    Display verbose logging
        --debug                      Display debug logging
        --trace                      Display error stack traces
        --version                    Display the version




■ 単純なリモートコマンドの実行(bolt command run

リモートホストに対して、一度きりの単純なコマンドを実行してみましょう。ここでは hostname を実行します。

  • 実行コマンド
bolt command run "hostname" -n 172.16.0.10 -u vagrant --no-host-key-check -p

各オプションの意味は以下の通りです。

オプション 説明 今回の指定値
command run リモート実行するコマンドを指定するサブコマンド hostname
-n (--nodes) リモートホスト 172.16.0.10
-u (--user) リモートユーザー名 vagrant
--no-host-key-check SSHホストキーのチェックを無効
-p (--password) リモートユーザーパスワード (指定なし、プロンプトで入力)
  • 実行結果
$ bolt command run "hostname" -n 172.16.0.10 -u vagrant --no-host-key-check -p
Please enter your password: (パスワードを入力)
Started on 172.16.0.10...
Finished on 172.16.0.10:
  STDOUT:
    testsv    ← リモートホストで hostname コマンドを実行した結果
Successful on 1 node: 172.16.0.10
Ran on 1 node in 0.67 seconds

補足

リモートホストを指定する -n(--nodes) とトランスポート

-n 172.16.0.10,172.16.0.11 のようにカンマ区切りにすると複数を対象にできます。また、インベントリファイルによって、ホスト情報や接続方式を管理することもできるようです。 もしWindows を対象に WinRM で接続する場合は 、以下のよ

  • -n winrm://winsv01 のようにスキームでトランスポートオプションを指定する
  • -n winsv01 --transport winrm のように、 --tranport オプションで指定する

-n オプションの詳細はこちら

トランスポートは以下の4つから選択できます。 - ssh (デフォルト) - winrm - local - docker

その他のオプション

上記で利用したオプションの他にも、秘密鍵の指定(--private-key)や、権限昇格の指定(--run-as)などのオプションもあります。 デバッグ表示する --debug も便利です。 すべてのオプションについては bolt help コマンド結果のヘルプを参照してください。

対応する公式ドキュメント


スクリプトを指定して実行する(bolt script run

Bolt は、先程のように短いコマンドの実行だけでなく、予め用意したスクリプトを指定してリモート上で実行することもできます。 ここでは、myscript.sh という簡単なシェルスクリプトを用意して、bolt 経由でリモート上で実行します。

#!/bin/bash
echo "Hello, Bolt! on (`hostname`)"
  • 実行コマンド
bolt script run myscript.sh -n 172.16.0.10 -u vagrant --no-host-key-check -p
  • オプション説明
オプション 説明 今回の指定値
script run リモート実行するスクリプトを指定するサブコマンド myscript.sh

※その他は bolt command run 実行時と同じ

  • 実行結果
$ bolt script run myscript.sh -n 172.16.0.10 -u vagrant --no-host-key-check -p
Please enter your password: (パスワードを入力)
Started on 172.16.0.10...
Finished on 172.16.0.10:
  STDOUT:
    Hello, Bolt! (on testsv)  ← リモートホストで myscript.sh を実行した結果
Successful on 1 node: 172.16.0.10
Ran on 1 node in 0.68 seconds

今回は、シェルスクリプトを指定しましたが、 Python や、PowerShell(リモートが Windows の場合)でもよいようです。

補足

対応する公式ドキュメント




■ 「タスク」を指定して実行する(bolt task run

今度はタスクの実行です。タスクとは特定の機能をも持つ機能単位で、モジュール名::タスク名 のように指定します。タスク名を省略して モジュール名 のように指定した場合は、 init という名前のタスクが呼ばれます。 さらに、実行するタスクにが利用するオプションと値を必要に応じて指定します。 例えば、service::linux name="postfix" action="stop" のようなかたちです。

(どのようなモジュール、タスクがあるのかは調べきれておりません・・。独自追加もできるようです。)

タスク実行その1: サービスの制御

ここでは、リモートホストhttpd サービスを停止するタスクを実行します。

  • 実行コマンド
bolt task run service::linux name="postfix" action="stop" -n 172.16.0.10 -u vagrant --no-host-key-check -p --run-as root
  • オプション説明
オプション 説明 今回の指定値
task run タスクを指定するサブコマンド service::linux
name タスク service::linux で利用するサービス名 posffix
action タスク service::linux で利用するアクション名 stop
--run-as su 先ユーザー root

※その他は bolt command run 実行時と同じ

  • 実行結果
$ bolt task run service::linux name="postfix" action="stop" -n 172.16.0.10 -u vagrant --no-host-key-check -p --run-as root
Please enter your password: (パスワードを入力)
Started on 172.16.0.10...
Finished on 172.16.0.10:
  {
    "status": "postfix stop"
  }
Successful on 1 node: 172.16.0.10
Ran on 1 node in 1.51 seconds

タスク実行その2: パッケージのインストール

今度は、linux にパッケージをインストールしてみます。利用するタスクは package::linux です。

$ bolt task run package::linux action=install name=wget -n 172.16.0.10 -u vagrant --no-host-key-check -p --run-as root
  • オプション説明 | オプション | 説明 | 今回の指定値 | |---|---|--| | task run | タスクを指定するサブコマンド| package::linux | | name | タスク package::linux で利用するサービス名 | install | | action | タスク package::linux で利用するアクション名 | wget |
$ bolt task run package::linux action=install name=wget -n 172.16.0.10 -u vagrant --no-host-key-check -p --run-as root
Please enter your password:
Started on 172.16.0.10...
Finished on 172.16.0.10:
  {
    "status": "wget install"
  }
Successful on 1 node: 172.16.0.10
Ran on 1 node in 1.97 seconds

補足

モジュールやタスクのオプションなどの情報

タスクの一覧は bolt task show コマンド で確認できます。

  • タスク一覧の表示
$ bolt task show
facts                              Gather system facts
facts::bash                        Gather system facts using bash
facts::powershell                  Gather system facts using powershell
facts::ruby                        Gather system facts using ruby and facter
package                            Manage and inspect the state of packages
package::linux                     Manage the state of packages (without a puppet agent)
package::windows                   Manage the state of packages (without a puppet agent)
puppet_agent::install              Install the Puppet agent package
puppet_agent::install_powershell
puppet_agent::install_shell
puppet_agent::version              Get the version of the Puppet agent package installed. Returns nothing if nonepresent.
puppet_agent::version_powershell
puppet_agent::version_shell
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
service::linux                     Manage the state of services (without a puppet agent)
service::windows                   Manage the state of Windows services (without a puppet agent)

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

各モジュール、タスクの情報は bolt task show モジュール名::タスク名 コマンド で確認できます。

  • service の情報
$ bolt task show service

service - Manage and inspect the state of services

USAGE:
bolt task run --nodes <node-name> service action=<value> name=<value> provider=<value>

PARAMETERS:
- action: Enum[start, stop, restart, enable, disable, status]
    The operation (start, stop, restart, enable, disable, status) to perform on the service
- name: String[1]
    The name of the service to operate on.
- provider: Optional[String[1]]
    The provider to use to manage or inspect the service, defaults to the system service manager

MODULE:
built-in module
  • service::linux の情報
$ bolt task show service::linux

service::linux - Manage the state of services (without a puppet agent)

USAGE:
bolt task run --nodes <node-name> service::linux action=<value> name=<value>

PARAMETERS:
- action: Enum[start, stop, restart]
    The operation (start, stop) to perform on the service
- name: String[1]
    The name of the service to operate on.

MODULE:
built-in module

対応する公式ドキュメント




■ 「プラン」を指定して実行する(bolt plan run

最後に、プランの実行を試します。プランとは、コマンドやタスクなどを組み合わせて一連の処理にまとめたようなものです。

ここでは、以下の3つの処理のプランを作成して、実行します。

  • httpd のインストール
  • httpd の起動
  • index.html のコピー

プランの作成

まず、myweb モジュールのプランを配置するためのディレクトリを作成します。

mkdir -p modules/myweb/plans/

続いてプランのファイル(.pp)を作成します。ここでは myweb モジュールの deploy という名前のプランを作成するため、ファイル名を modules/myweb/plans/deploy.pp とします。

plan myweb::deploy (TargetSpec $nodes) {
  run_task(
    "package::linux",
    $nodes,
    name => "httpd",
    action => "install"
  )

  run_task(
    "service::linux",
    $nodes,
    name => "httpd",
    action => "start"
  )

  upload_file(
    "/vagrant/bolt/index.html",
    "/var/www/html/index.html",
    $nodes
  )
}

bolt plan show でプランの定義を確認できます

$ bolt plan show --modulepath ./modules/
aggregate::count
aggregate::nodes
canary
facts
facts::info
myweb::deploy    ← 作成したプラン
puppetdb_fact
reboot

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

プランの処理内容

run_task で、package::linux を指定して、httpd をインストールします。続いて、service::linux で http サービスを起動します。 最後にupload_fileでコンテンツファイルをコピーします。

なお、upload_file で指定するパス "/vagrant/bolt/index.html" は、最初、bolt コマンド実行時からの相対パスで指定しましたが、見つからなかったため絶対パスにしました。

index.html の作成

コンテンツとなる簡単な HTML ファイル(index.html)を作成します。

<html>
<body>
<h1> Hello, Bolt!</h1>
</body>
<html>

実行

  • 実行コマンド
bolt plan run test::command nodes=172.16.0.10 --no-host-key-check -u vagrant -p --run-as root  --modulepath ./modules/
  • オプション説明
オプション 説明 今回の指定値
plan run タスクを指定するサブコマンド test::command
nodes プラン test::command で利用するパラメータ(リモートホスト 172.16.0.10
--modulepath モジュールのパス ./modules/
  • 実行結果
$ bolt plan run myweb::deploy nodes=172.16.0.10 --no-host-key-check -u vagrant -p --run-as root  --modulepath ./modules/
Please enter your password: (パスワードを入力)
Starting: plan myweb::deploy
Starting: task package::linux on 172.16.0.10
Finished: task package::linux with 0 failures in 3.99 sec
Starting: task service::linux on 172.16.0.10
Finished: task service::linux with 0 failures in 1.3 sec
Starting: file upload from /vagrant/bolt/index.html to /var/www/html/index.html on 172.16.0.10
Finished: file upload from /vagrant/bolt/index.html to /var/www/html/index.html with 0 failures in 0.93 sec
Finished: plan myweb::deploy in 6.27 sec
Plan completed successfully with no result

f:id:akira6592:20181220115117p:plain
コンテンツ表示確認(URLは環境都合上のもの)

無事に httpd がインストール、起動し、用意したコンテンツが表示されました。

補足

対応する公式ドキュメント




■ まとめ

エージェントレスなインフラ自動化ツール「Puppet Bolt」で、簡単なコマンド実行や、いくつかの処理をまとめたプランの実行を試してみました。 プランの作成に少々戸惑いましたが、コードで柔軟な処理がかけるかも知れません。 実行コマンドも、今回は長めになってしまいましたが、何かしらの定義ファイルを用意しておけば、もっと短くできそうです。

参考

サイト