Ansible是一种运维自动化工具软件,用来批量配置服务器或网络设备(目标主机)。
一、概念理解
通过SSH协议进行连接,详细参考:SSH协议
(1)Ansible知道目标主机密码,并通过密码访问。
Ansible将密码配置在以明文的形式配置在文件(也就是hosts文件),存在安全性问题。
(2)Ansible主机生成密钥对,并将公钥拷贝到目标主机。
通过ssh-copy-id命令进行拷贝,并修改~/.ssh的目录权限。如:
ssh-copy-id -i /root/.ssh/id_rsa.pub root@30.0.1.43
当目标主机较多时,这种方法也比较受限。
(3)Ansible自动化安装配置工具
Redhat下,通过Kickstart工具进行,可进行大批量的认证。
目标主机列表定义在/etc/ansible/hosts文件,称为 "inventory" 。定义格式为:
[webservers]
30.0.1.234
30.0.1.154
运维过程中,需要做的事情很多,如:
(1)基础命令,如ls;
(2)定时任务,如crontab
(3)启停服务,如service ngnix restart
(4)包管理,如apt install ansible
......
运维人员通过两种方式来告知Ansible做事:
一种是我们熟悉的命令行方式,类似ansible webserver -m ping,称之**“Ad-Hoc命令”**;
一种是通过YAML语法定义要执行的命令,称为 “Playbook” 方式。
Ansible通过 “Module” 来实现,如command、shell、copy等等。
查找Ansible提供的模块
root@linux:/etc/ansible# ansible-doc -l
a10_server Manage A10 Networks AX/SoftAX/Thunder/vThunder devices' server object.
a10_server_axapi3 Manage A10 Networks AX/SoftAX/Thunder/vThunder devices
......
模块的详细信息
root@linux:/etc/ansible# 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.
(1)了解YAML
PlayBook是通过YAML语法来实现的,详情参考:《Yaml:数据的另一种形态》。
(2)清楚PlayBook有哪些关键字,如何来的?
--- #文件开始
- hosts: 30.0.1.43 #目标主机
remote_user: root #目标主机用户
vars: #变量关键字
http_port: 8088 #定义变量
tasks: #任务关键字
- name: create new file #自定义任务
file: name=/tmp/playtest.txt state=touch #Module file,创建新文件
.......
handlers: #处理关键字
- name: restart apache #自定义处理名称
service: name=httpd state=restarted #重启服务
说明:file、service都是module名字。
二、Ansible小示例
基础环境:Ubuntu18.04 LTS
root@linux:/# apt install ansible
......
root@linux:/# apt install sshpass
查看ansible版本信息:
root@linux:/etc/ansible# ansible --version
ansible 2.5.1
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/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.17 (default, Sep 30 2020, 13:38:04) [GCC 7.5.0]
已生成ansible.cfg、hosts文件
root@linux:/etc/ansible# ls
ansible.cfg hosts
(1)目标主机有两台,IP地址分别为:30.0.1.234、30.0.1.154
(2)配置hosts文件
root@linux:/etc/ansible# vi hosts
[webservers]
30.0.1.234
30.0.1.154
(3)执行ping操作,测试连接
root@linux:~# ansible webservers -m ping
The authenticity of host '30.0.1.154 (30.0.1.154)' can't be established.
ECDSA key fingerprint is SHA256:THHVZ1IfwqJk0YpV7Qk/a+ZvMds4phRQJEbrJIJFagg.
Are you sure you want to continue connecting (yes/no)? The authenticity of host '30.0.1.234 (30.0.1.234)' can't be established.
ECDSA key fingerprint is SHA256:THHVZ1IfwqJk0YpV7Qk/a+ZvMds4phRQJEbrJIJFagg.
Are you sure you want to continue connecting (yes/no)? yes
30.0.1.154 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Warning: Permanently added '30.0.1.154' (ECDSA) to the list of known hosts.\\r\\nroot@30.0.1.154: Permission denied (publickey,password).\\r\\n",
"unreachable": true
}
30.0.1.234 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Host key verification failed.\\r\\n",
"unreachable": true
}
连接失败
(4)修改ansible.cfg配置文件,不进行host_key的校验
root@linux:~# vi /etc/ansible/ansible.cfg
# uncomment this to disable SSH key host checking
host_key_checking = False
root@linux:/etc/ansible# ansible webservers -m ping
30.0.1.154 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: root@30.0.1.154: Permission denied (publickey,password).\\r\\n",
"unreachable": true
}
30.0.1.234 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Warning: Permanently added '30.0.1.234' (ECDSA) to the list of known hosts.\\r\\nroot@30.0.1.234: Permission denied (publickey,password).\\r\\n",
"unreachable": true
}
依然无法连接到目标主机,root用户不允许进行远程登录
(5)修改hosts文件
root@linux:/etc/ansible# vi hosts
[webservers]
30.0.1.234
30.0.1.154
[webservers:vars]
ansible_ssh_user=linux
ansible_ssh_pass=user@linux
ansible_become=true
ansible_become_method=su
ansible_become_user=root
ansible_become_pass=root@linux
上面配置语句的含义为:以linux用户登录,然后再提高权限,切换到root用户。
(6)再次测试,连接成功
root@linux:/etc/ansible# ansible webservers -m ping
30.0.1.234 | SUCCESS => {
"changed": false,
"ping": "pong"
}
30.0.1.154 | SUCCESS => {
"changed": false,
"ping": "pong"
}
"pong" 表示ping成功,连接成功
全部0条评论
快来发表一下你的评论吧 !