ansible是什么? 由python2.7开发的一个基于模块在远程主机上批量执行命令或者脚本的一个工具 安装 下载wget yum install -y wget 将epel源下载到本地 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo 安装ansible yum install -y ansible ansible命令格式 shell Usage: ansible <host-pattern> [options] ansible 192.xxx.xxx.xx -m ping,第一次使用报错主机名为空,需要进行配置 ping走什么协议 ICMP rpm -ql ansible|more # 查看ansible生成的文件 进入etc下找到cfg文件进行配置 rmp -ql ansible | more,查看生成的文件并一屏显示,user/bin中的文件为2进制
vi /etc/ansible/hosts,此时执行报错,因为ansible底层走的事ssh协议,所以需要执行密码 当输入密码后,就可以连接成功 ssh - keygen,执行后一直回车 出现这样的界面后就可以进行拷贝连接,加上你的用户名跟端口ip拷贝成功后,就可以使用密钥进行连接了 ![]()
host-pattern的格式 - 单个的ip地址 此时再使用ansible进行ping就不要使用密码,可以一台一台的机器是否在线,ansible 192.168.226.101 -m ping #单独机器的ping 此方法还可多个ip同时使用,ansible 192.168.226.101,192.168.226.102 -m ping #多个机器的ping ansible all -m ping,all将全部的机器都探测是否在线,ansible all -m ping #全部机器 按功能进行分组 www[001:006].example.com 代表 www001-www006(包含),测试ansible是否包含结尾 ansible web -m ping #单个的组 ansible 'web:&db' -m ping #多个组的交集 ansible 'web:!db' -m ping #多个组的差集,在前面但是不在后面
ansible-doc 命令格式 ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin] ansible -doc -l|wc -l ,查看所有的模块 command ansible web -m command -a "pwd" ansible web -m command -a "ls" ansible web -m command -a "chdir=/tmp pwd" #切换目录并执行命令 ansible web -m command -a "creates=/tmp pwd" #因为tmp目录存在,pwd不会执行 ansible web -m command -a "creates=/tmp2 pwd" #因为tmp2不存在,pwd执行,用来判断tmp是否 存在,并没有创建 ansible web -m command -a "removes=/tmp2 pwd" #因为tmp2不存在pwd不执行 echo "1234" |passwd --stdin alex #设置用户的密码,无需二次确认密码直接设置密码的方式 使用ansible命令设置账户密码 ansible web -m shell -a "echo '1234' |passwd --stdin alex",密码设置成功 ansible web -m shell -a "chdir=/tmp pwd" shell脚本文件,文件结尾名为sh,/bin/bash代表解释器 执行会出现权限不足的情况,此时要增加一个执行的权限 增加执行的权限后再执行就没有问题了 指定解释器的方式执行脚本文件 ![]() ![]() ![]() ansible 192.168.226.101 -m shell -a "/root/a.sh" # 执行shell脚本,文件要有执行的权限,执行shell脚本的第二种方法,第二种的路径方法,要提前给文件加上执行的权限
![]() ![]() ![]() ![]() ansible 192.168.226.101 -m shell -a "/root/a.py" #执行Python文件 script
查看帮助信息 ansible db -m script -a "/root/a.sh" #执行本地的文件,管控机的文件 ansible db -m script -a "creates=/root/a.sh /root/a.sh" # 判断被控机上的文件是否存在,如果不存在,就执行,如果存在,就跳过 ansible db -m script -a "creates=/tmp /root/a.sh" #判断被控机上的文件 copy backup #创建一个备份文件,以时间戳结尾 ansible web -m copy -a "src=/etc/fstab dest=/tmp/f" #复制本地文件到远程主机,并修改文件名,多次执行不会改变,因为checksum值是一样的 ![]() ![]() ansible web -m copy -a "src=/etc/init.d dest=/tmp backup=yes group=alex mode=755" #复制本地的目录到远程主机,修改目录权限,则目录里面的文件也会跟着变更
ansible web -m copy -a "src=/etc/init.d/ dest=/tmp backup=yes group=alex mode=755" #复制本地目录下的所有文件,
ansible web -m copy -a "content='大弦嘈嘈如急雨,小弦切切如私语,嘈嘈切切错 杂弹,大珠小珠落玉盘' dest=/tmp/b" #直接往文件里面写内容,覆盖写,慎用
来源:http://www./content-4-181751.html |
|