8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

とりあえずのansibleサーバ構築~実行まで

Posted at

ansibleサーバ構築

  • 概要
▼ansibleサーバ
OS : CentOS7
IP : 192.168.119.20

▼ansibleクライアント
OS : CentOS7
IP : 192.168.119.21
  • 最新化
yum update
  • SElinuxの無効化
# getenforce 
Enforcing

# vi /etc/selinux/config
SELINUX=disabled

# reboot

# getenforce
Disabled
  • firewallの無効化
systemctl stop firewalld
systemctl disable firewalld
  • ansibleユーザの作成
useradd ansible
  • sudo 許可ユーザに設定
visudo
==========
追加
# User rules for ansible
ansible ALL=(ALL) NOPASSWD:ALL
==========
  • 公開鍵認証を作成
su - ansible
mkdir /home/ansible/.ssh
cd /home/ansible/.ssh
ssh-keygen -t rsa
==========
Enter file in which to save the key (/home/ansible/.ssh/id_rsa): [enter]
Enter passphrase (empty for no passphrase): [enter]
Enter same passphrase again: [enter]
==========
chmod 700 /home/ansible/.ssh
chown -R ansible:ansible /home/ansible/.ssh
  • epelインストール
sudo yum install epel-release
  • ansibleインストール(最新)
sudo yum install ansible --enablerepo=epel
  • ansibleインストール(バージョン指定)
sudo pip install ansible\==2.4.3

※こちらでインストールすると /etc/ansible は自動生成されない

  • バージョン確認
$ ansible --version
ansible 2.9.10
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Apr  2 2020, 13:16:51) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]

ansibleクライアントの設定

  • ユーザの作成
useradd ansible
  • sudo 許可ユーザに設定
visudo
==========
追加
# User rules for ansible
ansible ALL=(ALL) NOPASSWD:ALL
==========
  • サーバ側から公開鍵の転送
scp /home/ansible/.ssh/id_rsa.pub user@192.168.119.21:/var/tmp
  • 公開鍵認証を作成
su - ansible
mkdir /home/ansible/.ssh
cd /home/ansible/.ssh
mv /var/tmp/id_rsa.pub /home/ansible/.ssh/id_rsa.pub
mv id_rsa.pub authorized_keys
chmod 700 /home/ansible/.ssh
chmod 600 /home/ansible/.ssh/authorized_keys
chown -R ansible:ansible /home/ansible/.ssh
  • [ansibleサーバ]ログインできることの確認
ssh ansible@192.168.119.21

ansibleサーバ上で設定ファイルの変更

  • [ansibleサーバ]管理対象ホストの変更
# /etc/ansible配下の権限を変更
chown -R ansible:ansible /etc/ansible
su - ansible

# デフォルトのファイルを退避
cp -a /etc/ansible/hosts{,.old}

# ファイルの中身を空にする
:> /etc/ansible/hosts

# /etc/ansible/hosts に ansibleクライアントのIPアドレスの記載
vi /etc/ansible/hosts
==========
[ansible_client]
192.168.119.21
==========
# 疎通確認
ansible -m ping ansible_client
==========
192.168.119.21 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
==========

これでサーバをクライアントの疎通までできました。

簡単にクライアントの設定を変更

  • [ansibleサーバ]クライアントの設定変更内容を記載

内容は、firewallの停止を行う命令文を記載しました。

touch /etc/ansible/roles/ansible_client_setup.yml
vi /etc/ansible/roles/ansible_client_setup.yml
==========
---
- hosts: ansible_client
  become: true
  tasks:
    - name: service firewalld stop
      service: name=firewalld state=stopped
==========
  • [ansibleクライアント]firewallの起動を確認
service firewalld status
==========
Redirecting to /bin/systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since 月 2020-08-31 19:16:38 JST; 19h ago
     Docs: man:firewalld(1)
 Main PID: 679 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─679 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

 8月 31 19:16:37 ansible_client systemd[1]: Starting firewalld - dynamic firewall daemon...
 8月 31 19:16:38 ansible_client systemd[1]: Started firewalld - dynamic firewall daemon.
 8月 31 19:16:39 ansible_client firewalld[679]: WARNING: AllowZoneDrifting is enabled. This is considered an ...now.
Hint: Some lines were ellipsized, use -l to show in full.
==========
  • [ansibleサーバ]クライアントの設定をansibleから変更
cd /etc/ansible
ansible-playbook -i hosts roles/ansible_client_setup.yml
==========
PLAY [ansible_client] ***********************************************************************************************

TASK [Gathering Facts] **********************************************************************************************
ok: [192.168.119.21]

TASK [service firewalld stop] ***********************************************************************************
changed: [192.168.119.21]

PLAY RECAP **********************************************************************************************************
192.168.119.21             : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
==========
changed=1 になったので、変更がされました!
  • [ansibleクライアント]firewallの停止を確認
service firewalld status
==========
Redirecting to /bin/systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

 8月 31 19:16:37 ansible_client systemd[1]: Starting firewalld - dynamic firewall daemon...
 8月 31 19:16:38 ansible_client systemd[1]: Started firewalld - dynamic firewall daemon.
 8月 31 19:16:39 ansible_client firewalld[679]: WARNING: AllowZoneDrifting is enabled. This is considered an ...now.
 9月 01 15:07:36 ansible_client systemd[1]: Stopping firewalld - dynamic firewall daemon...
 9月 01 15:07:37 ansible_client systemd[1]: Stopped firewalld - dynamic firewall daemon.
Hint: Some lines were ellipsized, use -l to show in full.
==========

きちんと止まっていることが確認できました!

ちなみに2回目実行したら...?

  • [ansibleサーバ]同じ設定ファイルでクライアントの環境を変えずにansibleを再度実行
cd /etc/ansible
ansible-playbook -i hosts roles/ansible_client_setup.yml
==========
PLAY [ansible_client] ***********************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************************
ok: [192.168.119.21]

TASK [Turn off firewalld service] ***********************************************************************************************************************************************************************************************************
ok: [192.168.119.21]

PLAY RECAP **********************************************************************************************************************************************************************************************************************************
192.168.119.21             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0    
==========
すでにクライアント側のfirewallは停止状態にあるので、changeはされずにそのまま終わりました。

実行する際に躓いたところ

  • sudo権限
firewallの停止などミドルウェアの停止はsudo権限が必要になります。
sudo権限をつけるためには、/etc/ansible/roles/ansible_client_setup.yml
==========
become: true
==========
を、書いていないとansibleユーザでsudoなしで実行しているので、以下のようにタイムアウトとなってしまいます。
ansible-playbook -i hosts roles/ansible_client_setup.yml

PLAY [ansible_client] ***********************************************************************************************

TASK [Gathering Facts] **********************************************************************************************
ok: [192.168.119.21]

TASK [service firewalld stop] ***********************************************************************************
fatal: [192.168.119.21]: FAILED! => {"changed": false, "msg": "Unable to disable service firewalld: Failed to execute operation: 接続がタイムアウトしました\n"}

PLAY RECAP **********************************************************************************************************
192.168.119.21             : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

参考

8
8
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
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?