ansible

一 . ansible的安装

#将epel源下载到本地
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
#安装ansible
yum install -y ansible

二 . ansible的使用

  命令格式: ansible <host-pattern> [options]

ansible -h  # 查看<host-pattern>帮助信息
-a MODULE_ARGS, #模块参数
-C, --check # 干跑,白跑
-f FORKS, --forks=FORKS #指定并发,默认5个
--list-hosts #列出主机
-m MODULE_NAME# 模块名称
--syntax-check #检查语法
-k  #密码

rpm -ql ansible|more # 查看ansible生成的文件
/etc/ansible
/etc/ansible/ansible.cfg #配置文件
/etc/ansible/hosts  #  vim这个文件夹,然后写上ip地址 然后就能ping通了
/etc/ansible/roles #空文件夹

  在管控机上面管理被管控机的操作

ssh-keygen  生成公钥和私钥,无限回车就好,然后生成一个秘钥
ssh-copy-id root@192.168.111.129 发送公钥  然后就直接能在128上连上129的服务器
然后128就是管控机,可以通过128这台机器探测其他被管控机是否能ping通

# 这样就不用每次ping都输如密码了, ping走的是ICMP协议

  将ip进行分组(以后可以按照功能分组,比如所有数据库是一组)

# 将ip分组用中括号[]
vim /etc/ansible/hosts
[组名1]
192.168.111.[129:130]  # 就是这两个ip都是一组的
[组名2]
192.168.111.[130:131]

ansible 组名1 -m ping  # 129和130都可以ping通
ansible  组名1,组名2 -m ping  # 并集
ansible '组名1:组名2' -m ping # 也是并集
ansible '组名1:&组名2' -m ping #多个组的交集
ansible '组名1:!组名2' -m ping #多个组的差集,组名1有组名2没有  # 千万不能用双引号

  <host-pattern>和 [options] 的用法

<host-pattern>
 组名1,组名2    # 并集
'组名1:组名2'   # 并集
'组名1:&组名2'  # 交集
'组名1:!组名2'   # 差集,组名1有而组名2没有
多个ip地址, 用逗号分隔
all

[options]
查看参数[options]的帮助信息  ansible-doc
使用方法: Usage: ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin]
ansible-doc -j  以json数据返回
ansible-doc -l  列出所有模块
ansible-doc -s  以片段格式展示模块信息  ansible-doc -s ping 看ping模块的片段信息,ansible-doc ping 全部信息

  command

ansible-doc -s command   看看command有哪些参数
ansible web -m command -a "ls"  看web组里面有那几个id地址
ansible web -m command -a "chdir=/tmp pwd" #切换到/tmp下并执行命令
ansible web -m command -a "creates=/tmp pwd" #因为tmp目录存在,pwd不会执行 creates只判断是否存在,不创建
ansible web -m command -a "creates=/tmp2 pwd" #因为tmp2不存在,pwd执行
ansible web -m command -a "removes=/tmp2 pwd" #因为tmp2不存在, pwd不执行
ansible web -m command -a "removes=/tmp pwd" #因为tmp目录存在,pwd会执行
ansible web -m command -a "useradd attila"  创建用户
echo "666" |passwd --stdin attila  #设置密码,不用输入第二次(确认密码) 要在web组里面的ip服务器上输入指令
command命令里面不能有特殊字符,如<>&|等

  shell

ansible web -m shell -a "echo '666' |passwd --stdin attila"   # 可以在管控机上直接设置密码
ansible web -m shell -a "chdir=/tmp pwd"  # 基本指令和command一样
ansible 192.168.226.101 -m shell -a "bash test.sh" # .sh后缀名是shell脚本,bash是执行shell脚本
ansible 192.168.226.101 -m shell -a "python test.py" #执行Python文件默认的是2.X版本 需要在顶行写 #coding:utf-8
ansible 192.168.226.101 -m shell -a "/root/test.py"  #需要在test.py文件中顶行指定python解释器 #!/bin/env python 

  script

ansible db -m script -a '/root/test.sh'   # 在128的机器上创建脚本,里面写的是创建wonderful文件夹 \
    # b组内的ip都执行这个脚本,分别在自己的服务器上创建了wonderful文件夹
ansible db -m script -a 'bash test.sh'
ansible db -m script -a "creates=/root/test.sh  pwd"   # 如果被控机上(db组)没有就执行pwd

  copy

backup #创建一个备份文件,以时间结尾
content #直接往文件里面写内容
dest #目标地址,就是ansible后面的地址
group #属组
mode# 文件的权限 W 2 R 4 X 1
owner #属主
src #源地址,所在服务器

ansible web -m copy -a "src=/opt/tianlong.txt dest=/opt/xiaofeng"  
    # 从本机往web组里面copy,多次执行不会改变,因为checksum值是一样的
ansible web -m copy -a "src=/opt/tianlong.txt dest=/opt/xiaofeng.txt backup=yes"
    # 如果web组里的服务器上有/opt/xiaofeng.txt,然后当你把本机的/opt/tianlong.txt内容改了之后,然后执行上个命令,\
      /opt/xiaofeng.txt跟最新的一样,/opt/xiaofeng.时间 是备份没有修改前的
ansible web -m copy -a "src=a.sh dest=/tmp/a.sh backup=yes group=attila mode=755"
    # 复制本地文件到远程主机,并指定属组和权限
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=/opt/xiakexing.txt"  # 直接往文件里面写内容,覆盖写,慎用

 三 . 查看linux版本号

uname -r  #查看内核版本号

 

posted @ 2019-05-05 19:52  一个很善良的抱爱  阅读(399)  评论(0编辑  收藏  举报