一、Ansible介绍Ansible是2013年推出的一款IT自动化和DevOps软件,2015年被RedHat收购,基于Python研发,结合继承了很多老运维工具的优点,实现了批量操作系统配置、批量程序部署、批量操作命令等功能。ansible的优点主从工作模式只有SSH和Python才能使用。clientlessansible功能强大,支持丰富的自定义模块。这些模块支持剧本。上手容易,门槛低。基于Python开发,二次开发更容易。有很多公司。社区活跃Ansible特点模块化设计,调用特定模块完成特定任务,基于Python语言、PyYAML(半结构化语言)Jinja2实现paramiko,其模块支持JSON等标准输出格式,可以使用任何编程语言来实现重写管理主机Ansible可以安装在任何装有Python2(2.7版)或Python3(3.5版及更高版本)的计算机上运行。这包括RedHat、Debian、CentOS、macOS、任何BSD等。控制节点不支持Windows。Ansible使用以下模块。需要安装ParamikoPyYAML(半结构化语言)Jinja2httplib2six。对于托管主机,ansible默认通过SSH协议管理机器。被管主机需要开启SSH服务,以允许ansible主机登录被管节点。安装Python2(2.7版本)或Python3(3.5及以上版本)如果宿主节点启用了SElinux,则需要安装libselinux-Python部署证书文件Ansible通过SSH远程执行命令,SSH远程执行命令必须通过authentication,将密码写入配置文件安全性很差。一般采用密钥方式进行认证。如果所有主机的公钥都没有秘钥,则执行该命令会出错。Managementhosts,5台托管主机,为了实现批量部署程序,批量运行命令等功能,具体需求如下:Ansible常用配置参数说明Ansible配置文件搜索顺序首先查看ANSIBLE_CONFIG变量定义的配置文件,然后查看当前目录下的/ansible.cfg文件,再次查看当前主目录下的~/ansible.cfg文件,最后查看/etc/ansible/ansible.cfg文件/etc/ansible/ansible.cfg是默认ansible配置文件路径/etc/ansible/hosts是ansible默认主机文件路径ansible.cfg配置文件inventory定义托管主机地址配置主机文件路径名指定的配置文件,写入远程主机地址host_key_checking=Falsesshhostkeyverification配置参数-如果为False,则无需输入yes-如果为True,则等待输入yesansible_ssh_user通过这个变量ansible_ssh_passsshpassword(这种方式不安全,强烈推荐使用SSHkey)ansible_ssh_private_key_filessh使用的私钥文件,适用于有多个key,又不想使用SSHagentAnsible的情况managedhostaddressconfigurationhostfileformat#meanscomment[groupname]主机名或IP地址,其他参数-vars变量定义,用在组名后,如[all:vars]//指定所有组密钥的存放位置app:children]//这里web和db是不同的组/root/keyfile/id_dsa》ansiblew命令基本列出要执行的hostsansibleall--list-hosts批量检测hostsansibleall-mping-kansiblehostcollection-m模块名-a模块参数,多个用“逗号”分隔-m模块名,默认命令模块-a或--args模块参数其他参数-i库存文件路径,或可执行脚本-k使用交互式登录密码-e定义变量-v显示详细信息1)在管理主机上安装EPEL源。EPEL源码包含上面ansible需要的所有模块[root@ansible~]#wget-O/etc/yum.repos.d/epel.repohttp://mirrors.aliyun.com/repo/epel-7.repo[root@ansible~]#yumcleanall[root@ansible~]#yumrepolist2)Installansible[root@ansible~]#yum-yinstallansible[root@ansible~]#ansible--version//查看ansible版本ansible2.9.133)生成秘钥配置免密登录托管主机[root@ansible~]#vim/etc/ansible/hosts[web]web1web2[db]db[1:2][other]cache[root@ansible~]#ssh-keygen//一路输入生成秘钥[root@ansible.ssh]#ansibleall-mping//测试失败web1|无法到达!=>{"changed":false,"msg":"无法通过ssh连接到主机:权限被拒绝(publickey,gssapi-keyex,gssapi-with-mic,password).","unreachable":true}db1|无法到达!=>{“更改”:false,“msg”:“无法通过ssh连接到主机:权限被拒绝(publickey,gssapi-keyex,gssapi-with-mic,密码)。”,“无法访问”:true}。..[root@ansible.ssh]#foriin{40..45};dossh-copy-idroot@192.168.4.1$i;done//使用for循环将secretkey发送给托管主机实现免密登录[root@ansible.ssh]#ansibleall-mping//成功db2|SUCCESS=>{"ansible_facts":{"discovered_interpreter_python":"/usr/bin/python"},"changed":false,"ping":"pong"}db1|SUCCESS=>{"ansible_facts":{"discovered_interpreter_python":"/usr/bin/python"},"changed":false,"ping":"pong"}...4)指定Key存储位置[root@ansible~]#mkdirkeyfile/[root@ansible~]#mv.ssh/id_dsakeyfile///修改key的存放位置[root@ansible~]#ansibleall-mping//失败web1|无法到达!=>{"changed":false,"msg":"无法通过ssh连接到主机:权限被拒绝(publickey,gssapi-keyex,gssapi-with-mic,password)。","unreachable":true}。.....[root@ansible~]#cat/etc/ansible/hosts[web]web1web2[db]db[1:2][other]cache[all:vars]ansible_ssh_private_key_file="/root/keyfile/id_dsa"//指定key的存储位置[root@ansible~]#ansibleall-mping//成功db2|SUCCESS=>{"ansible_facts":{"discovered_interpreter_python":"/usr/bin/python"},"changed":false,"ping":"pong"}......[root@ansible~]#mkdirmyansible[root@ansible~]#cdmyansible/[root@ansiblemyansible]#vimmyhost[app1]web1db1[app2]web2db2[app:children]app1app2[other]cache[all:vars]ansible_ssh_private_key_file="/root/密钥文件/id_dsa[root@ansiblemyansible]#vimansible.cfg[defaults]inventory=myhosthost_key_checking=False[root@ansiblemyansible]#lsansible.cfgmyhost[root@ansiblemyansible]#ansibleapp1-mpingweb1|SUCCESS=>{“ansible_facts”:{“discovered_interpreter_python”:“/usr/bin/python”},“changed”:false,“ping”:“pong”}db1|SUCCESS=>{"ansible_facts":{"discovered_interpreter_python":"/usr/bin/python"},"changed":false,"ping":"pong"}[root@ansiblemyansible]#ansibleapp-mpingdb1|SUCCESS=>{"ansible_facts":{"discovered_interpreter_python":"/usr/bin/python"},"changed":false,"ping":"pong"}...5)测试ansible.cfg文件[root@ansiblemyansible]#ansibleapp--list-hosts//首先检查是否成功在该目录hosts(4)下的ansible.cfg文件:web1db1web2db2[root@ansiblemyansible]#cd..[root@ansible~]#ansibleapp--list-hosts//该目录下没有ansible.cfg文件。默认的/etc/ansible/hosts没有定义应用程序组错误[警告]:无法匹配提供的主机模式,忽略:app[警告]:没有匹配的主机,与主机无关(0):
