Ansible コマンドモジュール

AnsibleAnsibleBeginner
今すぐ練習

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

この実験では、リモートホストでコマンドを実行するための強力なツールであるAnsible Commandモジュールを探ります。Commandモジュールを使うことで、Ansibleのプレイブックやタスクから直接コマンドラインとやり取りでき、リモートシステムを管理するための多用途な方法が提供されます。この実験を通じて、Ansible Commandモジュールを使ってさまざまなコマンドを実行し、変数と引数を使って作業し、コマンドの出力をキャプチャする方法を学びます。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) ansible(("Ansible")) -.-> ansible/ModuleOperationsGroup(["Module Operations"]) linux(("Linux")) -.-> linux/FileandDirectoryManagementGroup(["File and Directory Management"]) ansible(("Ansible")) -.-> ansible/PlaybookEssentialsGroup(["Playbook Essentials"]) linux(("Linux")) -.-> linux/SystemInformationandMonitoringGroup(["System Information and Monitoring"]) linux/BasicFileOperationsGroup -.-> linux/ls("Content Listing") ansible/ModuleOperationsGroup -.-> ansible/command("Execute Commands") ansible/ModuleOperationsGroup -.-> ansible/debug("Test Output") linux/BasicFileOperationsGroup -.-> linux/tail("File End Display") linux/FileandDirectoryManagementGroup -.-> linux/pwd("Directory Displaying") ansible/PlaybookEssentialsGroup -.-> ansible/playbook("Execute Playbook") linux/SystemInformationandMonitoringGroup -.-> linux/df("Disk Space Reporting") linux/SystemInformationandMonitoringGroup -.-> linux/service("Service Managing") subgraph Lab Skills linux/ls -.-> lab-290161{{"Ansible コマンドモジュール"}} ansible/command -.-> lab-290161{{"Ansible コマンドモジュール"}} ansible/debug -.-> lab-290161{{"Ansible コマンドモジュール"}} linux/tail -.-> lab-290161{{"Ansible コマンドモジュール"}} linux/pwd -.-> lab-290161{{"Ansible コマンドモジュール"}} ansible/playbook -.-> lab-290161{{"Ansible コマンドモジュール"}} linux/df -.-> lab-290161{{"Ansible コマンドモジュール"}} linux/service -.-> lab-290161{{"Ansible コマンドモジュール"}} end

シンプルなAnsibleプレイブックを作成する

このステップでは、Commandモジュールを使ってシンプルなコマンドを実行する最初のAnsibleプレイブックを作成します。

まず、プロジェクトディレクトリに移動します。

cd ~/project

次に、好きなテキストエディタを使って simple_command.yml という新しいファイルを作成します。たとえば、nano エディタを使うことができます。

nano simple_command.yml

ファイルに次の内容を追加します。

---
- name: Execute a simple command
  hosts: localhost
  tasks:
    - name: Run 'ls' command
      command: ls -l

このプレイブックを分解してみましょう。

  • hosts: localhost の行は、プレイブックがローカルマシンで実行されることを指定しています。
  • tasks セクションには実行するタスクのリストが含まれています。
  • command: ls -l の行は、Commandモジュールを使って ls -l コマンドを実行しており、長い形式でファイルとディレクトリを一覧表示します。

ファイルを保存してエディタを終了します(nanoでは、Ctrl+Xを押してからYを押し、その後Enterを押します)。

次に、次のコマンドでプレイブックを実行します。

ansible-playbook simple_command.yml

次のような出力が表示されるはずです。

PLAY [Execute a simple command] ************************************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [Run 'ls' command] ********************************************************
changed: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

この出力は、プレイブックが正常に実行され、ローカルマシンで ls -l コマンドが実行されたことを示しています。

Commandモジュールで変数を使用する

このステップでは、Ansible Commandモジュールで変数を使用する方法を学びます。変数を使うことで、プレイブックをより柔軟で再利用可能にすることができます。

variable_command.yml という新しいファイルを作成します。

nano variable_command.yml

ファイルに次の内容を追加します。

---
- name: Use variables with the Command module
  hosts: localhost
  vars:
    file_path: /etc/passwd
    line_count: 5
  tasks:
    - name: Display the last few lines of a file
      command: "tail -n {{ line_count }} {{ file_path }}"
      register: command_output

    - name: Show the command output
      debug:
        var: command_output.stdout_lines

このプレイブックではいくつかの新しい概念が導入されています。

  • vars セクションでは、プレイブック全体で使用できる変数を定義しています。
  • コマンド内で変数を参照するには {{ variable_name }} の構文を使用します。
  • register キーワードを使って、コマンドの出力を command_output という名前の変数に保存します。
  • debug モジュールを使って、command_output 変数の内容を表示します。

ファイルを保存してエディタを終了します。

次に、プレイブックを実行します。

ansible-playbook variable_command.yml

次のような出力が表示されるはずです。

PLAY [Use variables with the Command module] ***********************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [Display the last few lines of a file] ************************************
changed: [localhost]

TASK [Show the command output] *************************************************
ok: [localhost] => {
    "command_output.stdout_lines": [
        "games:x:5:60:games:/usr/games:/usr/sbin/nologin",
        "man:x:6:12:man:/var/cache/man:/usr/sbin/nologin",
        "lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin",
        "mail:x:8:8:mail:/var/mail:/usr/sbin/nologin",
        "news:x:9:9:news:/var/spool/news:/usr/sbin/nologin"
    ]
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

この出力は、/etc/passwd ファイルの最後の5行を表示しており、変数をCommandモジュールと一緒に使う方法を示しています。

コマンド出力をキャプチャして処理する

このステップでは、コマンドの出力をキャプチャし、Ansibleを使ってさらに処理する方法を学びます。

process_output.yml という新しいファイルを作成します。

nano process_output.yml

ファイルに次の内容を追加します。

---
- name: Capture and process command output
  hosts: localhost
  tasks:
    - name: Get disk usage information
      command: df -h
      register: df_output

    - name: Display all partitions
      debug:
        msg: "{{ df_output.stdout_lines }}"

    - name: Find root partition
      set_fact:
        root_partition: "{{ df_output.stdout_lines | select('match', '\\s+/$') | first | default('') }}"

    - name: Display root partition information
      debug:
        msg: "Root partition: {{ root_partition }}"
      when: root_partition!= ''

    - name: Extract usage percentage
      set_fact:
        root_usage: "{{ root_partition.split()[-2].rstrip('%') | int }}"
      when: root_partition!= ''

    - name: Display root partition usage
      debug:
        msg: "Root partition is {{ root_usage }}% full"
      when: root_partition!= ''

    - name: Check if root partition is over 80% full
      fail:
        msg: "Warning: Root partition is over 80% full!"
      when: root_partition!= '' and root_usage > 80

    - name: Display message if root partition not found
      debug:
        msg: "Root partition (/) not found in df output"
      when: root_partition == ''

このプレイブックは、ルートパーティションが簡単に検出できない場合に対応するため、より堅牢です。

  • 利用可能なパーティションを確認するために、すべてのパーティションを表示します。
  • ルートパーティションを見つけるために、より柔軟なパターンを使用します。
  • ルートパーティションが見つからない場合に対応するためのチェックを追加します。
  • ルートパーティションが見つからない場合にエラーを回避するために、default('') フィルタを使用します。

ファイルを保存してエディタを終了します。

次に、プレイブックを実行します。

ansible-playbook process_output.yml

次のような出力が表示されるはずです。

PLAY [Capture and process command output] ************************************************

TASK [Gathering Facts] *******************************************************************
ok: [localhost]

TASK [Get disk usage information] ********************************************************
changed: [localhost]

TASK [Display all partitions] ************************************************************
ok: [localhost] => {
    "msg": [
        "Filesystem      Size  Used Avail Use% Mounted on",
        "overlay          20G  618M   20G   4% /",
        "tmpfs            64M     0   64M   0% /dev",
        "tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup",
        "shm              64M  128K   64M   1% /dev/shm",
        "/dev/vdb        100G   17G   84G  17% /etc/hosts"
    ]
}

TASK [Find root partition] ***************************************************************
ok: [localhost]

TASK [Display root partition information] ************************************************
skipping: [localhost]

TASK [Extract usage percentage] **********************************************************
skipping: [localhost]

TASK [Display root partition usage] ******************************************************
skipping: [localhost]

TASK [Check if root partition is over 80% full] ******************************************
skipping: [localhost]

TASK [Display message if root partition not found] ***************************************
ok: [localhost] => {
    "msg": "Root partition (/) not found in df output"
}

PLAY RECAP *******************************************************************************
localhost                  : ok=7    changed=1    unreachable=0    failed=1    skipped=1    rescued=0    ignored=0

この出力は、すべてのパーティションを表示し、ルートパーティションを特定し、その使用状況をチェックします。システムによっては正確な値が異なる場合があります。

Commandモジュールのオプションを使う

このステップでは、Ansible Commandモジュールの動作を制御するためのいくつかのオプションを調べます。

command_options.yml という新しいファイルを作成します。

nano command_options.yml

ファイルに次の内容を追加します。

---
- name: Explore Command module options
  hosts: localhost
  tasks:
    - name: Run a command with a specific working directory
      command: pwd
      args:
        chdir: /tmp

    - name: Run a command with environment variables
      command: echo $MY_VAR
      environment:
        MY_VAR: "Hello from Ansible"

    - name: Run a command and ignore errors
      command: ls /nonexistent_directory
      ignore_errors: yes

    - name: Run a command with a timeout
      command: sleep 2
      async: 5
      poll: 0
      register: sleep_result

    - name: Check sleep command status
      async_status:
        jid: "{{ sleep_result.ansible_job_id }}"
      register: job_result
      until: job_result.finished
      retries: 5
      delay: 1

このプレイブックはCommandモジュールの様々なオプションを示しています。

  • chdir: コマンドを実行する前に作業ディレクトリを変更します。
  • environment: コマンド用の環境変数を設定します。
  • ignore_errors: コマンドが失敗してもプレイブックの実行を続行します。
  • asyncpoll: タイムアウト付きでコマンドを非同期で実行します。

ファイルを保存してエディタを終了します。

次に、プレイブックを実行します。

ansible-playbook command_options.yml

次のような出力が表示されるはずです。

PPLAY [Explore Command module options]

TASK [Gathering Facts]
ok: [localhost]

TASK [Run a command with a specific working directory]
changed: [localhost]

TASK [Run a command with environment variables]
changed: [localhost]

TASK [Run a command and ignore errors]
fatal: [localhost]: FAILED! => {"changed": true, "cmd": ["ls", "/nonexistent_directory"], "delta": "0:00:00.006113", "end": "2024-09-06 09:40:43.373350", "msg": "non-zero return code", "rc": 2, "start": "2024-09-06 09:40:43.367237", "stderr": "ls: cannot access '/nonexistent_directory': No such file or directory", "stderr_lines": ["ls: cannot access '/nonexistent_directory': No such file or directory"], "stdout": "", "stdout_lines": []}
...ignoring

TASK [Run a command with a timeout]
changed: [localhost]

TASK [Check sleep command status]
FAILED - RETRYING: Check sleep command status (10 retries left).
FAILED - RETRYING: Check sleep command status (9 retries left).
FAILED - RETRYING: Check sleep command status (8 retries left).
FAILED - RETRYING: Check sleep command status (7 retries left).
FAILED - RETRYING: Check sleep command status (6 retries left).
FAILED - RETRYING: Check sleep command status (5 retries left).
FAILED - RETRYING: Check sleep command status (4 retries left).
FAILED - RETRYING: Check sleep command status (3 retries left).
FAILED - RETRYING: Check sleep command status (2 retries left).
FAILED - RETRYING: Check sleep command status (1 retries left).
fatal: [localhost]: FAILED! => {"ansible_job_id": "5877920468.2517", "attempts": 10, "changed": false, "finished": 0, "started": 1}

PLAY RECAP

この出力は、調べたCommandモジュールのオプションの異なる動作を示しています。

Dockerに対応したシナリオでCommandモジュールを使用する

この最後のステップでは、より現実的なDockerに対応したシナリオでAnsible Commandモジュールを使用します。SSHサービスの状態を確認し、必要に応じて管理します。

check_service_docker.yml という新しいファイルを作成します。

nano check_service_docker.yml

ファイルに次の内容を追加します。

---
- name: Check and manage SSH service in Docker
  hosts: localhost
  become: yes ## これによりAnsibleがsudoを使用できるようになります
  tasks:
    - name: Check SSH service status
      command: service ssh status
      register: ssh_status
      ignore_errors: yes

    - name: Display SSH service status
      debug:
        msg: "SSH service status: {{ ssh_status.stdout }}"

    - name: Start SSH service if not running
      command: service ssh start
      when: ssh_status.rc!= 0

    - name: Verify SSH service is running
      command: service ssh status
      register: ssh_status_after

    - name: Display final SSH service status
      debug:
        msg: "SSH service status is now: {{ ssh_status_after.stdout }}"

    - name: Check if SSH port is listening
      command: netstat -tuln | grep :22
      register: ssh_port_check
      ignore_errors: yes

    - name: Display SSH port status
      debug:
        msg: "SSH port 22 is {{ 'open' if ssh_port_check.rc == 0 else 'closed' }}"

このプレイブックは以下のアクションを実行します。

  1. service コマンドを使用してSSHサービスの状態を確認します。
  2. サービスの現在の状態を表示します。
  3. サービスが実行されていない場合、サービスを起動します。
  4. 起動の可能性がある後、サービスの状態を検証します。
  5. サービスの最終状態を表示します。
  6. SSHポート(22)がリッスンしているかどうかを確認します。
  7. SSHポートの状態を表示します。

ファイルを保存してエディタを終了します。

次に、sudo権限でプレイブックを実行します。

sudo ansible-playbook check_service_docker.yml

次のような出力が表示されるはずです。

PLAY [Check and manage SSH service in Docker] *****************************************

TASK [Gathering Facts] ****************************************************************
ok: [localhost]

TASK [Check SSH service status] *******************************************************
changed: [localhost]

TASK [Display SSH service status] *****************************************************
ok: [localhost] => {
    "msg": "SSH service status: * sshd is running"
}

TASK [Start SSH service if not running] ***********************************************
skipping: [localhost]

TASK [Verify SSH service is running] **************************************************
changed: [localhost]

TASK [Display final SSH service status] ***********************************************
ok: [localhost] => {
    "msg": "SSH service status is now: * sshd is running"
}

TASK [Check if SSH port is listening] *************************************************
changed: [localhost]

TASK [Display SSH port status] ********************************************************
ok: [localhost] => {
    "msg": "SSH port 22 is open"
}

PLAY RECAP ****************************************************************************
localhost                  : ok=6    changed=3    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

この出力は、SSHサービスが既に実行されていたため、起動する必要がなかったことを示しています。プレイブックはサービスの状態を正常に確認および検証し、またSSHポートが開いていることも確認しました。

まとめ

この実験では、Ansible Commandモジュールの多様性と威力を探求しました。以下のことを学びました。

  1. 基本的なコマンドを実行するために、Commandモジュールを使用して簡単なAnsibleプレイブックを作成する。
  2. Commandモジュールで変数を使用して、プレイブックをより柔軟で再利用可能にする。
  3. コマンド出力をキャプチャして処理し、コマンド結果に基づいて判断を下すことができる。
  4. 作業ディレクトリの変更、環境変数の設定、エラーの処理など、Commandモジュールの動作を制御するための様々なオプションを使用する。
  5. システムサービスを確認および管理することで、現実世界のシナリオでCommandモジュールを適用する。

これらのスキルは、Ansibleを使ってシステム管理タスクを自動化し、効率的にリモートホストを管理するための堅牢な基盤を形成します。Ansibleを使い続けるうちに、Commandモジュールが自動化ツールキットの中で多用途なツールであることがわかるでしょう。

Commandモジュールは強力ですが、利用可能な場合には、専用のAnsibleモジュール(サービスの管理には service モジュールなど)を使用する方がよい場合が多いことを忘れないでください。これらの専用モジュールは、より良い冪等性を提供し、オフザシェルでより複雑なシナリオを処理することができます。

練習を続け、Ansibleの機能を探求して、自動化スキルをさらに向上させ、ITオペレーションを効率化しましょう。