LoginSignup
3
2

More than 3 years have passed since last update.

Ansible構築から実行まで

Last updated at Posted at 2019-11-03

Ansible構築

1.EPEL追加

ansibleはEPELにあるらしい
EPEL(Extra Packages for Enterprise Linux)を追加する.

yum install epel-release

2.Ansible Install

yum install ansible

Ansible実行

1.localhost向けにpingテスト

ping moduleを使ってlocalhost向けに実行

[root@ansible ansible]# ansible -m ping localhost
localhost | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

-m:実行したいモジュール名を指定 

Module一覧

2.remoteサーバ向けにpingテスト

事前にAnsible実行サーバとremote間でssh key接続を有効化する

下記参考に実施
SSH公開鍵認証で接続するまで

また、今回は検証環境のためRootUserでのLoginを許可

/etc/ssh/sshd_config
PermitRootLogin yes

config変更後プロセスRestart

systemctl restart sshd

あとfingerprintの確認が出るとAnsibleがコケるので以下変更

/etc/ansible/ansible.cfg
host_key_checking = False

hostsファイル編集

hostsファイルはAnsibleのplaybookやmoduleを実行する際に実行ノード情報を記載するファイル
インベントリファイルと呼ばれる
[]でホストグループを指定し、その下に対応するIPアドレスやFQDNを記載する
Ansibleのインベントリファイルを参考にしました

/etc/ansible/hosts
[parent]
192.168.0.220

[grafana]
192.168.0.202

[zabbix]
192.168.0.204

[parent:children]
grafana
zabbix

ようやく実行

[root@ansible ansible]# ansible -m ping grafana
192.168.0.202 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
[root@ansible ansible]# ansible -m ping zabbix
192.168.0.204 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
[root@ansible ansible]# ansible -m ping parent
192.168.0.202 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.0.204 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.0.220 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

3.playbook動作確認

playbook作成

今回は指定したホストグループで
wget/httpd/bash-completion/bind-utilsの
最新をinstallする

/etc/ansible/simple.yml
- hosts: parent
  gather_facts: no

  tasks:
    - name: install modules
      yum:
        name: "{{ packages }}"
        disable_gpg_check: no
        state: latest
      vars:
        packages:
          - wget
          - httpd
          - bash-completion
          - bind-utils



hosts:インベントリファイルに記載されるホストグループ名を指定
tasks以下が実際の処理
name:task実行時の出力なので任意
yum:yum moduleを使用
yum-name:yum操作を行うpackages名 ※今回は変数で指定している
disable_gpg_check:パッケージの署名チェックを無効化
state:latest(最新ファイルをinstall) / absent(インストールされていない状態)

実行

[root@ansible ansible]# ansible-playbook -i hosts simple.yml

PLAY [parent] ***********************************************************************************************************************************************************************

TASK [install modules] **************************************************************************************************************************************************************
ok: [192.168.0.204]
ok: [192.168.0.202]
ok: [192.168.0.220]

PLAY RECAP **************************************************************************************************************************************************************************
192.168.0.202              : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.0.204              : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.0.220              : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

3
2
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
3
2