在CentOS7
下,采用yum
形式安装Anisle
并进行验证。
安装Ansible
直接采用yum
安装,默认安装的是环境为Python2
。
安装Ansible
需要epel
源。
[root@root ~]# yum install ansible -y
# 仅显示依赖部分
Dependencies Resolved
============================================================================================================================================================================================================================================
Package Arch Version Repository Size
============================================================================================================================================================================================================================================
Installing:
ansible noarch 2.9.25-1.el7 epel 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
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
python2-cryptography x86_64 1.7.2-2.el7 base 502 k
python2-httplib2 noarch 0.18.1-3.el7 epel 125 k
python2-jmespath noarch 0.9.4-2.el7 epel 41 k
python2-pyasn1 noarch 0.1.9-7.el7 base 100 k
sshpass x86_64 1.06-2.el7 extras 21 k
Transaction Summary
============================================================================================================================================================================================================================================
Install 1 Package (+20 Dependent packages)
Total download size: 21 M
Installed size: 122 M
查看已安装Ansible
版本
[root@root ~]# ansible --version
ansible 2.9.25
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, Nov 16 2020, 22:23:17) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
查看默认配置文件列表
[root@root ~]# ll /etc/ansible
total 28
-rw-r--r-- 1 root root 19985 Aug 22 04:07 ansible.cfg
-rw-r--r-- 1 root root 1016 Aug 22 04:07 hosts
drwxr-xr-x 2 root root 4096 Aug 22 04:07 roles
设置免密登陆
[root@root ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:cJ/tuzJTkYguW8mluVn8x+6CZgZPbEKTZEqGuF60Wiw root@webserver
The key's randomart image is:
+---[RSA 2048]----+
| . . |
| . o o o |
| + +.+.o . . |
| E = .o=.ooo |
| . = +SOo .. |
| o . X *.. |
| + X +.. |
| . o X o.o |
| + +o*o |
+----[SHA256]-----+
[root@root-webserver ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.16.1.40
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '172.16.1.40 (172.16.1.40)' can't be established.
ECDSA key fingerprint is SHA256:Te3efu3dJGjY/KkzMxaQ/dBIo17o7b2hFvrllYKttn0.
ECDSA key fingerprint is MD5:6f:e9:75:78:6e:a9:da:95:09:c4:e7:23:59:10:97:39.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.16.1.40's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@172.16.1.40'"
and check to make sure that only the key(s) you wanted were added.
验证免密登陆
[root@root ~]# ssh 'root@172.16.1.40'
Last login: Sun Jan 9 18:04:23 2022 from 172.16.1.40
[root@root ~]# logout
添加主机
[root@root ~]# vi /etc/ansible/hosts
[cloud]
172.16.1.40
使用ping模块测试主机是否配置成功
[root@root ~]# ansible cloud -m ping
172.16.1.40 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
查看cloud组主机
[root@root ~]# ansible cloud --list-hosts
hosts (1):
172.16.1.40