ansible安装:
忽 略
master与client ssh免密:
需要修改ssh的配置文件/etc/ssh/ssh_config
1. #进行ping模块的连接测试
3. [root@ansible python]# ansible nginx -m ping
4. webB | FAILED! => { #我们发现webB还是没链接成功,这是因为本机的known_hosts文件还没有记录对方主机的信息。
5. "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."
6. }
想要解决known_hosts的问题,只需要修改ssh的配置文件/etc/ssh/ssh_config即可
1. #修改ssh配置文件
2. [root@ansible .ssh]# sed -n '35p' /etc/ssh/ssh_config
3. # StrictHostKeyChecking ask
4. [root@ansible .ssh]# vim /etc/ssh/ssh_config
5. [root@ansible .ssh]# sed -n '35p' /etc/ssh/ssh_config
6. StrictHostKeyChecking no #去掉注释,修改成这样
7.
8. #重启ssh服务
9. [root@ansible .ssh]# systemctl reload sshd.service
/etc/ansible/hosts文件配置
设置方法一:
4. [all_remote] #被管理的主机组名称
5. webA ansible_ssh_host=192.168.200.132 ansible_ssh_port=22 ansible_ssh_user=root #第一台主机
6. webB ansible_ssh_host=192.168.200.138 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=666666 #第二台主机
7.
8. 【注意】
9. 如果是免密的那么铭文密码就不需要了
10. ansible_ssh_pass=
执行命令示范:
ansible all_remote -m shell -a "pushd ~/;ls /etc | tail -3;popd"
设置方法二:
[all_remote]
#当前面是ansible_ssh_host=,需要前面加上web01等等
web01 ansible_ssh_host=11.222.76.9 ansible_ssh_port=22
#默认前面直接加ip,后面指定端口可以使用ansible_ssh_port或者ansible_port
11.222.76.9 ansible_ssh_port=22
执行命令示范:
ansible all_remote -m shell -a "pushd ~/;ls /etc | tail -3;popd" -u myuser
【注意】
由于hosts没有指定用户,所有命令行-u来指定
参数说明:
8. 特别提示:
9. WebA ===> 主机名
10. ansible_ssh_host ===>主机IP
11. ansible_ssh_port ===>ssh的默认端口
12. ansible_ssh_user ===>ssh的用户名
13. ansible_ssh_pass ===>ssh的用户的连接密码
ansible常用模块用法:
使用ping模块用来查看服务器是否连接正常:
ansible -i /etc/ansible/hosts web01:web02 -m ping -u user
【参数】:
-i指定hosts的路径
web01:web02是指定hosts里面的web01与web02机器
ansible all_remote:!web01 -m ping -u user
【参数】:
!web01指排查web01这台机器
ansible webA:webB -m ping -u user
ansible模块command(不支持管道,不建议使用):
#正确用法:
ansible all_remote -m command -a “pwd”
#command模块不支持管道符操作:
【错误】 ansible all -m command -a “echo test | grep t”
command模块不支持重定向操作
【错误】 ansible all -m command -a "echo bb >> /tmp/test
ansible模块shell(支持管道,支持重定向):
#shell模块支持管道符
【正确】 ansible all -m shell -a “echo albitest | grep a”
#shell支持重定向
【正确】ansible all -m shell -a "echo bb >> /tmp/testansib
#如果遇到特殊符号需加入\转义,如此ansible才能正常运行
【正确】
ansible all -m shell -a “cat /etc/passwd | awk -F”:" ‘{print $1}’"
ansible模块raw,最原始的方式运行命令(不依赖python,仅通过ssh实现):
#清除yum缓存
ansible all -m raw -a “yum -y clean all”
建立yum缓存
ansible all -m raw -a “yum makecache”
#yum装nmap包
ansible all -m raw -a “yum -y install nmap”
ansible的copy模块批量下发文件或文件夹:
copy模块的参数,ansible 主机组 -m 模块 -a 命令
•
o src:指定源文件或目录
o dest:指定目标服务器的文件或目录
o backup:是否要备份
o owner:拷贝到目标服务器后,文件或目录的所属用户
o group:拷贝到目标服务器后,文件或目录的所属群组
o mode:文件或目录的权限
src===>源文件路径 dest=目标路径位置:
ansible all -m copy -a “src=/root/test.txt dest=/tmp/”
copy模块拷贝文件夹:
特别提示:
如果目标路径里有与我拷贝的文件同名文件的话,会直接覆盖目标路径下的文件
#拷贝/service/scripts/ 目录下所有内容到dest的路径下(注意两条命令的对比)
ansible webA -m copy -a "src=/service/scripts/ dest=/service/scripts/"
#拷贝/service/scripts目录本身及其内部的所有内容到dest的路径下(注意两条命令的对比)
ansible webA -m copy -a "src=/service/scripts dest=/service/scripts/ backup=yes"
使用copy模块,为远程机器文件(不存在就创建)输入数据:
ansible -i /etc/ansible/hosts web01 -m copy -a "content="haha" dest=/root/dockerfile" -u user
copy模块拷贝文件时,指定文件的属主,需要注意,远程主机上必须存在对应的用户:
ansible ansible-demo3 -m copy -a "src=/testdir/copytest dest=/testdir/ owner=jenkins"
copy模块拷贝文件时,指定文件的权限:
ansible ansible-demo3 -m copy -a "src=/testdir/copytest dest=/testdir/ mode=0644
ansible的script模块批量运行脚本:
ansible的script模块能够实现远程服务器批量运行本地的shell脚本
34. ansible $Group -m script -a "/service/scripts/auto_nginx.sh"
ansible报错汇总:
权限问题:(ansible user问题)
[root@VM20201125-0 ~]# ansible -i /etc/ansible/hosts all_remote -m ping
11.269.21.21 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: [email protected]: Permission denied (publickey,password).",
"unreachable": true
}
【注意】
原因可能是hosts中没有设置ansible_host_user或者命令行执行ansible没有执行-u +用户
没有正确找到/etc/ansible/hosts文件
[root@kVM20208925-0 ~]# ansible -i /etc/ansible/hostss all_remote -m ping -u myself
[WARNING]: Unable to parse /etc/ansible/hostss as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[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: all_remote