Ansible 官方介绍
操作系统 : CentOS Linux release 7.6.1810 (Core)
操作系统安装包:CentOS-7-x86_64-Minimal-1708.iso
python : 2.7.5
ansible : 2.9.10
至少准备两台可以联网的主机(或虚拟机)。其中一台作为 安装并使用 ansible ,另外一台做为受控主机。
官方文档
官方安装文档
检测是否安装
ansible --version
示例系统没有安装,先安装 EPEL 源
yum -y install epel-release
安装 ansible (推荐 yum 安装, pip 安装 缺少 /etc/ansible/ansible.cfg 文件)
yum -y install ansible
检测是否安装成功&查看版本信息
ansible --version
结果
ansible 2.9.18
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 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
官方用户手册
ansible -h
说明 : ansible 命令如何使用。
ansible-doc module
说明 : 详细的帮助文档。
ansible-doc -s module
说明 : 精简的帮助文档。
参数说明 : -l, --list 所有模块列表;-s, --snippet 显示指定模块的 playbook 片段。
ping 如何使用
ansible-doc -s ping
结果
- name: Try to connect to host, verify a usable python and return `pong' on success
ping:
data: # Data to return for the `ping' return value. If this parameter is set to `crash', the module will cause an exception.
使用之前,首先要修改配置文件(修改配置文件后,不需要重启服务。因为不是以服务方式运行)
vi /etc/ansible/ansible.cfg
需要修改的内容(不更改,则链接主机的时候,需要手动连接一次)
host_key_checking = False
修改配置文件
vi /etc/ansible/hosts
文件最后增加
192.168.1.2
运行命令(需要输入密码)
ansible 192.168.1.2 -u root -m ping -k
参数说明
-u 用户名
-m , --module-name 运行的模块名称
-k, --ask-pass 需要输入密码
运行结果
[root@test ~]# ansible 192.168.1.2 -u root -m ping -k
SSH password:
192.168.1.2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
PS : 如果不修改配置文件 /etc/ansible/hosts , 则会报错
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
[WARNING]: Could not match supplied host pattern, ignoring: 192.168.1.2
修改配置文件
vi /etc/ansible/hosts
文件最后增加
192.168.1.2 ansible_ssh_user=root ansible_ssh_pass="123456"
192.168.1.3 ansible_ssh_user=root ansible_ssh_pass="123456"
192.168.1.4 ansible_ssh_user=root ansible_ssh_pass="123456"
运行命令
ansible all -m ping
结果
192.168.1.2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.1.3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.1.4 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
ansible 主机生成密钥
[root@test ~]# ssh-keygen
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:TONcecQlKKx3uEqGaIZkZGlOE5phe3aIwXGUXJTiFYg root@test
The key's randomart image is:
+---[RSA 2048]----+
|o=Bo=+o. oo.. |
|oE==.o o .o.. |
|Ooo+o. .ooo . |
| +o.. .=oo.. |
|o. . . .So |
|. + . o . |
| o o . |
| . |
| |
+----[SHA256]-----+
查看结果
[root@test ~]# cd ~/.ssh
[root@test .ssh]# ll
total 12
-rw-------. 1 root root 1675 Apr 24 19:53 id_rsa
-rw-r--r--. 1 root root 403 Apr 24 19:53 id_rsa.pub
-rw-r--r--. 1 root root 519 Apr 24 14:03 known_hosts
将公钥 copy 到被控主机
[root@test .ssh]# scp ~/.ssh/id_rsa.pub 192.168.1.2:~/.ssh/
root@192.168.1.2's password:
id_rsa.pub 100% 403 276.3KB/s 00:00
[root@test .ssh]# scp ~/.ssh/id_rsa.pub 192.168.1.3:~/.ssh/
[email protected]'s password:
id_rsa.pub 100% 403 252.7KB/s 00:00
[root@test .ssh]# scp ~/.ssh/id_rsa.pub 192.168.1.4:~/.ssh/
root@192.168.1.4's password:
id_rsa.pub
主机导入公钥(需要 使用方式 2 : 配置文件保存密码 配置了密码,才可以使用 ansible 远程。否则需要去每台机器配置)
ansible all -m shell -a 'cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys'
结果
192.168.1.3 | CHANGED | rc=0 >>
192.168.1.4 | CHANGED | rc=0 >>
192.168.1.2 | CHANGED | rc=0 >>
修改配置文件
vi /etc/ansible/hosts
文件最后修改为(删除 用户名、密码,通过密钥进行验证)
192.168.1.2
192.168.1.3
192.168.1.4
测试
ansible all -m ping
结果(成功)
192.168.1.3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.1.2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.1.4 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
提示信息 :
[root@test .ssh]# scp ~/.ssh/id_rsa.pub 192.168.1.2:~/.ssh/
root@192.168.1.2's password:
scp: /root/.ssh/: Is a directory
分析问题 :
192.168.1.2 没有运行过 ssh 命令
解决方法 :(192.168.1.2 运行以下命令,则自动生成相对应的目录)
ssh localhost
运行以上命令后 Ctrl + C 退出即可。
ansible 有很多模块。使用方法主要可以通过帮助文档查看
ansible-doc -s module