【Ansible】1.Ansibleのインストール


はじめに

AnsibleはOSの設定や構成管理を自動化するオープンソースのツールです。
主にLinuxで利用されていますが、モジュールを追加することによりWindows、vSphere
さらには、パブリッククラウド環境でも利用することが出来ます。

インストールの準備

ますは、Ansibleをインストールしてみましょう。
管理する側をコントロールノードと言い、CentOSやRedhatなどの主要なLinuxディストリビューションにインストールすることが出来ます。
今回は下記のOSをコントロールノードとして利用しています。

[root@centos8 ~]# cat /etc/redhat-release
CentOS Linux release 8.2.2004 (Core)

python、モジュール管理ツールpipをインストールしておきます。

[root@centos8 ~]# dnf install python38 -y
[root@centos8 ~]# cat << EOS >> .bash_profile
> alias python=python3
> EOS
[root@centos8 ~]# source .bash_profile
[root@centos8 ~]# python --version
Python 3.8.0
[root@centos8 ~]# curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
[root@centos8 ~]# python get-pip.py --user
[root@centos8 ~]# cp /root/.local/bin/pip /usr/local/bin

インストール

ansibleのインストールには下記の方法があります。

  • パッケージマネージャから
  • pipから
  • ソースコードから

今回はパッケージマネージャからインストールします。
インストールにあたりEPEL(Extra Package Enterprise Linux)を有効にします。

[root@centos8 ~]# dnf install epel-release -y
[root@centos8 ~]# dnf install ansible -y
[root@centos8 ~]# ansible --version
ansible 2.9.15
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Apr 16 2020, 01:36:27) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]

インストールの確認

Hello worldを出力する簡単なplaybookを作成し、動作確認とします。

[root@centos8 ~]# mkdir ansible
[root@centos8 ~]# cd ansible/
[root@centos8 ansible]# cat inventory
[target]
localhost
[root@centos8 ansible]# cat main.yaml
- hosts: target
  gather_facts: no
  tasks:
    - name: Test ansible-playbook
      debug: msg="Hello world"
[root@centos8 ansible]# ansible-playbook -i inventory main.yaml

PLAY [target] **************************************************************************************

TASK [Test ansible-playbook] ***********************************************************************
ok: [localhost] => {
    "msg": "Hello world"
}

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