砂場

調べ物用

Ansibleメモ

homebrew

省略

Ansible

インストール

1
$ brew install ansible

接続先の設定

1
2
3
4
$ mkdir ansible
$ mkdir ansible/inventory
$ cd ansible
$ vi inventory/hosts

vagrantなどでsshのポート番号をデフォルト値以外にしている場合、ansible_portで指定する

1
2
[targets]
127.0.0.1 ansible_port=2222

疎通確認

1
2
3
4
5
$ ansible all -i inventory/hosts -u vagrant -m ping
127.0.0.1 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

Playbook作成

1
2
3
4
5
6
7
8
9
10
11
12
$ vi myplaybook.yml
- hosts: targets
  become: yes
  tasks:
  - name: install git
    apt:
      name: git
      update_cache: yes
  - name: install cmake
    apt:
      name: cmake
      update_cache: yes

文法チェック

1
2
3
$ ansible-playbook -i hosts myplaybook.yml --syntax-check

playbook: myplaybook.yml

タスクリスト表示

1
2
3
4
5
6
7
$  ansible-playbook -i hosts myplaybook.yml --list-tasks

playbook: myplaybook.yml

  play #1 (targets): targets  TAGS: []
    tasks:
      install tools   TAGS: []

dry-run

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ ansible-playbook -i hosts -u vagrant myplaybook.yml --check

PLAY [targets] *****************************************************************

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

TASK [install git] *************************************************************
ok: [127.0.0.1]

TASK [install cmake] ***********************************************************
changed: [127.0.0.1]

PLAY RECAP *********************************************************************
127.0.0.1                  : ok=3    changed=1    unreachable=0    failed=0 

実行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ansible-playbook -i hosts -u vagrant myplaybook.yml

PLAY [targets] *****************************************************************

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

TASK [install git] *************************************************************
ok: [127.0.0.1]

TASK [install cmake] ***********************************************************
changed: [127.0.0.1]

PLAY RECAP *********************************************************************
127.0.0.1                  : ok=3    changed=1    unreachable=0    failed=0