Add Hosts for Ansible

System Env

[root@ansible ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@ansible ~]# lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.6.1810 (Core)
Release: 7.6.1810
Codename: Core
[root@ansible ~]# ansible –version
ansible 2.7.7
config file = None
configured module search path = [‘/root/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.6.6 (default, Jan 26 2019, 16:53:05) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]

Generate Ansible Server’s public key

[root@ansible ~]# ssh-keygen

Deploy Ansible Server’s public key

  • Deploy public key to s1

    [root@ansible ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@s1

Add Hosts for Ansible

[root@ansible ~]# mkdir /etc/ansible
[root@ansible ~]# vim /etc/ansible/hosts
[root@ansible ~]# cat /etc/ansible/hosts
[servers]
s1

Run some test command

[root@ansible ~]# ansible s1 -m ping
s1 | SUCCESS => {
“changed”: false,
“ping”: “pong”
}