什么是Ansible

ansible是一种运维自动化工具软件,用来批量配置服务器或网络设备(目标主机)。
一、概念理解
ansible如何来连接目标主机?通过ssh协议进行连接,详细参考:ssh协议
目标主机为何能相信ansible,并接受其指令?(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工具进行,可进行大批量的认证。
ansible如何知道需要连接哪些目标主机?目标主机列表定义在/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做事:
一种是我们熟悉的命令行方式,类似ansible webserver -m ping,称之**“ad-hoc命令”**;
一种是通过yaml语法定义要执行的命令,称为 “playbook” 方式。
ansible又是如何具备做这些事的能力的?ansible通过 “module” 来实现,如command、shell、copy等等。
运维人员如何知道ansible提供了哪些module?查找ansible提供的模块
root@linux:/etc/ansible# ansible-doc -la10_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.如何读懂playbook?(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
安装ansible软件root@linux:/# apt install ansible......root@linux:/# apt install sshpass查看ansible版本信息:
root@linux:/etc/ansible# ansible --versionansible 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# lsansible.cfg hostsansible连通目标主机(1)目标主机有两台,ip地址分别为:30.0.1.234、30.0.1.154
(2)配置hosts文件
root@linux:/etc/ansible# vi hosts [webservers]30.0.1.23430.0.1.154(3)执行ping操作,测试连接
root@linux:~# ansible webservers -m pingthe 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)? yes30.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 checkinghost_key_checking = falseroot@linux:/etc/ansible# ansible webservers -m ping30.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.23430.0.1.154[webservers:vars]ansible_ssh_user=linuxansible_ssh_pass=user@linuxansible_become=trueansible_become_method=suansible_become_user=rootansible_become_pass=root@linux上面配置语句的含义为:以linux用户登录,然后再提高权限,切换到root用户。
(6)再次测试,连接成功
root@linux:/etc/ansible# ansible webservers -m ping30.0.1.234 | success => {changed: false, ping: pong}30.0.1.154 | success => {changed: false, ping: pong}pong 表示ping成功,连接成功

新思考:以“新物联”提速新基建
文心一言APP与澳门签署战略合作!打造“AI+旅游”澳门样本
电子芯闻早报:中国第一芯将浮出水面 小米VR今日发布
智慧城市出现比以往更智能的购物方式?
沃尔沃计划在瑞典舍夫德工厂开始生产电动机
什么是Ansible
数据存储能为企业带来什么
赛灵思带来280MHz全频段C-band解决方案
自制AVR单片机解锁器
dsp有没有必要装_dsp有电流声怎么处理
20万吨磷酸铁锂正极材料项目投产!
镭豆2+智能空气质量检测仪评测 第一款入驻苹果店的PM2.5检测仪
米家智能除菌加湿器发布 售价249元
基于FPGA技术实现DMUX专用集成器件功能系统的设计
人工智能军事变革尚未到来
Melexis推出针对经济适用型电动汽车进行优化的高精度电流传感器IC
Redmi Note/Redmi 7获得MIUI 11稳定版更新,提供了全新的视觉效果
华为p50发布会直播回放
一种基于紫金桥软件中报警的特殊使用方法—对报警进行持续提示
任正非在“蓝血十杰”表彰会上的讲演稿