ansibleの使い方

install

brew install ansible

接続確認

❯ cat hosts
[server]
xxx.xxx.xxx.xxx

実行

❯ ansible server -i hosts -m ping -u ubuntu
18.183.206.58 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

playbook適用

❯ cat playbook.yml
---
- name: Update web servers
  hosts: server
  remote_user: ubuntu
  become: true

  tasks:
  - name: Run the equivalent of "apt-get update" as a separate step
    ansible.builtin.apt:
      update_cache: yes

syntax-check

ansible-playbook playbook.yml --syntax-check

dryrun

ansible-playbook playbook.yml -i hosts -C -D

適用

ansible-playbook playbook.yml -i hosts

playbookの探し方

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/apt_module.html

こんな感じで下の方にサンプルがあるのでそれをコピペする

Best Practices

ディレクトリ構造とか読むと良い

https://docs.ansible.com/ansible/2.9_ja/user_guide/playbooks_best_practices.html