snippet: ansible

## Roleディレクトリレイアウトを一気に作る

```sh:role1は任意のロール名
ansible-galaxy init --offline role1
```

以下が一気に作られる。

```rb:
role1/README.md
role1/defaults/main.yml
role1/handlers/main.yml
role1/meta/main.yml
role1/tasks/main.yml
role1/tests/inventory
role1/tests/test.yml
role1/vars/main.yml
```

## memo

```bash:memo
# hosts作成
# ansible.cfg作成
# 実行
ansible-playbook install-python.yml
ansible-playbook setting-jst9.yml
ansible-playbook setting-repos.list
ansible-playbook apt-install.yml
ansible-playbook install-docker.yml
```


## 準備

* hostsで連番は[:]で範囲指定出来る。

```bash:hosts
[servers]
catc0[1:9]
```

```bash:ansible.cfg
[defaults]
inventory = hosts
nocows = 1
display_args_to_stdout = True

[ssh_connection]
ssh_args = -F ../../config
```

* ssh_args は絶対パスか相対パスで指定。$HOMEや~は使えなかった。
* ansible.cfgについては
    * /etc/ansible/ansible.cfg か
    * [Configuration file — Ansible Documentation](http://docs.ansible.com/ansible/intro_configuration.html#inventory)参照

```diff:display_args_to_stdoutオプション時の出力例
- < TASK [change /etc/timezone] >
+ / TASK [change /etc/timezone content=Asia/Tokyo, \
+ \ dest=/etc/timezone]                            /
 -----------------------------
```

## pythonのインストール(raw module)

```rb:install-python.yml
- name: raw
  hosts: servers
  gather_facts: no
  sudo: yes
  tasks:

    - name: install python
      raw: apt-get install -yq python
```

## JST-9 (copy, command module)

```yaml:CentOS7で確認
    ## timezone
    - name: set timezone to Asia/Tokyo
      timezone:
        name: Asia/Tokyo
```

* もしくはcopy moduleで文字列を書き込む

```rb:setting-jst9.yml
- hosts: servers
  gather_facts: no
  sudo: yes
  tasks:

   - name: change /etc/timezone
     copy: content=Asia/Tokyo dest=/etc/timezone

   - name: change timezone
     command: dpkg-reconfigure -f noninteractive tzdata

   - command: date
     sudo: no
```

```rb:面倒ならshellコマンドで
- hosts: servers
  gather_facts: no
  sudo: yes
  tasks:

   - name: set timezone
     shell: >
       grep "Asia/Tokyo" /etc/timezone \
       || (echo "Asia/Tokyo" > /etc/timezone && dpkg-reconfigure -f noninteractive tzdata)
       date
```

## repos設定 (copy, apt module)

```rb:setting-repos.yml
deb http://archive.ubuntu.com/ubuntu/ xenial main restricted
deb http://archive.ubuntu.com/ubuntu/ xenial multiverse
deb http://archive.ubuntu.com/ubuntu/ xenial universe
deb http://archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ xenial-updates main restricted
deb http://archive.ubuntu.com/ubuntu/ xenial-updates multiverse
deb http://archive.ubuntu.com/ubuntu/ xenial-updates universe
deb http://security.ubuntu.com/ubuntu xenial-security main restricted
deb http://security.ubuntu.com/ubuntu xenial-security multiverse
deb http://security.ubuntu.com/ubuntu xenial-security universe
```

```ruby:setting-repos.yml
- hosts: servers
  gather_facts: no
  sudo: yes
  tasks:

  - copy: src=setting-repos.list dest=/etc/apt/sources.list
  - apt: update_cache=yes
```

## packagesのインストール (apt module)

```ruby:apt-packages.yml 
- hosts: servers
  gather_facts: no
  sudo: yes
  tasks:

  - name: install packages
    apt: pkg={{ item }} state=installed
    with_items:
      - vim
      - w3m
      - nkf
```

## dockerのインストール (vars, name module)

```rb:install-docker.yml
- name: raw
  hosts: servers
  gather_facts: no
  sudo: yes
  vars:
    myuser: user1
    version: 1.8.0-rc1

  tasks:

    - name: install docker
      raw: wget -qO- https://get.docker.com/ | sh

    - name: join docker user
      user: name={{myuser}} groups=docker append=yes

    - name: install docker-compose
      shell: >
        test -e /usr/local/bin/docker-compose && exit 0 \
        || wget -O /usr/local/bin/docker-compose https://github.com/docker/compose/releases/download/{{versio
n}}/docker-compose-`uname -s`-`uname -m` \
           && chmod +x /usr/local/bin/docker-compose
```


## 手でやるやつ

```bash:
sudo vim /etc/hosts
sudo sh -c "echo catc0X > /etc/hostname"; sudo reboot
```