読者です 読者をやめる 読者になる 読者になる

Ansible入門

Ansible

Ansibleとは

Chef, Puppetと同様の構成管理ツール

インストール

$ git clone git://github.com/ansible/ansible.git --recursive
$ cd ./ansible
$ source ./hacking/env-setup
$ ansible
Usage: ansible <host-pattern> [options]

Options:
  -a MODULE_ARGS, --args=MODULE_ARGS
                        module arguments
  --ask-become-pass     ask for privilege escalation password
...

リモートホストの管理

※事前にVagrant等でSSHログインできるホストを用意しておく

Inventoryの作成

$ vim ~/ansible_hosts

[webservers]
192.168.33.100 ansible_user=vagrant ansible_ssh_pass=vagrant

疎通確認

$ ansible all -m ping -i ~/ansible_hosts

192.168.33.100 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

しゃべらせてみる

$ ansible all -a "/bin/echo hello" -i ~/ansible_hosts

192.168.33.100 | SUCCESS | rc=0 >>
hello

リモートホストの構成

ex.) Apache

Playbook

# httpd.yml

---
- hosts: webservers
  vars:
    http_port: 80
    max_clients: 200
  become: true
  tasks:
  - name: ensure apache is at the latest version
    yum:
      name: httpd
      state: latest
  - name: ensure apache is running (and enable it at boot)
    service:
      name: httpd
      state: started
      enabled: yes
  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted

実行

$ ansible-playbook -i ~/ansible_hosts httpd.yml

PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [192.168.33.100]

TASK [ensure apache is at the latest version] **********************************
changed: [192.168.33.100]

TASK [ensure apache is running (and enable it at boot)] ************************
changed: [192.168.33.100]

PLAY RECAP *********************************************************************
192.168.33.100             : ok=3    changed=2    unreachable=0    failed=0

入門Ansible

入門Ansible