LoginSignup
13
14

More than 5 years have passed since last update.

Ansible 事始め

Last updated at Posted at 2016-02-17

Ansible のインストールを投稿したので、早速使ってみました。

Ansible は通信として SSH を利用します。
SSH はログイン認証としてパスワード認証、鍵認証があります。
セキュリティ上、鍵認証が推奨されますが、今回は簡易の為パスワード認証で実施します。

実行環境

  • CentOS 6.7
  • Ansible 2.0

まずは、疎通確認として ping を実施します。

# 対象サーバの情報を設定(今回は localhost(自サーバ)を設定)
$ echo localhost > hosts
# ansible 実行
# all: 対象サーバグループ指定
# -i: 対象サーバ情報設定ファイルを指定
# --ask-pass: ログインパスワードを入力
# -m: 実行モジュールを指定
$ ansible all -i hosts --ask-pass -m ping
SSH password:xxxxxxxx
localhost | FAILED! => {
    "failed": true,
    "msg": "ERROR! Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host."
}
# エラーが表示される場合は、SSH で一度ログインして対象サーバの公開鍵を登録
$ ssh localhost
・・・
Are you sure you want to continue connecting (yes/no)? yes
# SSH 接続を一度切断
$ exit
# ansible 再実行
$ ansible all -i hosts --ask-pass -m ping
SSH password: xxxxxxxx
localhost | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

ping に対して pong が返され、正常に実行されました。

次に Playbook を記述してみます。
Playbook は構築手順を記述したファイルです。
Ansible には Role という役割毎でファイルを分割する機能もあるのですが、今回は一つのファイルに全て詰め込んでみました。

今回は練習用として、私が投稿した Vagrant box 作成手順(CentOS 6.7) の設定手順を記載してみました。
参考までに。

- hosts: localhost
  # become: sudo での実行可否
  become: yes
  # gather_facts: サーバ情報取得可否
  gather_facts: no
  # 変数宣言
  vars:
    # vagrant 用ユーザ名
    vagrant_user_name: vagrant
    # vagrant 用ユーザの参加グループ
    vagrant_user_group: wheel
    # vagrant 用ユーザのパスワード
    vagrant_user_password: vagrant
    # パスワードハッシュ化用ランダム変数(salt)
    # salt 生成用コマンド : cat /dev/urandom | tr -dc 'a-zA-Z-9' | head -c 16
    password_salt: ################
  # 実行タスク
  tasks:
    - name: libselinux のインストール(ansible から SELinux 設定変更の為)
      yum:
        name: libselinux-python
        state: present
    - name: eth0 設定変更(起動時の有効化)と再起動
      lineinfile:
        dest: /etc/sysconfig/network-scripts/ifcfg-eth0
        regexp: 'ONBOOT=no'
        line: 'ONBOOT=yes'
        state: present
      notify: restart network
    - name: SELinux の設定を無効化(開発環境のため)
      selinux: state=disabled
    - name: vagrant ユーザパスワード生成(変数 salt を利用)
      shell: python -c 'import crypt; print crypt.crypt("{{ vagrant_user_password }}", "$6${{ password_salt }}$")'
      changed_when: false
      register: password
    - name: vagrant ユーザ作成
      user:
        name: "{{ vagrant_user_name }}"
        group: "{{ vagrant_user_group }}"
        password: "{{ password.stdout }}"
    - name: SSH 公開鍵配置ディレクトリの作成
      file:
        path: /home/{{ vagrant_user_name }}/.ssh
        state: directory
        owner: "{{ vagrant_user_name }}"
        group: "{{ vagrant_user_group }}"
        mode: 0700
    - name: 公開鍵(InSecure KeyPair)のダウンロード
      get_url:
        url: https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub
        dest: /home/{{ vagrant_user_name }}/.ssh/authorized_keys
    - name: 公開鍵の権限設定
      file:
        path: /home/{{ vagrant_user_name }}/.ssh/authorized_keys
        owner: "{{ vagrant_user_name }}"
        group: "{{ vagrant_user_group }}"
        mode: 0600
    - name: sshd 設定変更(DNS 利用無効化)
      lineinfile:
        dest: /etc/ssh/sshd_config
        backrefs: yes
        regexp: '^#(UseDNS)(.*)(yes)'
        line: '\1\2no'
      notify: restart sshd
    - name: sudo 設定変更(requiretty / NOPASSWD の設定)
      lineinfile:
        dest: /etc/sudoers
        backrefs: yes
        regexp: '{{ item.regexp}}'
        line: '{{ item.line }}'
      with_items:
        - regexp: '^(Defaults)(.*)(requiretty)'
          line: '# \1\2\3'
        - regexp: '^# (%wheel)(.*)(NOPASSWD)(.*)'
          line: '\1\2\3\4'
    - name: iptables の無効化
      service:
        name: iptables
        state: stopped
        enabled: no
    - name: ip6tables の無効化
      service:
        name: ip6tables
        state: stopped
        enabled: no
    - name: epel レポジトリのインストール
      yum:
        name: epel-release
        state: present
    - name: ius レポジトリのインストール
      yum:
        name: https://centos6.iuscommunity.org/ius-release.rpm
        state: present
    - name: remi レポジトリのインストール
      yum:
        name: http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
        state: present
    - name: インストール済みパッケージのアップデート
      yum:
        name: "*"
        state: latest
    - name: 開発環境用パッケージをインストール
      yum:
        name: "@Development tools"
        state: present
    - name: 追加で vagrant 開発に必要なパッケージをインストール
      yum:
        name: "{{ item }}"
        state: present
      with_items:
        - man
        - man-pages
        - man-pages-overrides
        - man-pages-ja
        - wget
        - kernel-devel
# VirtualBoxLinuxAddtionsをインストール(一度のみ)
# インストール前に再起動が必要なため、コメントアウトしてあります。
# また、VirtualBoxLinuxAddtions のメディアを挿入する必要有
#   - name: CD イメージをマウントするディレクトリの作成
#     file:
#       path: /media/cdrom
#       state: directory
#   - name: CD イメージをマウント
#     mount:
#       name: /media/cdrom
#       src: /dev/cdrom
#       fstype: iso9660
#       opts: ro
#       state: mounted
#    - name: VirtualBox Guest Addtions のインストール
#      shell: sh /media/cdrom/VBoxLinuxAdditions.run
#      changed_when: false
#      ignore_errors: true
#    - name: CDイメージをアンマウント
#      mount:
#        name: /media/cdrom
#        src: /dev/cdrom
#        fstype: iso9660
#        state: unmounted
#   - name: fstab から削除
#     mount:
#       name: /media/cdrom
#       src: /dev/cdrom
#       fstype: iso9660
#       state: absent
    - name: 古い git パッケージをアンインストール
      yum:
        name: git
        state: absent
    - name: git に追加で必要なパッケージをインストール
      yum:
        name: "{{ item }}"
        state: present
      with_items:
        - curl-devel
        - expat-devel
        - gettext-devel
        - openssl-devel
        - zlib-devel
        - perl-ExtUtils-MakeMaker
    - name: 最新の git のソースファイルをダウンロード
      get_url:
        url: https://www.kernel.org/pub/software/scm/git/git-2.7.0.tar.gz
        dest: /usr/local/src
    - name: ダウンロードした git のソースファイルを解凍
      unarchive:
        src: /usr/local/src/git-2.7.0.tar.gz
        dest: /usr/local/src/
        creates: /usr/local/src/git-2.7.0
    - name: git のインストール
      shell: ./configure && make && make install
      args:
        chdir: /usr/local/src/git-2.7.0
        creates: /usr/local/bin/git
    - name: udev ルールの削除
      file:
        src: /dev/null
        dest: /etc/udev/rules.d/70-persistent-net.rules
        force: yes
        state: link
    - name: eth0 設定変更(ネットワーク個別設定削除)
      lineinfile:
        dest: /etc/sysconfig/network-scripts/ifcfg-eth0
        backrefs: yes
        regexp: '{{ item.regexp}}'
        line: '{{ item.line }}'
      with_items:
        - regexp: 'HWADDR=.*'
          line: ''
        - regexp: 'UUID=.*'
          line: ''
  handlers:
    - name: restart network
      service: name=network state=restarted
      changed_when: false
    - name: restart sshd
      service: name=sshd state=restarted
      changed_when: false

以上となります。

Playbook の書き方については他の方の記述方法を参考にすると良いと思います。
次は Role を導入したいと考えています。

13
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
14