テクノロジー DEVOPS 非公開: Ansible アドホック コマンド ガイドと例

Ansible アドホック コマンド ガイドと例

アドホック コマンドは Ansible でタスクを即座に実行するために使用され、後で使用するために保存する必要はありません。この記事は、Ansible アドホック コマンドについて説明します。

Ansible には、別の Ansible Playbook を作成する必要のないタスクが複数あります。そのタスクに対して ansible アドホック コマンドを実行するだけです。これらは、ターゲット ホスト上で単一のタスクを実行するワンライナー コマンドです。これらのコマンドは /usr/bin/ansible にあります。

Ansible アドホック コマンド ガイドと例
Ansible アドホック コマンド ガイドと例

Ansible アドホック コマンドとは何ですか?

Ansible アドホック コマンドは、完全なプレイブックを作成しなくても、サーバーのグループで単一のタスクを実行できる便利なツールです。これらのコマンドは、サービスのステータスの確認やコマンドの迅速な実行などの単純なタスクを実行するのに最適です。ただし、ソフトウェアのインストールなどのより複雑なタスクの場合は、通常、プレイブックを使用する方が適切です。

必要なモジュール名やモジュールに指定する必要があるパラメータなどの関連情報を含む 1 行のコードを入力して、アドホック コマンドを実行します。

Ansible アドホック コマンドの構文は次のとおりです。

 ansible <hosts> -m <module> -a "<arguments>"

ここ:

  • <hosts> はターゲット ホストのカンマ区切りのリストです。
  • <module> は使用する Ansible モジュールの名前です。
  • <arguments> は、モジュールに渡される任意の引数です。

たとえば、サーバーのグループのディスク容量を確認したい場合は、次のようにシェル モジュールで df -h コマンドを使用できます。

 ansible web_servers -m shell -a "df -h"

シェル モジュールは、web_servers グループ内のすべてのサーバーで df -h コマンドを実行します。その後、各サーバーからの出力をコンソールで確認できるようになります。とても簡単で効率的です!

Ansible アドホック コマンドを使用する理由は何ですか?

アドホック コマンドは、迅速で使いやすいだけでなく、それほど複雑さを必要としない単純な 1 回限りのタスクに最適です。また、新しいモジュールやコマンドを Playbook に追加する前に試すのにも最適な方法です。これは、間違いを避け、長期的には時間を節約するのに役立ちます。

サーバーの問題のトラブルシューティングを行う場合は、これらを使用してサービスのステータスを確認したり、ログ ファイルを表示したり、その他の診断タスクを実行したりできます。

さらに、アドホックコマンドも柔軟です。これらは、パッケージのインストール、ファイルのコピー、複数のサーバーでのコマンドの同時実行など、あらゆる種類のタスクに使用できます。

サーバー管理をより簡単かつ効率的にしたい場合は、Ansible アドホック コマンドが最適です。

以下にいくつかの使用方法を示します。

  • サーバーの再起動
  • ファイルの管理
  • パッケージの管理
  • ユーザーとグループの管理
  • サービスの管理
  • 事実の収集
  • パターンとアドホック コマンド

すべてのホストに ping を送信して実行中かどうかを確認したり、ファイルのコピー、サーバーの再起動、パッケージのインストールなどのタスクを、Ansible アドホック コマンドを使用して簡単に実行できます。以下は、知っておく必要がある基本的な Ansible アドホック コマンドのリストです。

Ansible アドホック コマンド ガイドと例
Ansible アドホック コマンド ガイドと例

基本コマンド

以下のアドホック コマンドは、インベントリ ファイル内のすべてのホストで ping モジュールを実行します。ここで、 -m モジュールのオプションです。

 <strong>root@:/home/# ansible all -m ping</strong>
node1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

以下に示すコマンドは、ホストのグループ (インベントリ ファイル ‘/etc/ansible/hosts’ に存在する クライアント) 上でセットアップ モジュールを実行します。

 root@:/home/# ansible Client -m setup -a "filter=ansible_distribution*"
node1 | SUCCESS => {
    "ansible_facts": {
        "ansible_distribution": "Ubuntu", 
        "ansible_distribution_file_parsed": true, 
        "ansible_distribution_file_path": "/etc/os-release", 
        "ansible_distribution_file_variety": "Debian", 
        "ansible_distribution_major_version": "18", 
        "ansible_distribution_release": "cosmic", 
        "ansible_distribution_version": "18.10", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

以下のコマンドは、SSH パスワード認証を求めるために使用されます。コマンドの最後に –ask-pass オプションを追加する必要があります。コマンドを実行すると、SSH パスワードの入力を求められます。

 root@:/home/# ansible Client -m ping --ask-pass
SSH password: 
node1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

以下のコマンドは、root 権限を持つ非 root ユーザーとしてアドホック コマンドを実行する機能を提供します。 --become オプションは root 権限を与え、 -K オプションはパスワードを要求します。

 root@:/home/# ansible Client -m shell -a 'fdisk -l' -u  --become -K
BECOME password: 
node1 | CHANGED | rc=0 >>
Disk /dev/loop0: 14.5 MiB, 15208448 bytes, 29704 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop2: 42.1 MiB, 44183552 bytes, 86296 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop3: 149.9 MiB, 157184000 bytes, 307000 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop5: 140.7 MiB, 147501056 bytes, 288088 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop6: 151.2 MiB, 158584832 bytes, 309736 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop7: 14.8 MiB, 15458304 bytes, 30192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/sda: 500 GiB, 536870912000 bytes, 1048576000 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xcef957f5

Device     Boot     Start        End   Sectors   Size Id Type
/dev/sda1            2048  462639103 462637056 220.6G 83 Linux
/dev/sda2  *    462639104  464592895   1953792   954M 83 Linux
/dev/sda3       464592896  482168831  17575936   8.4G 82 Linux swap / Solaris
/dev/sda4       482168832 1048573951 566405120 270.1G 83 Linux


Disk /dev/loop8: 4 MiB, 4218880 bytes, 8240 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

このアドホック コマンドは、フォークの数を定義する -f オプションを使用してシステムを再起動するために使用されます。

 root@:/home/# ansible Client -a "/sbin/reboot" -f 1
Ansible アドホック コマンド ガイドと例
Ansible アドホック コマンド ガイドと例

ファイル転送

以下の ansible アドホック コマンドは、インベントリ ファイルで定義されているホストのグループ (クライアント) のソースから宛先にファイルをコピーするために使用されます。パスワードを入力すると、「change」パラメータの出力は「true」になります。これは、ファイルが宛先にコピーされたことを意味します。

 root@:/home/# ansible Client -m copy -a 'src=/home//nginx.yml dest=/home//Desktop/ owner=root mode=0644' -u root --become -K
BECOME password: 
node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "5631822866afd5f19b928edb3ba018385df22dd3", 
    "dest": "/home//Desktop/nginx.yml", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "0d6ffe1069fc25ad4f8ad700277c4634", 
    "mode": "0644", 
    "owner": "root", 
    "size": 280, 
    "src": "/root/.ansible/tmp/ansible-tmp-1562253463.3-214622150088155/source", 
    "state": "file", 
    "uid": 0
}

以下のコマンドを実行して、コピー モジュールが正しく動作したかどうかを確認します。コピーされたファイルは、前のコマンドで指定した宛先にあるはずです。

 root@:/home/# ls Desktop/

nginx.yml

次のアドホック コマンドで fetch module を実行するための新しいディレクトリを作成しています。

 root@:/home/# mkdir example

root@:/home/# ls

Desktop  Documents  example  examples.desktop  nginx_new.yml  nginx.yml

以下の ansible アドホック コマンドは、コマンドで定義されたホストからファイルをダウンロードするために使用されます。このコマンドでは、fetch モジュールを使用して、node1 サーバーから ansible ノード上のローカル宛先にファイルをダウンロードしています。

 root@:/home/# ansible node1 -m fetch -a 'src=/etc/sudoers.d/nginx.yml dest=/home//example/ flat=yes'

node1 | SUCCESS => {

    "changed": false,

    "checksum": "5631822866afd5f19b928edb3ba018385df22dd3",

    "dest": "/home//example/nginx.yml",

    "file": "/etc/sudoers.d/nginx.yml",

    "md5sum": "0d6ffe1069fc25ad4f8ad700277c4634"

}

コマンドで指定された宛先にファイルがダウンロードされたかどうかを確認します。

 root@:/home/# ls example

nginx.yml
Ansible アドホック コマンド ガイドと例
Ansible アドホック コマンド ガイドと例

パッケージの管理

以下のコマンドは、apt モジュールを使用して、前述のホスト (クライアント) のグループに nginx をインストールします。

 root@:/home/# ansible Client -m apt -a 'name=nginx state=latest' --become

node1 | SUCCESS => {

    "ansible_facts": {

        "discovered_interpreter_python": "/usr/bin/python"

    },

    "cache_update_time": 1562411227,

    "cache_updated": false,

    "changed": false

}

以下に示すコマンドは、apt モジュールを使用してホストのグループ (クライアント) 上の nginx を削除し、関連するすべての設定をパージします。

 root@:/home/# ansible Client -m apt -a 'name=nginx state=absent purge=yes' --become

node1 | CHANGED => {

    "ansible_facts": {

        "discovered_interpreter_python": "/usr/bin/python"

    },

    "changed": true,

    "stderr": "",

    "stderr_lines": [],

    "stdout": "Reading package lists...\nBuilding dependency tree...\nReading state information...\nThe following packages were automatically installed and are no longer required:\n  libnginx-mod-http-geoip libnginx-mod-http-image-filter\n  libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream\n  nginx-common nginx-core\nUse 'sudo apt autoremove' to remove them.\nThe following packages will be REMOVED:\n  nginx*\n0 upgraded, 0 newly installed, 1 to remove and 241 not upgraded.\nAfter this operation, 44.0 kB disk space will be freed.\n(Reading database ... \r(Reading database ... 5%\r(Reading database ... 10%\r(Reading database ... 15%\r(Reading database ... 20%\r(Reading database ... 25%\r(Reading database ... 30%\r(Reading database ... 35%\r(Reading database ... 40%\r(Reading database ... 45%\r(Reading database ... 50%\r(Reading database ... 55%\r(Reading database ... 60%\r(Reading database ... 65%\r(Reading database ... 70%\r(Reading database ... 75%\r(Reading database ... 80%\r(Reading database ... 85%\r(Reading database ... 90%\r(Reading database ... 95%\r(Reading database ... 100%\r(Reading database ... 180191 files and directories currently installed.)\r\nRemoving nginx (1.15.5-0ubuntu2.1) ...\r\n",

    "stdout_lines": [

        "Reading package lists...",

        "Building dependency tree...",

        "Reading state information...",

        "The following packages were automatically installed and are no longer required:",

        "  libnginx-mod-http-geoip libnginx-mod-http-image-filter",

        "  libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream",

        "  nginx-common nginx-core",

        "Use 'sudo apt autoremove' to remove them.",

        "The following packages will be REMOVED:",

        "  nginx*",

        "0 upgraded, 0 newly installed, 1 to remove and 241 not upgraded.",

        "After this operation, 44.0 kB disk space will be freed.",

        "(Reading database ... ",

        "(Reading database ... 5%",

        "(Reading database ... 10%",

        "(Reading database ... 15%",

        "(Reading database ... 20%",

        "(Reading database ... 25%",

        "(Reading database ... 30%",

        "(Reading database ... 35%",

        "(Reading database ... 40%",

        "(Reading database ... 45%",

        "(Reading database ... 50%",

        "(Reading database ... 55%",

        "(Reading database ... 60%",

        "(Reading database ... 65%",

        "(Reading database ... 70%",

        "(Reading database ... 75%",

        "(Reading database ... 80%",

        "(Reading database ... 85%",

        "(Reading database ... 90%",

        "(Reading database ... 95%",

        "(Reading database ... 100%",

        "(Reading database ... 180191 files and directories currently installed.)",

        "Removing nginx (1.15.5-0ubuntu2.1) ..."

    ]

}
Ansible アドホック コマンド ガイドと例
Ansible アドホック コマンド ガイドと例

サービスの管理

以下の ansible アドホック コマンドはサービス モジュールを実行して、ホスト上で nginx を起動します。状態値を開始する必要があります。

 root@:/home/# ansible Client -m service -a 'name=nginx state=started enabled=yes' --become

node1 | SUCCESS => {

    "ansible_facts": {

        "discovered_interpreter_python": "/usr/bin/python"

    },

    "changed": false,

    "enabled": true,

    "name": "nginx",

    "state": "started",

    "status": {

        "ActiveEnterTimestamp": "Sat 2019-07-06 08:28:02 EDT",

        "ActiveEnterTimestampMonotonic": "31411371",

        "ActiveExitTimestampMonotonic": "0",

        "ActiveState": "active",

        "After": "sysinit.target system.slice systemd-journald.socket basic.target network.target",

        "AllowIsolate": "no",

        "AmbientCapabilities": "",

        "AssertResult": "yes",

        "AssertTimestamp": "Sat 2019-07-06 08:27:59 EDT",

        "AssertTimestampMonotonic": "27694868",

        "Before": "multi-user.target shutdown.target",

        "BlockIOAccounting": "no",

        "BlockIOWeight": "[not set]",

        "CapabilityBoundingSet": "cap_chown cap_dac_override cap_dac_read_search cap_fowner cap_fsetid cap_kill cap_setgid cap_setuid cap_setpcap cap_linux_immutable cap_net_bind_service cap_net_broadcast cap_net_admin cap_net_raw cap_ipc_lock cap_ipc_owner cap_sys_module cap_sys_rawio cap_sys_chroot cap_sys_ptrace cap_sys_pacct cap_sys_admin cap_sys_boot cap_sys_nice cap_sys_resource cap_sys_time cap_sys_tty_config cap_mknod cap_lease cap_audit_write cap_audit_control cap_setfcap cap_mac_override cap_mac_admin cap_syslog cap_wake_alarm cap_block_suspend",

        "CollectMode": "inactive",

        "ConditionResult": "yes",

        "ConditionTimestamp": "Sat 2019-07-06 08:27:59 EDT",

        "ConditionTimestampMonotonic": "27694867",

        "ConfigurationDirectoryMode": "0755",

        "Conflicts": "shutdown.target",

        "ControlGroup": "/system.slice/nginx.service",

        "ControlPID": "0",

        "ExecMainStartTimestamp": "Sat 2019-07-06 08:28:02 EDT",

        "ExecMainStartTimestampMonotonic": "31411353",

        "ExecMainStatus": "0",

        "ExecReload": "{ path=/usr/sbin/nginx ; argv[]=/usr/sbin/nginx -g daemon on; master_process on; -s reload ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",

        "ExecStart": "{ path=/usr/sbin/nginx ; argv[]=/usr/sbin/nginx -g daemon on; master_process on; ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",

        "ExecStartPre": "{ path=/usr/sbin/nginx ; argv[]=/usr/sbin/nginx -t -q -g daemon on; master_process on; ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",

        "ExecStop": "{ path=/sbin/start-stop-daemon ; argv[]=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid ; ignore_errors=yes ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",

        "FailureAction": "none",

        "FileDescriptorStoreMax": "0",

        "FragmentPath": "/lib/systemd/system/nginx.service",

        "GID": "[not set]",

        "GuessMainPID": "yes",

        "IOAccounting": "no",

        "IOSchedulingClass": "0",

        "IOSchedulingPriority": "0",

        "IOWeight": "[not set]",

    }

}

以下はサービス モジュールを実行してホスト上の nginx を停止します。状態値が停止に変わります。

 root@:/home/# ansible Client -m service -a 'name=nginx state=stopped' --become

node1 | CHANGED => {

    "ansible_facts": {

        "discovered_interpreter_python": "/usr/bin/python"

    },

    "changed": true,

    "name": "nginx",

    "state": "stopped",

    "status": {

        "ActiveEnterTimestamp": "Sat 2019-07-06 08:28:02 EDT",

        "ActiveEnterTimestampMonotonic": "31411371",

        "ActiveExitTimestampMonotonic": "0",

        "ActiveState": "active",

        "After": "sysinit.target system.slice systemd-journald.socket basic.target network.target",

        "AllowIsolate": "no",

        "AmbientCapabilities": "",

        "AssertResult": "yes",

        "AssertTimestamp": "Sat 2019-07-06 08:27:59 EDT",

        "AssertTimestampMonotonic": "27694868",

        "Before": "multi-user.target shutdown.target",

        "BlockIOAccounting": "no",

        "BlockIOWeight": "[not set]",

        "CPUAccounting": "no",

        "CPUQuotaPerSecUSec": "infinity",

        "CanReload": "yes",

        "CanStart": "yes",

        "CanStop": "yes",

        "CapabilityBoundingSet": "cap_chown cap_dac_override cap_dac_read_search cap_fowner cap_fsetid cap_kill cap_setgid cap_setuid cap_setpcap cap_linux_immutable cap_net_bind_service cap_net_broadcast cap_net_admin cap_net_raw cap_ipc_lock cap_ipc_owner cap_sys_module cap_sys_rawio cap_sys_chroot cap_sys_ptrace cap_sys_pacct cap_sys_admin cap_sys_boot cap_sys_nice cap_sys_resource cap_sys_time cap_sys_tty_config cap_mknod cap_lease cap_audit_write cap_audit_control cap_setfcap cap_mac_override cap_mac_admin cap_syslog cap_wake_alarm cap_block_suspend",

        "CollectMode": "inactive",

        "ConditionResult": "yes",

        "ConditionTimestamp": "Sat 2019-07-06 08:27:59 EDT",

        "ConditionTimestampMonotonic": "27694867",

        "ConfigurationDirectoryMode": "0755",

        "Conflicts": "shutdown.target",

        "ControlGroup": "/system.slice/nginx.service",

        "ControlPID": "0",

        "DefaultDependencies": "yes",

        "Delegate": "no",

        "Description": "A high performance web server and a reverse proxy server",

        "DevicePolicy": "auto",

        "Documentation": "man:nginx(8)",

        "DynamicUser": "no",

  }

}

システムの確認

以下で説明する ansible アドホック コマンドは、シェル モジュールを実行して、ルート パーティションで使用可能なディスクを確認します。

 root@:/home/# ansible Client -m shell -a 'df -h /dev/sda2' --become

node1 | CHANGED | rc=0 >>

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda2       923M  113M  748M  14% /boot

以下で説明するこのコマンドは、シェル モジュールを実行して、ホスト上の空きメモリ (RAM) をチェックします。

 root@:/home/# ansible Client -m shell -a 'free -m' --become

node1 | CHANGED | rc=0 >>

              total        used        free      shared  buff/cache   available

Mem:           5101         854        2760          27        1487        3947

Swap:          8581           0        8581

このコマンドは、実行中の各サーバーの稼働時間をチェックします。

 root@:/home/# ansible Client -a "uptime"

node1 | CHANGED | rc=0 >>

 11:31:17 up 1 day,  2:40,  2 users,  load average: 0.23, 0.05, 0.02

事実の収集

以下の ansible アドホック コマンドは、システムに存在するすべての変数を含む、システムのすべてのアドホック情報を提供します。

 root@:/home/# ansible all -m setup

node1 | SUCCESS => {

    "ansible_facts": {

        "ansible_all_ipv4_addresses": [

            "172.17.0.1",

            "10.0.2.15"

        ],

        "ansible_all_ipv6_addresses": [

            "fe80::763e:c0b4:14df:b273"

        ],

        "ansible_apparmor": {

            "status": "enabled"

        },

        "ansible_architecture": "x86_64",

        "ansible_bios_date": "12/01/2006",

        "ansible_bios_version": "VirtualBox",

        "ansible_cmdline": {

            "BOOT_IMAGE": "/vmlinuz-4.18.0-25-generic",

            "quiet": true,

            "ro": true,

            "root": "UUID=5f85d8b7-0ab2-48c9-9e6e-4ecfbcbdaa83",

            "splash": true

        },

        "ansible_date_time": {

            "date": "2019-07-07",

            "day": "07",

            "epoch": "1562525628",

            "hour": "14",

            "iso8601": "2019-07-07T18:53:48Z",

            "iso8601_basic": "20190707T145348850596",

            "iso8601_basic_short": "20190707T145348",

            "iso8601_micro": "2019-07-07T18:53:48.850697Z",

            "minute": "53",

            "month": "07",

            "second": "48",

            "time": "14:53:48",

            "tz": "EDT",

            "tz_offset": "-0400",

            "weekday": "Sunday",

            "weekday_number": "0",

            "weeknumber": "26",

            "year": "2019"

        },

        "ansible_default_ipv4": {

            "address": "10.0.2.15",

            "alias": "enp0s3",

            "broadcast": "10.0.2.255",

            "gateway": "10.0.2.2",

            "interface": "enp0s3",

            "macaddress": "08:00:27:68:64:9a",

            "mtu": 1500,

            "netmask": "255.255.255.0",

            "network": "10.0.2.0",

            "type": "ether"

        },

        "ansible_default_ipv6": {},

        "ansible_device_links": {

            "ids": {

                "sda": [

                    "ata-VBOX_HARDDISK_VB3a0a2351-0b6c0ed5"

                ],

                "sda1": [

                    "ata-VBOX_HARDDISK_VB3a0a2351-0b6c0ed5-part1"

                ],

                "sda2": [

                    "ata-VBOX_HARDDISK_VB3a0a2351-0b6c0ed5-part2"

                ],

                "sda3": [

                    "ata-VBOX_HARDDISK_VB3a0a2351-0b6c0ed5-part3"

                ],

                "sda4": [

                    "ata-VBOX_HARDDISK_VB3a0a2351-0b6c0ed5-part4"

                ],

                "sr0": [

                    "ata-VBOX_CD-ROM_VB2-01700376"

                ]

            },

            "labels": {

                "sr0": [

                    "VBox_GAs_6.0.2"

                ]

            },

            "masters": {},

            "uuids": {

                "sda1": [

                    "5f85d8b7-0ab2-48c9-9e6e-4ecfbcbdaa83"

                ],

                "sda2": [

                    "b8b7f87b-c3bf-48ed-a44c-f9b3ce0afbe5"

                ],

                "sda3": [

                    "a6c77fa6-e292-4a0d-b21f-8804f1949bbd"

                ],

                "sda4": [

                    "8207f970-4d9a-47db-a5d5-f620e5b17b7b"

                ],

                "sr0": [

                    "2019-01-14-14-57-19-65"

                ]

            }

        },

        "ansible_devices": {

            "loop0": {

                "holders": [],

                "host": "",

                "links": {

                    "ids": [],

                    "labels": [],

                    "masters": [],

                    "uuids": []

                },

                "model": null,

                "partitions": {},

                "removable": "0",

                "rotational": "1",

                "sas_address": null,

                "sas_device_handle": null,

                "scheduler_mode": "none",

                "sectors": "29704",

                "sectorsize": "512",

                "size": "14.50 MB",

                "support_discard": "4096",

                "vendor": null,

                "virtual": 1

            },

            "loop1": {

                "holders": [],

                "host": "",

                "links": {

                    "ids": [],

                    "labels": [],

                    "masters": [],

                    "uuids": []

                },

                "model": null,

                "partitions": {},

                "removable": "0",

                "rotational": "1",

                "sas_address": null,

                "sas_device_handle": null,

                "scheduler_mode": "none",

                "sectors": "0",

                "sectorsize": "512",

                "size": "0.00 Bytes",

                "support_discard": "4096",

                "vendor": null,

                "virtual": 1

            },

}

以上、Ansible アドホック コマンドについての説明でした。先に進み、Ansible セットアップでこれらのコマンドを試してください。これらのコマンドを使用すると、これらのタスクを実行するための Ansible プレイブックを作成せずに、Ansible モジュールを実行できます。これらのコマンドの実行中に問題が発生した場合は、コメントを入力してください。

「 Ansible アドホック コマンド ガイドと例」についてわかりやすく解説!絶対に観るべきベスト2動画

【第3回】Ansibleを学ぼう。Ansibleの概要
自动化运维系列5 Ansible 免密认证和AD hoc

アドホック コマンドは Ansible でタスクを即座に実行するために使用され、後で使用するために保存する必要はありません。この記事は、Ansible アドホック コマンドについて説明します。

Ansible には、別の Ansible Playbook を作成する必要のないタスクが複数あります。そのタスクに対して ansible アドホック コマンドを実行するだけです。これらは、ターゲット ホスト上で単一のタスクを実行するワンライナー コマンドです。これらのコマンドは /usr/bin/ansible にあります。

Ansible アドホック コマンド ガイドと例
Ansible アドホック コマンド ガイドと例

Ansible アドホック コマンドとは何ですか?

Ansible アドホック コマンドは、完全なプレイブックを作成しなくても、サーバーのグループで単一のタスクを実行できる便利なツールです。これらのコマンドは、サービスのステータスの確認やコマンドの迅速な実行などの単純なタスクを実行するのに最適です。ただし、ソフトウェアのインストールなどのより複雑なタスクの場合は、通常、プレイブックを使用する方が適切です。

必要なモジュール名やモジュールに指定する必要があるパラメータなどの関連情報を含む 1 行のコードを入力して、アドホック コマンドを実行します。

Ansible アドホック コマンドの構文は次のとおりです。

 ansible <hosts> -m <module> -a "<arguments>"

ここ:

  • <hosts> はターゲット ホストのカンマ区切りのリストです。
  • <module> は使用する Ansible モジュールの名前です。
  • <arguments> は、モジュールに渡される任意の引数です。

たとえば、サーバーのグループのディスク容量を確認したい場合は、次のようにシェル モジュールで df -h コマンドを使用できます。

 ansible web_servers -m shell -a "df -h"

シェル モジュールは、web_servers グループ内のすべてのサーバーで df -h コマンドを実行します。その後、各サーバーからの出力をコンソールで確認できるようになります。とても簡単で効率的です!

Ansible アドホック コマンドを使用する理由は何ですか?

アドホック コマンドは、迅速で使いやすいだけでなく、それほど複雑さを必要としない単純な 1 回限りのタスクに最適です。また、新しいモジュールやコマンドを Playbook に追加する前に試すのにも最適な方法です。これは、間違いを避け、長期的には時間を節約するのに役立ちます。

サーバーの問題のトラブルシューティングを行う場合は、これらを使用してサービスのステータスを確認したり、ログ ファイルを表示したり、その他の診断タスクを実行したりできます。

さらに、アドホックコマンドも柔軟です。これらは、パッケージのインストール、ファイルのコピー、複数のサーバーでのコマンドの同時実行など、あらゆる種類のタスクに使用できます。

サーバー管理をより簡単かつ効率的にしたい場合は、Ansible アドホック コマンドが最適です。

以下にいくつかの使用方法を示します。

  • サーバーの再起動
  • ファイルの管理
  • パッケージの管理
  • ユーザーとグループの管理
  • サービスの管理
  • 事実の収集
  • パターンとアドホック コマンド

すべてのホストに ping を送信して実行中かどうかを確認したり、ファイルのコピー、サーバーの再起動、パッケージのインストールなどのタスクを、Ansible アドホック コマンドを使用して簡単に実行できます。以下は、知っておく必要がある基本的な Ansible アドホック コマンドのリストです。

Ansible アドホック コマンド ガイドと例
Ansible アドホック コマンド ガイドと例

基本コマンド

以下のアドホック コマンドは、インベントリ ファイル内のすべてのホストで ping モジュールを実行します。ここで、 -m モジュールのオプションです。

 <strong>root@:/home/# ansible all -m ping</strong>
node1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

以下に示すコマンドは、ホストのグループ (インベントリ ファイル ‘/etc/ansible/hosts’ に存在する クライアント) 上でセットアップ モジュールを実行します。

 root@:/home/# ansible Client -m setup -a "filter=ansible_distribution*"
node1 | SUCCESS => {
    "ansible_facts": {
        "ansible_distribution": "Ubuntu", 
        "ansible_distribution_file_parsed": true, 
        "ansible_distribution_file_path": "/etc/os-release", 
        "ansible_distribution_file_variety": "Debian", 
        "ansible_distribution_major_version": "18", 
        "ansible_distribution_release": "cosmic", 
        "ansible_distribution_version": "18.10", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

以下のコマンドは、SSH パスワード認証を求めるために使用されます。コマンドの最後に –ask-pass オプションを追加する必要があります。コマンドを実行すると、SSH パスワードの入力を求められます。

 root@:/home/# ansible Client -m ping --ask-pass
SSH password: 
node1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

以下のコマンドは、root 権限を持つ非 root ユーザーとしてアドホック コマンドを実行する機能を提供します。 --become オプションは root 権限を与え、 -K オプションはパスワードを要求します。

 root@:/home/# ansible Client -m shell -a 'fdisk -l' -u  --become -K
BECOME password: 
node1 | CHANGED | rc=0 >>
Disk /dev/loop0: 14.5 MiB, 15208448 bytes, 29704 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop2: 42.1 MiB, 44183552 bytes, 86296 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop3: 149.9 MiB, 157184000 bytes, 307000 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop5: 140.7 MiB, 147501056 bytes, 288088 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop6: 151.2 MiB, 158584832 bytes, 309736 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop7: 14.8 MiB, 15458304 bytes, 30192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/sda: 500 GiB, 536870912000 bytes, 1048576000 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xcef957f5

Device     Boot     Start        End   Sectors   Size Id Type
/dev/sda1            2048  462639103 462637056 220.6G 83 Linux
/dev/sda2  *    462639104  464592895   1953792   954M 83 Linux
/dev/sda3       464592896  482168831  17575936   8.4G 82 Linux swap / Solaris
/dev/sda4       482168832 1048573951 566405120 270.1G 83 Linux


Disk /dev/loop8: 4 MiB, 4218880 bytes, 8240 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

このアドホック コマンドは、フォークの数を定義する -f オプションを使用してシステムを再起動するために使用されます。

 root@:/home/# ansible Client -a "/sbin/reboot" -f 1
Ansible アドホック コマンド ガイドと例
Ansible アドホック コマンド ガイドと例

ファイル転送

以下の ansible アドホック コマンドは、インベントリ ファイルで定義されているホストのグループ (クライアント) のソースから宛先にファイルをコピーするために使用されます。パスワードを入力すると、「change」パラメータの出力は「true」になります。これは、ファイルが宛先にコピーされたことを意味します。

 root@:/home/# ansible Client -m copy -a 'src=/home//nginx.yml dest=/home//Desktop/ owner=root mode=0644' -u root --become -K
BECOME password: 
node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "5631822866afd5f19b928edb3ba018385df22dd3", 
    "dest": "/home//Desktop/nginx.yml", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "0d6ffe1069fc25ad4f8ad700277c4634", 
    "mode": "0644", 
    "owner": "root", 
    "size": 280, 
    "src": "/root/.ansible/tmp/ansible-tmp-1562253463.3-214622150088155/source", 
    "state": "file", 
    "uid": 0
}

以下のコマンドを実行して、コピー モジュールが正しく動作したかどうかを確認します。コピーされたファイルは、前のコマンドで指定した宛先にあるはずです。

 root@:/home/# ls Desktop/

nginx.yml

次のアドホック コマンドで fetch module を実行するための新しいディレクトリを作成しています。

 root@:/home/# mkdir example

root@:/home/# ls

Desktop  Documents  example  examples.desktop  nginx_new.yml  nginx.yml

以下の ansible アドホック コマンドは、コマンドで定義されたホストからファイルをダウンロードするために使用されます。このコマンドでは、fetch モジュールを使用して、node1 サーバーから ansible ノード上のローカル宛先にファイルをダウンロードしています。

 root@:/home/# ansible node1 -m fetch -a 'src=/etc/sudoers.d/nginx.yml dest=/home//example/ flat=yes'

node1 | SUCCESS => {

    "changed": false,

    "checksum": "5631822866afd5f19b928edb3ba018385df22dd3",

    "dest": "/home//example/nginx.yml",

    "file": "/etc/sudoers.d/nginx.yml",

    "md5sum": "0d6ffe1069fc25ad4f8ad700277c4634"

}

コマンドで指定された宛先にファイルがダウンロードされたかどうかを確認します。

 root@:/home/# ls example

nginx.yml
Ansible アドホック コマンド ガイドと例
Ansible アドホック コマンド ガイドと例

パッケージの管理

以下のコマンドは、apt モジュールを使用して、前述のホスト (クライアント) のグループに nginx をインストールします。

 root@:/home/# ansible Client -m apt -a 'name=nginx state=latest' --become

node1 | SUCCESS => {

    "ansible_facts": {

        "discovered_interpreter_python": "/usr/bin/python"

    },

    "cache_update_time": 1562411227,

    "cache_updated": false,

    "changed": false

}

以下に示すコマンドは、apt モジュールを使用してホストのグループ (クライアント) 上の nginx を削除し、関連するすべての設定をパージします。

 root@:/home/# ansible Client -m apt -a 'name=nginx state=absent purge=yes' --become

node1 | CHANGED => {

    "ansible_facts": {

        "discovered_interpreter_python": "/usr/bin/python"

    },

    "changed": true,

    "stderr": "",

    "stderr_lines": [],

    "stdout": "Reading package lists...\nBuilding dependency tree...\nReading state information...\nThe following packages were automatically installed and are no longer required:\n  libnginx-mod-http-geoip libnginx-mod-http-image-filter\n  libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream\n  nginx-common nginx-core\nUse 'sudo apt autoremove' to remove them.\nThe following packages will be REMOVED:\n  nginx*\n0 upgraded, 0 newly installed, 1 to remove and 241 not upgraded.\nAfter this operation, 44.0 kB disk space will be freed.\n(Reading database ... \r(Reading database ... 5%\r(Reading database ... 10%\r(Reading database ... 15%\r(Reading database ... 20%\r(Reading database ... 25%\r(Reading database ... 30%\r(Reading database ... 35%\r(Reading database ... 40%\r(Reading database ... 45%\r(Reading database ... 50%\r(Reading database ... 55%\r(Reading database ... 60%\r(Reading database ... 65%\r(Reading database ... 70%\r(Reading database ... 75%\r(Reading database ... 80%\r(Reading database ... 85%\r(Reading database ... 90%\r(Reading database ... 95%\r(Reading database ... 100%\r(Reading database ... 180191 files and directories currently installed.)\r\nRemoving nginx (1.15.5-0ubuntu2.1) ...\r\n",

    "stdout_lines": [

        "Reading package lists...",

        "Building dependency tree...",

        "Reading state information...",

        "The following packages were automatically installed and are no longer required:",

        "  libnginx-mod-http-geoip libnginx-mod-http-image-filter",

        "  libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream",

        "  nginx-common nginx-core",

        "Use 'sudo apt autoremove' to remove them.",

        "The following packages will be REMOVED:",

        "  nginx*",

        "0 upgraded, 0 newly installed, 1 to remove and 241 not upgraded.",

        "After this operation, 44.0 kB disk space will be freed.",

        "(Reading database ... ",

        "(Reading database ... 5%",

        "(Reading database ... 10%",

        "(Reading database ... 15%",

        "(Reading database ... 20%",

        "(Reading database ... 25%",

        "(Reading database ... 30%",

        "(Reading database ... 35%",

        "(Reading database ... 40%",

        "(Reading database ... 45%",

        "(Reading database ... 50%",

        "(Reading database ... 55%",

        "(Reading database ... 60%",

        "(Reading database ... 65%",

        "(Reading database ... 70%",

        "(Reading database ... 75%",

        "(Reading database ... 80%",

        "(Reading database ... 85%",

        "(Reading database ... 90%",

        "(Reading database ... 95%",

        "(Reading database ... 100%",

        "(Reading database ... 180191 files and directories currently installed.)",

        "Removing nginx (1.15.5-0ubuntu2.1) ..."

    ]

}
Ansible アドホック コマンド ガイドと例
Ansible アドホック コマンド ガイドと例

サービスの管理

以下の ansible アドホック コマンドはサービス モジュールを実行して、ホスト上で nginx を起動します。状態値を開始する必要があります。

 root@:/home/# ansible Client -m service -a 'name=nginx state=started enabled=yes' --become

node1 | SUCCESS => {

    "ansible_facts": {

        "discovered_interpreter_python": "/usr/bin/python"

    },

    "changed": false,

    "enabled": true,

    "name": "nginx",

    "state": "started",

    "status": {

        "ActiveEnterTimestamp": "Sat 2019-07-06 08:28:02 EDT",

        "ActiveEnterTimestampMonotonic": "31411371",

        "ActiveExitTimestampMonotonic": "0",

        "ActiveState": "active",

        "After": "sysinit.target system.slice systemd-journald.socket basic.target network.target",

        "AllowIsolate": "no",

        "AmbientCapabilities": "",

        "AssertResult": "yes",

        "AssertTimestamp": "Sat 2019-07-06 08:27:59 EDT",

        "AssertTimestampMonotonic": "27694868",

        "Before": "multi-user.target shutdown.target",

        "BlockIOAccounting": "no",

        "BlockIOWeight": "[not set]",

        "CapabilityBoundingSet": "cap_chown cap_dac_override cap_dac_read_search cap_fowner cap_fsetid cap_kill cap_setgid cap_setuid cap_setpcap cap_linux_immutable cap_net_bind_service cap_net_broadcast cap_net_admin cap_net_raw cap_ipc_lock cap_ipc_owner cap_sys_module cap_sys_rawio cap_sys_chroot cap_sys_ptrace cap_sys_pacct cap_sys_admin cap_sys_boot cap_sys_nice cap_sys_resource cap_sys_time cap_sys_tty_config cap_mknod cap_lease cap_audit_write cap_audit_control cap_setfcap cap_mac_override cap_mac_admin cap_syslog cap_wake_alarm cap_block_suspend",

        "CollectMode": "inactive",

        "ConditionResult": "yes",

        "ConditionTimestamp": "Sat 2019-07-06 08:27:59 EDT",

        "ConditionTimestampMonotonic": "27694867",

        "ConfigurationDirectoryMode": "0755",

        "Conflicts": "shutdown.target",

        "ControlGroup": "/system.slice/nginx.service",

        "ControlPID": "0",

        "ExecMainStartTimestamp": "Sat 2019-07-06 08:28:02 EDT",

        "ExecMainStartTimestampMonotonic": "31411353",

        "ExecMainStatus": "0",

        "ExecReload": "{ path=/usr/sbin/nginx ; argv[]=/usr/sbin/nginx -g daemon on; master_process on; -s reload ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",

        "ExecStart": "{ path=/usr/sbin/nginx ; argv[]=/usr/sbin/nginx -g daemon on; master_process on; ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",

        "ExecStartPre": "{ path=/usr/sbin/nginx ; argv[]=/usr/sbin/nginx -t -q -g daemon on; master_process on; ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",

        "ExecStop": "{ path=/sbin/start-stop-daemon ; argv[]=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid ; ignore_errors=yes ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }",

        "FailureAction": "none",

        "FileDescriptorStoreMax": "0",

        "FragmentPath": "/lib/systemd/system/nginx.service",

        "GID": "[not set]",

        "GuessMainPID": "yes",

        "IOAccounting": "no",

        "IOSchedulingClass": "0",

        "IOSchedulingPriority": "0",

        "IOWeight": "[not set]",

    }

}

以下はサービス モジュールを実行してホスト上の nginx を停止します。状態値が停止に変わります。

 root@:/home/# ansible Client -m service -a 'name=nginx state=stopped' --become

node1 | CHANGED => {

    "ansible_facts": {

        "discovered_interpreter_python": "/usr/bin/python"

    },

    "changed": true,

    "name": "nginx",

    "state": "stopped",

    "status": {

        "ActiveEnterTimestamp": "Sat 2019-07-06 08:28:02 EDT",

        "ActiveEnterTimestampMonotonic": "31411371",

        "ActiveExitTimestampMonotonic": "0",

        "ActiveState": "active",

        "After": "sysinit.target system.slice systemd-journald.socket basic.target network.target",

        "AllowIsolate": "no",

        "AmbientCapabilities": "",

        "AssertResult": "yes",

        "AssertTimestamp": "Sat 2019-07-06 08:27:59 EDT",

        "AssertTimestampMonotonic": "27694868",

        "Before": "multi-user.target shutdown.target",

        "BlockIOAccounting": "no",

        "BlockIOWeight": "[not set]",

        "CPUAccounting": "no",

        "CPUQuotaPerSecUSec": "infinity",

        "CanReload": "yes",

        "CanStart": "yes",

        "CanStop": "yes",

        "CapabilityBoundingSet": "cap_chown cap_dac_override cap_dac_read_search cap_fowner cap_fsetid cap_kill cap_setgid cap_setuid cap_setpcap cap_linux_immutable cap_net_bind_service cap_net_broadcast cap_net_admin cap_net_raw cap_ipc_lock cap_ipc_owner cap_sys_module cap_sys_rawio cap_sys_chroot cap_sys_ptrace cap_sys_pacct cap_sys_admin cap_sys_boot cap_sys_nice cap_sys_resource cap_sys_time cap_sys_tty_config cap_mknod cap_lease cap_audit_write cap_audit_control cap_setfcap cap_mac_override cap_mac_admin cap_syslog cap_wake_alarm cap_block_suspend",

        "CollectMode": "inactive",

        "ConditionResult": "yes",

        "ConditionTimestamp": "Sat 2019-07-06 08:27:59 EDT",

        "ConditionTimestampMonotonic": "27694867",

        "ConfigurationDirectoryMode": "0755",

        "Conflicts": "shutdown.target",

        "ControlGroup": "/system.slice/nginx.service",

        "ControlPID": "0",

        "DefaultDependencies": "yes",

        "Delegate": "no",

        "Description": "A high performance web server and a reverse proxy server",

        "DevicePolicy": "auto",

        "Documentation": "man:nginx(8)",

        "DynamicUser": "no",

  }

}

システムの確認

以下で説明する ansible アドホック コマンドは、シェル モジュールを実行して、ルート パーティションで使用可能なディスクを確認します。

 root@:/home/# ansible Client -m shell -a 'df -h /dev/sda2' --become

node1 | CHANGED | rc=0 >>

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda2       923M  113M  748M  14% /boot

以下で説明するこのコマンドは、シェル モジュールを実行して、ホスト上の空きメモリ (RAM) をチェックします。

 root@:/home/# ansible Client -m shell -a 'free -m' --become

node1 | CHANGED | rc=0 >>

              total        used        free      shared  buff/cache   available

Mem:           5101         854        2760          27        1487        3947

Swap:          8581           0        8581

このコマンドは、実行中の各サーバーの稼働時間をチェックします。

 root@:/home/# ansible Client -a "uptime"

node1 | CHANGED | rc=0 >>

 11:31:17 up 1 day,  2:40,  2 users,  load average: 0.23, 0.05, 0.02

事実の収集

以下の ansible アドホック コマンドは、システムに存在するすべての変数を含む、システムのすべてのアドホック情報を提供します。

 root@:/home/# ansible all -m setup

node1 | SUCCESS => {

    "ansible_facts": {

        "ansible_all_ipv4_addresses": [

            "172.17.0.1",

            "10.0.2.15"

        ],

        "ansible_all_ipv6_addresses": [

            "fe80::763e:c0b4:14df:b273"

        ],

        "ansible_apparmor": {

            "status": "enabled"

        },

        "ansible_architecture": "x86_64",

        "ansible_bios_date": "12/01/2006",

        "ansible_bios_version": "VirtualBox",

        "ansible_cmdline": {

            "BOOT_IMAGE": "/vmlinuz-4.18.0-25-generic",

            "quiet": true,

            "ro": true,

            "root": "UUID=5f85d8b7-0ab2-48c9-9e6e-4ecfbcbdaa83",

            "splash": true

        },

        "ansible_date_time": {

            "date": "2019-07-07",

            "day": "07",

            "epoch": "1562525628",

            "hour": "14",

            "iso8601": "2019-07-07T18:53:48Z",

            "iso8601_basic": "20190707T145348850596",

            "iso8601_basic_short": "20190707T145348",

            "iso8601_micro": "2019-07-07T18:53:48.850697Z",

            "minute": "53",

            "month": "07",

            "second": "48",

            "time": "14:53:48",

            "tz": "EDT",

            "tz_offset": "-0400",

            "weekday": "Sunday",

            "weekday_number": "0",

            "weeknumber": "26",

            "year": "2019"

        },

        "ansible_default_ipv4": {

            "address": "10.0.2.15",

            "alias": "enp0s3",

            "broadcast": "10.0.2.255",

            "gateway": "10.0.2.2",

            "interface": "enp0s3",

            "macaddress": "08:00:27:68:64:9a",

            "mtu": 1500,

            "netmask": "255.255.255.0",

            "network": "10.0.2.0",

            "type": "ether"

        },

        "ansible_default_ipv6": {},

        "ansible_device_links": {

            "ids": {

                "sda": [

                    "ata-VBOX_HARDDISK_VB3a0a2351-0b6c0ed5"

                ],

                "sda1": [

                    "ata-VBOX_HARDDISK_VB3a0a2351-0b6c0ed5-part1"

                ],

                "sda2": [

                    "ata-VBOX_HARDDISK_VB3a0a2351-0b6c0ed5-part2"

                ],

                "sda3": [

                    "ata-VBOX_HARDDISK_VB3a0a2351-0b6c0ed5-part3"

                ],

                "sda4": [

                    "ata-VBOX_HARDDISK_VB3a0a2351-0b6c0ed5-part4"

                ],

                "sr0": [

                    "ata-VBOX_CD-ROM_VB2-01700376"

                ]

            },

            "labels": {

                "sr0": [

                    "VBox_GAs_6.0.2"

                ]

            },

            "masters": {},

            "uuids": {

                "sda1": [

                    "5f85d8b7-0ab2-48c9-9e6e-4ecfbcbdaa83"

                ],

                "sda2": [

                    "b8b7f87b-c3bf-48ed-a44c-f9b3ce0afbe5"

                ],

                "sda3": [

                    "a6c77fa6-e292-4a0d-b21f-8804f1949bbd"

                ],

                "sda4": [

                    "8207f970-4d9a-47db-a5d5-f620e5b17b7b"

                ],

                "sr0": [

                    "2019-01-14-14-57-19-65"

                ]

            }

        },

        "ansible_devices": {

            "loop0": {

                "holders": [],

                "host": "",

                "links": {

                    "ids": [],

                    "labels": [],

                    "masters": [],

                    "uuids": []

                },

                "model": null,

                "partitions": {},

                "removable": "0",

                "rotational": "1",

                "sas_address": null,

                "sas_device_handle": null,

                "scheduler_mode": "none",

                "sectors": "29704",

                "sectorsize": "512",

                "size": "14.50 MB",

                "support_discard": "4096",

                "vendor": null,

                "virtual": 1

            },

            "loop1": {

                "holders": [],

                "host": "",

                "links": {

                    "ids": [],

                    "labels": [],

                    "masters": [],

                    "uuids": []

                },

                "model": null,

                "partitions": {},

                "removable": "0",

                "rotational": "1",

                "sas_address": null,

                "sas_device_handle": null,

                "scheduler_mode": "none",

                "sectors": "0",

                "sectorsize": "512",

                "size": "0.00 Bytes",

                "support_discard": "4096",

                "vendor": null,

                "virtual": 1

            },

}

以上、Ansible アドホック コマンドについての説明でした。先に進み、Ansible セットアップでこれらのコマンドを試してください。これらのコマンドを使用すると、これらのタスクを実行するための Ansible プレイブックを作成せずに、Ansible モジュールを実行できます。これらのコマンドの実行中に問題が発生した場合は、コメントを入力してください。

「 Ansible アドホック コマンド ガイドと例」についてわかりやすく解説!絶対に観るべきベスト2動画

【第3回】Ansibleを学ぼう。Ansibleの概要
自动化运维系列5 Ansible 免密认证和AD hoc