ansible

ansibleの使い方

タグ ansible


概要

  • 複数のコンピュータの設定をまとめて行えるソフトウェア
  • inventory.yamlplaybook.yamlの2つの設定ファイルが必要
    • inventory.yaml
      • 対象とするサーバのリストを記述する
    • playbook.yaml
      • 何をするかを記述する

インストール

macOS

$ brew install ansible

inventory.yamlの例

mymachines:
  hosts:
    kichijouji:
      ansible_host: gimpeik.duckdns.org
    akabane:
      ansible_host: akabane.duckdns.org

playbook.yamlの例

 - name: My playbook
   hosts: mymachines
   vars:
     this_is_env: "環境変数をここで設定可能"
   tasks:
    - name: Debug Messages
      # リモートの環境変数を呼び出し
      ansible.builtin.debug:
        msg: "'' is the HOME environment variable."
    - name: Ping my hosts
      ansible.builtin.ping:
    - name: pull latest changes
      # リモートにあるgit管理されているフォルダのgit pull
      # ref. https://stackoverflow.com/questions/55704077/git-pull-with-ansible
      shell: git pull
      args:
        chdir: "/.var/dots"
        executable: /bin/bash

inventoryのサーバにpingする

$ ansible <mymachines> -i inventory.yaml -m ping

inventoryのサーバにplaybookの内容を実行する

$ ansible-playbook -i inventory.yaml playbook.yaml

参考