ノートブック - Ansible リファレンス
サイト管理者の個人的なメモ

[ Linux / Unix リファレンスガイド ] - [ ノートブック ] - [ ansible ]


Contents



サーバ構成自動化ツール Ansible に関するノート、備忘録

◆Ansibleとは
サーバ構築作業を自動化することが可能
構成手順は YAML によってテキストデータ化されるため、インフラのコード化 を実現する技術でもある

◆メリット
自動構築の対象サーバに特別なクライアントやエージェントを導入する必要がない
自動化でありながら、変数やキー入力によるインタラクティブな指示も可能
Unix/LinuxだけでなくWindows CiscoIOS Junos BIG-IP netapp など多くの
設定・操作を行うことが可能
AWS / Microsoft azure などクラウド環境もサポート

◆基礎知識
インベントリ (inventory) - ノード管理 / 構成対象のサーバのホスト名やIPアドレスなどを記述
プレイブック (playbook) - サーバ構築処理を記述したファイル
モジュール (module) - playbook で使用される命令や機能など

◆ansible リファレンス
公式サイトリファレンス
ansible-doc コマンド

◆インストール (CentOSの場合)
# yum -y install epel-release
# yum -y install ansible
# ansible --version

◆構成対象サーバの事前準備 (CentOS7の場合)
※ネットワーク設定(諸々の設定・DNS)およびSSHが動作していればOK

# vim /etc/sysconfig/network-scripts/ifcfg-enp0s3
# nmcli connection modify enp0s3 ipv4.dns 192.168.0.1
# echo "net.ipv6.conf.all.disable_ipv6 = 1" >> /etc/sysctl.conf
# systemctl restart network
# systemctl is-enabled sshd

◆ssh 鍵認証
# mkdir ~/.ssh
# touch ~/.ssh/config
# chmod 700 ~/.ssh/config
# ssh-keygen -t rsa
# ssh-copy-id <hostname>

◆ansible インベントリ
# mkdir /work/ansible
# vim /work/ansible/hosts
⇒ HOST および GROUP を定義する。ひとつのホストを複数のグループへ登録可能。
------------------------------------------
[http_servers]
192.168.0.201
192.168.0.202
[tomcat_servers]
192.168.0.203
[postgres]
192.168.0.204
[win2008r2_1]
192.168.0.205
[linux:children]
http_servers
tomcat_servers
postgres
[windows:children]
win2008r2_1

------------------------------------------
# touch /work/ansible/ansible.cfg
------------------------------------------
[defaults] hostfile = /work/ansible/hosts
------------------------------------------
# ansible all -m ping


◆プレイブックからサーバを自動構築
# touch /work/ansible/playbook1.yml
# vim /work/ansible/playbook1.yml
# ansible-playbook /work/ansible/playbook1.yml

◆ansible関連 コマンド
ansible
⇒ ansible -m <module_name>
-k デフォルトの公開鍵認証をパスワード認証で実行

# ansible all -m shell -a "<shell_commands>"

ansible-doc
⇒ モジュールの man。
# ansible-doc --list
# ansible-doc user

ansible-playbook
⇒ playbook よりサーバを自動構築する
 syntax-check list-hosts list-task などオプションも充実

ansible-console
ansible-galaxy
ansible-pull
ansible-vault


playbookの作成例 (あくまで練習用)

---
- hosts: all
  sudo: yes
  tasks:
    - name: add a new user
      user: name=admin password=password&2016!

- hosts: unix
  sudo: yes
  tasks:
    - name: disable selinux
      selinux: state=disabled
      when: ansible_os_family == 'RedHat'

    - name: stop firewalld
      service: name=firewalld state=stopped
      when: ansible_os_family == 'RedHat'

    - name: disable firewalld
      command: systemctl disable firewalld
      when: ansible_os_family == 'RedHat'

    - name: install adminpackages
      yum: name={{item}} state=latest
      with_items:
        - mlocate
        - iputils
        - bind-utils
      when: ansible_os_family == 'RedHat'

- hosts: http_servers
  sudo: yes
  tasks:
    - name: install apache
      yum: name=httpd state=latest

    - name: start apache and enabled
      service: name=httpd state=started enabled=yes

    - name: exec shell commands
      command: echo "hello ansible" > /root/hello.txt

    - name: copy index.html
      copy: src=./files/index.html dest=/var/www/html/index.html

    - name: install php packages
      yum: name={{item}} state=latest
      with_items:
        - php
        - php-devel
        - php-mbstring
        - php-pgsql
      notify:
        - restart httpd
    - name: copy hello.php
      copy: src=./files/hello.php dest=/var/www/html/hello.php
  handlers:
    - name: restart httpd
      service: name=httpd state=restarted

- hosts: tomcat_servers
  sudo: yes
  tasks:
    - name: install tomcat and java
      yum: name={{item}} state=latest
      with_items:
        - java-1.8.0-openjdk
        - java-1.8.0-openjdk-devel
        - tomcat
        - tomcat-webapps
        - tomcat-admin-webapps

    - name: start tomcat and enabled
      service: name=tomcat state=started enabled=yes

- hosts: postgres
  sudo: yes
  tasks:
    - name: install PostgreSQL
      yum: name={{item}} state=latest
      with_items:
        - postgresql-server
        - postgresql-devel
        - postgresql-contrib
        - postgresql

    - name: checking directory
      stat: path=/var/lib/pgsql/data
      register: pgdata

    - name: postgresql initdb
      become: True
      command: service postgresql initdb
      when: not pgdata.stat.exists

    - name: start postgresql and enabled
      service: name=postgresql state=started enabled=yes

                          
                          






以上




(c) 2012 - 2016 copyright oskp.net all rights reserved.