Last-modified: 2020-12-11 (金) 13:05:59 (1225d)

 すごく簡単なので、お気に入りです。

 

Ansibleの導入

導入するサーバの前提条件

  1. (Ansibleが導入可能な)任意のLinuxでよい。本稿ではCentOS 8を用いる。
  2. 事前導入するソフトウェアパッケージは最小構成。
  3. インターネットに接続可能 ※必要ならproxy設定
     /etc/dnf/dnf.conf
    proxy=http://proxy.jomura.net:8080/     #as your own
     ~/.bashrc
    export HTTP_PROXY=http://proxy.jomura.net:8080/     #as your own
    export HTTPS_PROXY=${HTTP_PROXY}
  4. パッケージの更新が事前に実行されている
    dnf clean all && dnf -y update && reboot

Ansibleのインストール

  • rootで実行
    dnf -y install epel-release
    dnf -y install ansible
    ansible --version
  • CentOS8だとansible 2.9.11。CentOS7だとansible 2.9.10。(2020年08月時点)

インベントリファイルの作成

  • 一般ユーザアカウントで実行
  • "#as your own"の行は、各自、値を編集してね
    cat << "_EOF_" > inventory.yml
    redmine_servers:
      hosts:
        its.jomura.net             #as your own
      vars:
        ansible_user: user         #as your own
        ansible_password: user     #as your own
        ansible_su_pass: root      #as your own
        proxy_env:
          no_proxy: 127.0.0.1,localhost  # do not delete
    #      http_proxy:  http://proxy.jomura.net:8080     #as your own
    #      https_proxy: http://proxy.jomura.net:8080     #as your own
    
    gitlab_servers:
      hosts:
        vcs.jomura.net             #as your own
      vars:
        ansible_user: user         #as your own
        ansible_password: user     #as your own
        ansible_su_pass: root      #as your own
        proxy_env:
          no_proxy: 127.0.0.1,localhost  # do not delete
          http_proxy:  http://proxy.jomura.net:8080     #as your own
          https_proxy: http://proxy.jomura.net:8080     #as your own
    
    jenkins_servers:
      hosts:
        build.jomura.net           #as your own
      vars:
        ansible_user: user         #as your own
        ansible_password: user     #as your own
        ansible_su_pass: root      #as your own
        proxy_env:
          no_proxy: 127.0.0.1,localhost  # do not delete
    #      http_proxy:  http://proxy.jomura.net:8080     #as your own
    #      https_proxy: http://proxy.jomura.net:8080     #as your own
    _EOF_

ssh公開鍵のfinger printの記録

  • playbookを実行する前に、各ホストにssh接続し、finger printをknown_hostsファイルに記録しておく
  • 一般ユーザアカウントで実行
    [user@localhost ~]$ ssh user@vcs.jomura.net
    The authenticity of host 'vcs.jomura.net (10.130.146.45)' can't be established.
    ECDSA key fingerprint is SHA256:yXhFg2AqmDebKZ3fHOwMbVyIijZIlNFQ5B750jcCo+g.
    ECDSA key fingerprint is MD5:0a:93:88:6e:7f:1f:dd:8c:44:92:af:ab:d5:a5:e6:64.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added 'vcs.jomura.net' (ECDSA) to the list of known hosts.

[option] 秘匿情報の暗号化

  • インベントリファイル等に、パスワード等の秘匿情報を平文で書いておきたくない場合は、ansible-vaultで暗号化する
  • 一般ユーザアカウントで実行
  1. [鍵ファイルの作成]
    echo 'PASSWORD' > ~/.ansible-vault-key
    chmod 400 ~/.ansible-vault-key
    • 'PASSWORD'は任意の鍵文字列
    • .ansible-vault-keyファイルを秘匿する必要がある
  2. [暗号化文字列の生成]
    #例
    echo -n 'user' | ansible-vault encrypt_string --vault-id ~/.ansible-vault-key --stdin-name 'ansible_password'
    • --stdin-name(暗号化する項目の名称)の指定は任意だが、書いた方がわかりやすい
      #例
      ansible_password: !vault |
                $ANSIBLE_VAULT;1.1;AES256
                64613333626230353935646131663335343964303838643835633662666662306361313464646635
                3235666362373830626465333036333738356566643133380a396465653735666361336238393138
                62313832333839316236326265653934333637393063643066333733333735653636373361303638
                3363303664353965660a396439643234363764396461326461383038323864313330336666616436
                6663
    • 出力された文字列で、inventory.ymlファイルの該当項目を置換する。
  3. [--vault-idオプションの付与]
    • 暗号化文字列に置換した場合は、以降のansible-playbook実行時に、鍵ファイルを指定する必要がある。(本稿では記述を省略している)
      #例
      ansible-playbook -i inventory.yml pb_jenkins_server.yml --vault-id ~/.ansible-vault-key

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS