Ansible[インストール〜接続確認]

EPEL設定は省略
***
#環境
* Amazon Linux AMI release 2016.03
* ansible-2.0.1.0-2.el6.noarch

#設定ファイル
* inventory
 * /etc/ansible/hostsへホストを管理しやすいようグルーピングした定義をすることでコマンドラインオプションで指定しなくてもすむ
* 定義するホストは正規表現や論理積・論理和等も使用可能

```text:example
abcde*.example.com

#abcde01~99まで
abcde[01:99].example.com

#prd or dev
prd:dev

#prdのうち、dev以外
prd!dev

#prdかつdev
prd:&db
```

* playbook
 * taskをまとめて記述

* ansible.cfg
 * ansible自体の設定ファイル

***

#構成
* Ansibleを実行する管理ホスト
* Ansibleで管理される対象ホスト
    * 管理ホスト→対象ホストへはsshできる必要がある
* Ansible実行には両ホストでPython2.6以上が必要

***
#設定
##Ansibleインストール
```bash
$ sudo yum install ansible --enablerepo=epel
$ rpm -qi ansible
Name        : ansible
Version     : 2.0.1.0
Release     : 2.el6
Architecture: noarch
Install Date: Mon 11 Apr 2016 09:34:58 AM UTC
Group       : Development/Libraries
Size        : 12887571
License     : GPLv3+
Signature   : RSA/SHA256, Fri 26 Feb 2016 03:08:18 PM UTC, Key ID 3b49df2a0608b895
Source RPM  : ansible-2.0.1.0-2.el6.src.rpm
Build Date  : Thu 25 Feb 2016 10:21:27 PM UTC
Build Host  : buildhw-04.phx2.fedoraproject.org
Relocations : (not relocatable)
Packager    : Fedora Project
Vendor      : Fedora Project
URL         : http://ansible.com
Summary     : SSH-based configuration management, deployment, and task execution system
Description :

Ansible is a radically simple model-driven configuration management,
multi-node deployment, and remote task execution system. Ansible works
over SSH and does not require any software or daemons to be installed
on remote nodes. Extension modules can be written in any language and
are transferred to managed machines automatically.
```

##inventory
inventoryにグルーピング、そのグループが使用するメタデータを定義しておくことで、コマンドラインオプションは不要

```bash:/etc/ansible/hosts

[prd-server]
10.0.1.68

[dev_server]
10.0.0.26
10.0.0.34

[prd-server:vars]
ansible_port=22
ansible_user=<user>
ansible_ssh_private_key_file=/home/<user>/.ssh/ansible_rsa

[dev-server:vars]
ansible_port=22
ansible_user=<user>
ansible_ssh_private_key_file=/home/<user>/.ssh/ansible_rsa
```

* Amazon Linux AMI release 2015.03からはPython2.7がデフォルトになっているため、Python2.6の依存関係でansible実行時にエラーが発生する

```bash:error
Traceback (most recent call last):
  File "/usr/bin/ansible", line 39, in <module>
    from ansible.errors import AnsibleError, AnsibleOptionsError, AnsibleParserError
ImportError: No module named ansible.errors
```

* Python2.7からPython2.6に切り替える

```bash
$ sudo alternatives --config python

There are 2 programs which provide 'python'.

  Selection    Command
-----------------------------------------------
*+ 1           /usr/bin/python2.7
   2           /usr/bin/python2.6

Enter to keep the current selection[+], or type selection number: 

$ sudo alternatives --set python /usr/bin/python2.6
$ sudo alternatives --config python

There are 2 programs which provide 'python'.

  Selection    Command
-----------------------------------------------
*  1           /usr/bin/python2.7
 + 2           /usr/bin/python2.6
```

* 書式

```text
Usage: ansible <host-pattern> [options]

Options:
  -a MODULE_ARGS, --args=MODULE_ARGS
                        module arguments
  --ask-become-pass     ask for privilege escalation password
  -k, --ask-pass        ask for connection password
  --ask-su-pass         ask for su password (deprecated, use become)
  -K, --ask-sudo-pass   ask for sudo password (deprecated, use become)
  --ask-vault-pass      ask for vault password
  -B SECONDS, --background=SECONDS
                        run asynchronously, failing after X seconds
                        (default=N/A)
  -b, --become          run operations with become (nopasswd implied)
  --become-method=BECOME_METHOD
                        privilege escalation method to use (default=sudo),
                        valid choices: [ sudo | su | pbrun | pfexec | runas |
                        doas ]
  --become-user=BECOME_USER
                        run operations as this user (default=root)
  -C, --check           don't make any changes; instead, try to predict some
                        of the changes that may occur
  -c CONNECTION, --connection=CONNECTION
                        connection type to use (default=smart)
  -D, --diff            when changing (small) files and templates, show the
                        differences in those files; works great with --check
  -e EXTRA_VARS, --extra-vars=EXTRA_VARS
                        set additional variables as key=value or YAML/JSON
  -f FORKS, --forks=FORKS
                        specify number of parallel processes to use
                        (default=5)
  -h, --help            show this help message and exit
  -i INVENTORY, --inventory-file=INVENTORY
                        specify inventory host path
                        (default=/etc/ansible/hosts) or comma separated host
                        list.
  -l SUBSET, --limit=SUBSET
                        further limit selected hosts to an additional pattern
  --list-hosts          outputs a list of matching hosts; does not execute
                        anything else
  -m MODULE_NAME, --module-name=MODULE_NAME
                        module name to execute (default=command)
  -M MODULE_PATH, --module-path=MODULE_PATH
                        specify path(s) to module library (default=None)
  --new-vault-password-file=NEW_VAULT_PASSWORD_FILE
                        new vault password file for rekey
  -o, --one-line        condense output
  --output=OUTPUT_FILE  output file name for encrypt or decrypt; use - for
                        stdout
  -P POLL_INTERVAL, --poll=POLL_INTERVAL
                        set the poll interval if using -B (default=15)
  --private-key=PRIVATE_KEY_FILE, --key-file=PRIVATE_KEY_FILE
                        use this file to authenticate the connection
  --scp-extra-args=SCP_EXTRA_ARGS
                        specify extra arguments to pass to scp only (e.g. -l)
  --sftp-extra-args=SFTP_EXTRA_ARGS
                        specify extra arguments to pass to sftp only (e.g. -f,
                        -l)
  --ssh-common-args=SSH_COMMON_ARGS
                        specify common arguments to pass to sftp/scp/ssh (e.g.
                        ProxyCommand)
  --ssh-extra-args=SSH_EXTRA_ARGS
                        specify extra arguments to pass to ssh only (e.g. -R)
  -S, --su              run operations with su (deprecated, use become)
  -R SU_USER, --su-user=SU_USER
                        run operations with su as this user (default=root)
                        (deprecated, use become)
  -s, --sudo            run operations with sudo (nopasswd) (deprecated, use
                        become)
  -U SUDO_USER, --sudo-user=SUDO_USER
                        desired sudo user (default=root) (deprecated, use
                        become)
  --syntax-check        perform a syntax check on the playbook, but do not
                        execute it
  -T TIMEOUT, --timeout=TIMEOUT
                        override the connection timeout in seconds
                        (default=10)
  -t TREE, --tree=TREE  log output to this directory
  -u REMOTE_USER, --user=REMOTE_USER
                        connect as this user (default=None)
  --vault-password-file=VAULT_PASSWORD_FILE
                        vault password file
  -v, --verbose         verbose mode (-vvv for more, -vvvv to enable
                        connection debugging)
  --version             show program's version number and exit
```

* 実行

```bash
$ ansible prd-server -m setup
(snip)
10.0.1.68 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "10.0.1.68"
        ],
        "ansible_all_ipv6_addresses": [
            "fe80::81a:92ff:fe62:b30b"
        ],
        "ansible_architecture": "x86_64",
        "ansible_bios_date": "12/07/2015",
        "ansible_bios_version": "4.2.amazon",
        "ansible_cmdline": {
            "BOOT_IMAGE": "/boot/vmlinuz-3.10.0-327.13.1.el7.x86_64",
            "LANG": "en_US.UTF-8",
            "console": "ttyS0,115200",
            "crashkernel": "auto",
            "ro": true,
            "root": "UUID=ef6ba050-6cdc-416a-9380-c14304d0d206"
        },
(snip)
```