• home
  • about
  • 全ての投稿
  • ソフトウェア・ハードウェアの設定のまとめ
  • 分析関連のまとめ
  • ヘルスケア関連のまとめ
  • 生涯学習関連のまとめ

ansible

date: 2023-04-01 excerpt: ansibleの使い方

tag: ansiblepython3


ansibleの使い方

概要

  • 複数のコンピュータの設定をまとめて行えるソフトウェア
  • inventory.yamlとplaybook.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

参考

  • Getting started with Ansible/docs.ansible.com
  • Ansible (ソフトウェア)/Wikipedia


ansiblepython3 Share Tweet