CentOS7にAnsibleをインストールする

IT技術

はじめに

構成管理ツールの一つである「Ansible」をインストールする。

システム環境

  • Ansibleサーバ(ansibleserver)
    • OS:CentOS 7.9
    • Ansible:2.9.27
  • Ansible管理対象(ansibleclient1,2)
    • OS:CentOS 7.9

インストール手順

Ansible用yumリポジトリのインストール

CentOSの標準リポジトリにはansibleが存在しない。まずはAnsible用yumリポジトリ「centos-release-ansible-29.noarch」をインストールする。
ansibleserverにて実行する。

[root@ansibleserver /]# yum search ansible
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: centos-distro.cavecreek.net
 * extras: repos.lax.layerhost.com
 * updates: mirror.vacares.com
========================================== N/S matched: ansible ==========================================
ansible-collection-microsoft-sql.noarch : The Ansible collection for Microsoft SQL Server management
centos-release-ansible-27.noarch : Ansible 2.7 packages from the CentOS ConfigManagement SIG repository
centos-release-ansible-28.noarch : Ansible 2.8 packages from the CentOS ConfigManagement SIG repository
centos-release-ansible-29.noarch : Ansible 2.9 packages from the CentOS ConfigManagement SIG repository
centos-release-ansible26.noarch : Ansible 2.6 packages from the CentOS ConfigManagement SIG repository
  Name and summary matches only, use "search all" for everything.
[root@ansibleserver /]# 
[root@ansibleserver /]# yum install centos-release-ansible-29.noarch
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: centos-distro.cavecreek.net
 * extras: repos.lax.layerhost.com
 * updates: mirror.vacares.com
Resolving Dependencies
--> Running transaction check
---> Package centos-release-ansible-29.noarch 0:1-1.el7 will be installed
--> Processing Dependency: centos-release-configmanagement for package: centos-release-ansible-29-1-1.el7.noarch
--> Running transaction check
---> Package centos-release-configmanagement.noarch 0:1-1.el7.centos will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==========================================================================================================
 Package                                  Arch            Version                   Repository       Size
==========================================================================================================
Installing:
 centos-release-ansible-29                noarch          1-1.el7                   extras          3.9 k
Installing for dependencies:
 centos-release-configmanagement          noarch          1-1.el7.centos            extras          4.3 k
Transaction Summary
==========================================================================================================
Install  1 Package (+1 Dependent package)
Total download size: 8.2 k
Installed size: 2.2 k
Is this ok [y/d/N]: y
Downloading packages:
(1/2): centos-release-ansible-29-1-1.el7.noarch.rpm                                | 3.9 kB  00:00:01     
(2/2): centos-release-configmanagement-1-1.el7.centos.noarch.rpm                   | 4.3 kB  00:00:01     
----------------------------------------------------------------------------------------------------------
Total                                                                     4.9 kB/s | 8.2 kB  00:00:01     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : centos-release-configmanagement-1-1.el7.centos.noarch                                  1/2 
  Installing : centos-release-ansible-29-1-1.el7.noarch                                               2/2 
  Verifying  : centos-release-ansible-29-1-1.el7.noarch                                               1/2 
  Verifying  : centos-release-configmanagement-1-1.el7.centos.noarch                                  2/2 
Installed:
  centos-release-ansible-29.noarch 0:1-1.el7                                                              
Dependency Installed:
  centos-release-configmanagement.noarch 0:1-1.el7.centos                                                 
Complete!
[root@ansibleserver /]# 

Ansible用yumリポジトリのインストール後、再度「yum search」コマンドで検索すると、「ansible.noarch」が表示されるようになる。

[root@ansibleserver /]# yum search ansible
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: centos-distro.cavecreek.net
 * centos-ansible-29: centos-distro.cavecreek.net
 * extras: repos.lax.layerhost.com
 * updates: mirror.vacares.com
========================================== N/S matched: ansible ==========================================
ansible-collection-microsoft-sql.noarch : The Ansible collection for Microsoft SQL Server management
ansible-test.noarch : Tool for testing ansible plugin and module code
centos-release-ansible-27.noarch : Ansible 2.7 packages from the CentOS ConfigManagement SIG repository
centos-release-ansible-28.noarch : Ansible 2.8 packages from the CentOS ConfigManagement SIG repository
centos-release-ansible-29.noarch : Ansible 2.9 packages from the CentOS ConfigManagement SIG repository
centos-release-ansible26.noarch : Ansible 2.6 packages from the CentOS ConfigManagement SIG repository
ansible.noarch : SSH-based configuration management, deployment, and task execution system
  Name and summary matches only, use "search all" for everything.
[root@ansibleserver /]# 

Ansibleインストール

続けてAnsibleをインストールする。

[root@ansibleserver /]# yum install ansible
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: centos-distro.cavecreek.net
 * centos-ansible-29: centos-distro.cavecreek.net
 * extras: repos.lax.layerhost.com
 * updates: mirror.vacares.com
Resolving Dependencies
--> Running transaction check
---> Package ansible.noarch 0:2.9.27-1.el7 will be installed
--> Processing Dependency: PyYAML for package: ansible-2.9.27-1.el7.noarch
--> Processing Dependency: python-jinja2 for package: ansible-2.9.27-1.el7.noarch
--> Processing Dependency: python-paramiko for package: ansible-2.9.27-1.el7.noarch
--> Processing Dependency: python-six for package: ansible-2.9.27-1.el7.noarch
--> Processing Dependency: python2-cryptography for package: ansible-2.9.27-1.el7.noarch
--> Processing Dependency: sshpass for package: ansible-2.9.27-1.el7.noarch
--> Running transaction check
---> Package PyYAML.x86_64 0:3.10-11.el7 will be installed
--> Processing Dependency: libyaml-0.so.2()(64bit) for package: PyYAML-3.10-11.el7.x86_64
---> Package python-jinja2.noarch 0:2.7.2-4.el7 will be installed
--> Processing Dependency: python-babel >= 0.8 for package: python-jinja2-2.7.2-4.el7.noarch
--> Processing Dependency: python-markupsafe for package: python-jinja2-2.7.2-4.el7.noarch
---> Package python-paramiko.noarch 0:2.1.1-9.el7 will be installed
--> Processing Dependency: python2-pyasn1 for package: python-paramiko-2.1.1-9.el7.noarch
---> Package python-six.noarch 0:1.9.0-2.el7 will be installed
---> Package python2-cryptography.x86_64 0:1.7.2-2.el7 will be installed
--> Processing Dependency: python-idna >= 2.0 for package: python2-cryptography-1.7.2-2.el7.x86_64
--> Processing Dependency: python-cffi >= 1.4.1 for package: python2-cryptography-1.7.2-2.el7.x86_64
--> Processing Dependency: python-setuptools for package: python2-cryptography-1.7.2-2.el7.x86_64
--> Processing Dependency: python-ipaddress for package: python2-cryptography-1.7.2-2.el7.x86_64
--> Processing Dependency: python-enum34 for package: python2-cryptography-1.7.2-2.el7.x86_64
--> Processing Dependency: openssl for package: python2-cryptography-1.7.2-2.el7.x86_64
---> Package sshpass.x86_64 0:1.06-2.el7 will be installed
--> Running transaction check
---> Package libyaml.x86_64 0:0.1.4-11.el7_0 will be installed
---> Package openssl.x86_64 1:1.0.2k-22.el7_9 will be installed
--> Processing Dependency: openssl-libs(x86-64) = 1:1.0.2k-22.el7_9 for package: 1:openssl-1.0.2k-22.el7_9.x86_64
--> Processing Dependency: make for package: 1:openssl-1.0.2k-22.el7_9.x86_64
---> Package python-babel.noarch 0:0.9.6-8.el7 will be installed
---> Package python-cffi.x86_64 0:1.6.0-5.el7 will be installed
--> Processing Dependency: python-pycparser for package: python-cffi-1.6.0-5.el7.x86_64
---> Package python-enum34.noarch 0:1.0.4-1.el7 will be installed
---> Package python-idna.noarch 0:2.4-1.el7 will be installed
---> Package python-ipaddress.noarch 0:1.0.16-2.el7 will be installed
---> Package python-markupsafe.x86_64 0:0.11-10.el7 will be installed
---> Package python-setuptools.noarch 0:0.9.8-7.el7 will be installed
--> Processing Dependency: python-backports-ssl_match_hostname for package: python-setuptools-0.9.8-7.el7.noarch
---> Package python2-pyasn1.noarch 0:0.1.9-7.el7 will be installed
--> Running transaction check
---> Package make.x86_64 1:3.82-24.el7 will be installed
---> Package openssl-libs.x86_64 1:1.0.2k-19.el7 will be updated
---> Package openssl-libs.x86_64 1:1.0.2k-22.el7_9 will be an update
---> Package python-backports-ssl_match_hostname.noarch 0:3.5.0.1-1.el7 will be installed
--> Processing Dependency: python-backports for package: python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch
---> Package python-pycparser.noarch 0:2.14-1.el7 will be installed
--> Processing Dependency: python-ply for package: python-pycparser-2.14-1.el7.noarch
--> Running transaction check
---> Package python-backports.x86_64 0:1.0-8.el7 will be installed
---> Package python-ply.noarch 0:3.4-11.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==========================================================================================================
 Package                                  Arch        Version                Repository              Size
==========================================================================================================
Installing:
 ansible                                  noarch      2.9.27-1.el7           centos-ansible-29       17 M
Installing for dependencies:
 PyYAML                                   x86_64      3.10-11.el7            base                   153 k
 libyaml                                  x86_64      0.1.4-11.el7_0         base                    55 k
 make                                     x86_64      1:3.82-24.el7          base                   421 k
 openssl                                  x86_64      1:1.0.2k-22.el7_9      updates                494 k
 python-babel                             noarch      0.9.6-8.el7            base                   1.4 M
 python-backports                         x86_64      1.0-8.el7              base                   5.8 k
 python-backports-ssl_match_hostname      noarch      3.5.0.1-1.el7          base                    13 k
 python-cffi                              x86_64      1.6.0-5.el7            base                   218 k
 python-enum34                            noarch      1.0.4-1.el7            base                    52 k
 python-idna                              noarch      2.4-1.el7              base                    94 k
 python-ipaddress                         noarch      1.0.16-2.el7           base                    34 k
 python-jinja2                            noarch      2.7.2-4.el7            base                   519 k
 python-markupsafe                        x86_64      0.11-10.el7            base                    25 k
 python-paramiko                          noarch      2.1.1-9.el7            base                   269 k
 python-ply                               noarch      3.4-11.el7             base                   123 k
 python-pycparser                         noarch      2.14-1.el7             base                   104 k
 python-setuptools                        noarch      0.9.8-7.el7            base                   397 k
 python-six                               noarch      1.9.0-2.el7            base                    29 k
 python2-cryptography                     x86_64      1.7.2-2.el7            base                   502 k
 python2-pyasn1                           noarch      0.1.9-7.el7            base                   100 k
 sshpass                                  x86_64      1.06-2.el7             extras                  21 k
Updating for dependencies:
 openssl-libs                             x86_64      1:1.0.2k-22.el7_9      updates                1.2 M
Transaction Summary
==========================================================================================================
Install  1 Package  (+21 Dependent packages)
Upgrade             (  1 Dependent package)
Total download size: 23 M
Is this ok [y/d/N]: y
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/23): libyaml-0.1.4-11.el7_0.x86_64.rpm                                          |  55 kB  00:00:02     
(2/23): PyYAML-3.10-11.el7.x86_64.rpm                                              | 153 kB  00:00:02     
(3/23): make-3.82-24.el7.x86_64.rpm                                                | 421 kB  00:00:00     
(4/23): python-backports-1.0-8.el7.x86_64.rpm                                      | 5.8 kB  00:00:00     
(5/23): python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch.rpm               |  13 kB  00:00:00     
(6/23): python-cffi-1.6.0-5.el7.x86_64.rpm                                         | 218 kB  00:00:00     
(7/23): python-enum34-1.0.4-1.el7.noarch.rpm                                       |  52 kB  00:00:00     
(8/23): python-idna-2.4-1.el7.noarch.rpm                                           |  94 kB  00:00:00     
(9/23): python-babel-0.9.6-8.el7.noarch.rpm                                        | 1.4 MB  00:00:00     
(10/23): python-ipaddress-1.0.16-2.el7.noarch.rpm                                  |  34 kB  00:00:00     
(11/23): python-markupsafe-0.11-10.el7.x86_64.rpm                                  |  25 kB  00:00:00     
(12/23): python-jinja2-2.7.2-4.el7.noarch.rpm                                      | 519 kB  00:00:00     
(13/23): python-paramiko-2.1.1-9.el7.noarch.rpm                                    | 269 kB  00:00:00     
(14/23): python-ply-3.4-11.el7.noarch.rpm                                          | 123 kB  00:00:00     
(15/23): python-pycparser-2.14-1.el7.noarch.rpm                                    | 104 kB  00:00:00     
(16/23): python-six-1.9.0-2.el7.noarch.rpm                                         |  29 kB  00:00:00     
(17/23): python-setuptools-0.9.8-7.el7.noarch.rpm                                  | 397 kB  00:00:00     
(18/23): python2-cryptography-1.7.2-2.el7.x86_64.rpm                               | 502 kB  00:00:00     
(19/23): python2-pyasn1-0.1.9-7.el7.noarch.rpm                                     | 100 kB  00:00:00     
(20/23): sshpass-1.06-2.el7.x86_64.rpm                                             |  21 kB  00:00:01     
(21/23): openssl-1.0.2k-22.el7_9.x86_64.rpm                                        | 494 kB  00:00:02     
warning: /var/cache/yum/x86_64/7/centos-ansible-29/packages/ansible-2.9.27-1.el7.noarch.rpm: Header V4 RSA/SHA256 Signature, key ID 6e8b7e8a: NOKEY
Public key for ansible-2.9.27-1.el7.noarch.rpm is not installed
(22/23): ansible-2.9.27-1.el7.noarch.rpm                                           |  17 MB  00:00:06     
(23/23): openssl-libs-1.0.2k-22.el7_9.x86_64.rpm                                   | 1.2 MB  00:00:03     
----------------------------------------------------------------------------------------------------------
Total                                                                     3.7 MB/s |  23 MB  00:00:06     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-ConfigManagement
Importing GPG key 0x6E8B7E8A:
 Userid     : "CentOS Config Management SIG (https://wiki.centos.org/SpecialInterestGroup/ConfigManagementSIG) <security@centos.org>"
 Fingerprint: c75a fb57 d5c0 f238 cb15 bec8 1ae1 10fa 6e8b 7e8a
 Package    : centos-release-configmanagement-1-1.el7.centos.noarch (@extras)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-ConfigManagement
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : python2-pyasn1-0.1.9-7.el7.noarch                                                     1/24 
  Installing : python-ipaddress-1.0.16-2.el7.noarch                                                  2/24 
  Updating   : 1:openssl-libs-1.0.2k-22.el7_9.x86_64                                                 3/24 
  Installing : python-six-1.9.0-2.el7.noarch                                                         4/24 
  Installing : sshpass-1.06-2.el7.x86_64                                                             5/24 
  Installing : libyaml-0.1.4-11.el7_0.x86_64                                                         6/24 
  Installing : PyYAML-3.10-11.el7.x86_64                                                             7/24 
  Installing : python-backports-1.0-8.el7.x86_64                                                     8/24 
  Installing : python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch                              9/24 
  Installing : python-setuptools-0.9.8-7.el7.noarch                                                 10/24 
  Installing : python-babel-0.9.6-8.el7.noarch                                                      11/24 
  Installing : python-ply-3.4-11.el7.noarch                                                         12/24 
  Installing : python-pycparser-2.14-1.el7.noarch                                                   13/24 
  Installing : python-cffi-1.6.0-5.el7.x86_64                                                       14/24 
  Installing : python-markupsafe-0.11-10.el7.x86_64                                                 15/24 
  Installing : python-jinja2-2.7.2-4.el7.noarch                                                     16/24 
  Installing : python-idna-2.4-1.el7.noarch                                                         17/24 
  Installing : 1:make-3.82-24.el7.x86_64                                                            18/24 
  Installing : 1:openssl-1.0.2k-22.el7_9.x86_64                                                     19/24 
  Installing : python-enum34-1.0.4-1.el7.noarch                                                     20/24 
  Installing : python2-cryptography-1.7.2-2.el7.x86_64                                              21/24 
  Installing : python-paramiko-2.1.1-9.el7.noarch                                                   22/24 
  Installing : ansible-2.9.27-1.el7.noarch                                                          23/24 
  Cleanup    : 1:openssl-libs-1.0.2k-19.el7.x86_64                                                  24/24 
  Verifying  : python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch                              1/24 
  Verifying  : python-enum34-1.0.4-1.el7.noarch                                                      2/24 
  Verifying  : python-setuptools-0.9.8-7.el7.noarch                                                  3/24 
  Verifying  : python-paramiko-2.1.1-9.el7.noarch                                                    4/24 
  Verifying  : 1:openssl-1.0.2k-22.el7_9.x86_64                                                      5/24 
  Verifying  : python-jinja2-2.7.2-4.el7.noarch                                                      6/24 
  Verifying  : 1:make-3.82-24.el7.x86_64                                                             7/24 
  Verifying  : python-six-1.9.0-2.el7.noarch                                                         8/24 
  Verifying  : python-idna-2.4-1.el7.noarch                                                          9/24 
  Verifying  : python-markupsafe-0.11-10.el7.x86_64                                                 10/24 
  Verifying  : python-ply-3.4-11.el7.noarch                                                         11/24 
  Verifying  : ansible-2.9.27-1.el7.noarch                                                          12/24 
  Verifying  : python-babel-0.9.6-8.el7.noarch                                                      13/24 
  Verifying  : python-backports-1.0-8.el7.x86_64                                                    14/24 
  Verifying  : python-cffi-1.6.0-5.el7.x86_64                                                       15/24 
  Verifying  : 1:openssl-libs-1.0.2k-22.el7_9.x86_64                                                16/24 
  Verifying  : python-pycparser-2.14-1.el7.noarch                                                   17/24 
  Verifying  : libyaml-0.1.4-11.el7_0.x86_64                                                        18/24 
  Verifying  : python-ipaddress-1.0.16-2.el7.noarch                                                 19/24 
  Verifying  : sshpass-1.06-2.el7.x86_64                                                            20/24 
  Verifying  : python2-pyasn1-0.1.9-7.el7.noarch                                                    21/24 
  Verifying  : PyYAML-3.10-11.el7.x86_64                                                            22/24 
  Verifying  : python2-cryptography-1.7.2-2.el7.x86_64                                              23/24 
  Verifying  : 1:openssl-libs-1.0.2k-19.el7.x86_64                                                  24/24 
Installed:
  ansible.noarch 0:2.9.27-1.el7                                                                           
Dependency Installed:
  PyYAML.x86_64 0:3.10-11.el7                                    libyaml.x86_64 0:0.1.4-11.el7_0         
  make.x86_64 1:3.82-24.el7                                      openssl.x86_64 1:1.0.2k-22.el7_9        
  python-babel.noarch 0:0.9.6-8.el7                              python-backports.x86_64 0:1.0-8.el7     
  python-backports-ssl_match_hostname.noarch 0:3.5.0.1-1.el7     python-cffi.x86_64 0:1.6.0-5.el7        
  python-enum34.noarch 0:1.0.4-1.el7                             python-idna.noarch 0:2.4-1.el7          
  python-ipaddress.noarch 0:1.0.16-2.el7                         python-jinja2.noarch 0:2.7.2-4.el7      
  python-markupsafe.x86_64 0:0.11-10.el7                         python-paramiko.noarch 0:2.1.1-9.el7    
  python-ply.noarch 0:3.4-11.el7                                 python-pycparser.noarch 0:2.14-1.el7    
  python-setuptools.noarch 0:0.9.8-7.el7                         python-six.noarch 0:1.9.0-2.el7         
  python2-cryptography.x86_64 0:1.7.2-2.el7                      python2-pyasn1.noarch 0:0.1.9-7.el7     
  sshpass.x86_64 0:1.06-2.el7                                   
Dependency Updated:
  openssl-libs.x86_64 1:1.0.2k-22.el7_9                                                                   
Complete!
[root@ansibleserver /]# 

無事にインストールできた。

[root@ansibleserver /]# ansible --version
ansible 2.9.27
  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, Oct 14 2020, 14:45:30) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
[root@ansibleserver /]# 

Ansible実行失敗時のretryファイル作成の無効化

デフォルト設定では、Ansible実行に失敗すると、失敗した情報が記載されたretryファイルが生成される。
あまり使用しないため、Ansible設定「retry_files_enabled」を変更し、retryファイルが生成されないようにする。

[root@ansibleserver /]# cp -ip /etc/ansible/ansible.cfg /etc/ansible/ansible.cfg.`date +%Y%m%d`
[root@ansibleserver /]# grep retry_files_enabled /etc/ansible/ansible.cfg
# You can enable this feature by setting retry_files_enabled to True
#retry_files_enabled = False
[root@ansibleserver /]# sed -i -e 's/\#retry_files_enabled = False/retry_files_enabled = False/g' /etc/ansible/ansible.cfg
[root@ansibleserver /]# grep retry_files_enabled /etc/ansible/ansible.cfg
# You can enable this feature by setting retry_files_enabled to True
retry_files_enabled = False
[root@ansibleserver /]# diff /etc/ansible/ansible.cfg /etc/ansible/ansible.cfg.`date +%Y%m%d`
258c258
< retry_files_enabled = False
---
> #retry_files_enabled = False
[root@ansibleserver /]# 

Ansible用hostsファイル設定

Ansibleを実行するための準備として、Ansible管理対象サーバを設定ファイル「/etc/ansible/hosts」に登録しておく。

[root@ansibleserver /]# cp -ip /etc/ansible/hosts /etc/ansible/hosts.`date +%Y%m%d`
[root@ansibleserver /]# vi /etc/ansible/hosts
[ansibleclient-all]
ansibleclient1
ansibleclient2
[ansibleclient-grp1]
ansibleclient1
[ansibleclient-grp2]
ansibleclient2
[root@ansibleserver /]#

SSH公開鍵認証設定

AnsibleサーバからAnsible管理対象サーバに対してAnsible実行するためには、SSH接続できるようにする必要がある。
ノンパス(パスワードなし)でSSH接続するの[方法1]の手順で設定し、SSH接続できるようになったことを確認する。

[root@ansibleserver /]# ssh ansibleclient1
[root@ansibleclient1 ~]# exit
logout
Connection to ansibleclient1 closed.
[root@ansibleserver /]# ssh ansibleclient2
[root@ansibleclient2 ~]# exit
logout
Connection to ansibleclient2 closed.
[root@ansibleserver /]# 

Ansible実行テスト

ping疎通確認

以下のコマンドで、ping疎通確認を行う。
[root]# ansible <ホスト名 | ホストグループ名 | all> -m ping

[root@ansibleserver /]# ansible ansibleclient-all -m ping
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
ansibleclient2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
ansibleclient1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
[root@ansibleserver /]# 

サーバ情報取得

Ansible setupモジュールを実行すると、Ansible管理対象サーバの基本情報が取得できる。
[root]# ansible <ホスト名 | ホストグループ名 | all> -m setup

[root@ansibleserver /]# ansible ansibleclient-all -m setup
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
ansibleclient1 | SUCCESS => {
    "ansible_facts": {
        "ansible_apparmor": {
            "status": "disabled"
        }, 
        "ansible_architecture": "x86_64", 
        "ansible_bios_date": "12/03/2020", 
〜〜〜省略〜〜〜
}
ansibleclient2 | SUCCESS => {
    "ansible_facts": {
        "ansible_apparmor": {
            "status": "disabled"
        }, 
        "ansible_architecture": "x86_64", 
        "ansible_bios_date": "12/03/2020", 
〜〜〜省略〜〜〜
}
[root@ansibleserver /]# 

任意コマンド実行

Ansible shellモジュールを実行すると、Ansible管理対象サーバで任意のコマンドを実行できる。
[root]# ansible <ホスト名 | ホストグループ名 | all> -m shell -a ‘df -h’
[root]# ansible <ホスト名 | ホストグループ名 | all> -m shell -a ‘ps -ef’

[root@ansibleserver /]# ansible ansibleclient-grp1 -m shell -a 'df -h'
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
ansibleclient1 | CHANGED | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
overlay          39G   11G   28G  28% /
tmpfs            64M     0   64M   0% /dev
tmpfs           344M     0  344M   0% /sys/fs/cgroup
shm              64M     0   64M   0% /dev/shm
/dev/sda3        39G   11G   28G  28% /etc/hosts
tmpfs           344M   14M  331M   4% /run
tmpfs            69M     0   69M   0% /run/user/0
[root@ansibleserver /]# 
[root@ansibleserver /]# ansible ansibleclient-grp2 -m shell -a 'ps -ef'
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
ansibleclient2 | CHANGED | rc=0 >>
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 00:40 ?        00:00:00 /sbin/init
root        21     1  0 00:40 ?        00:00:00 /usr/lib/systemd/systemd-journald
root        29     1  0 00:40 ?        00:00:00 /usr/lib/systemd/systemd-udevd
root        52     1  0 00:40 ?        00:00:00 /usr/lib/systemd/systemd-logind
dbus        56     1  0 00:40 ?        00:00:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
root        98     1  0 00:40 ?        00:00:00 /usr/sbin/sshd -D
root       267    98  0 01:15 ?        00:00:00 sshd: root@pts/0
root       397   267  0 01:17 pts/0    00:00:00 /bin/sh -c /usr/bin/python /root/.ansible/tmp/ansible-tmp-1635038236.17-484-83308131212905/AnsiballZ_command.py && sleep 0
root       406   397  3 01:17 pts/0    00:00:00 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1635038236.17-484-83308131212905/AnsiballZ_command.py
root       407   406  8 01:17 pts/0    00:00:00 ps -ef
[root@ansibleserver /]# 

まとめ

本記事ではAnsibleインストール〜簡単なテスト実行まで行った。
次回はansible-playbookの記事を作成したい。

タイトルとURLをコピーしました