ansible 和 saltstack
都是为了同时在多台主机上执行相同的命令, 但是 salt 配置麻烦, ansible 基本不用配置, ansible 通过 SSH 来连接并控制被控节点
1. 安装
第一步: 下载 epel 源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
第二步: 安装 ansible
yum install -y ansible
2. 管控主机秘钥登录被控主机
SSH 秘钥登录
- SSH-keygen # 用来生成 SSH 的密钥对
- SSH-copy-id 192.168.107.131 # 复制秘钥到远程主机
3. ansible 命令格式
- -a MODULE_ARGS, --args=MODULE_ARGS # 模块的参数
- -C, --check # 检查
- -f FORKS, --forks=FORKS #用来做高并发的
- --list-hosts #列出主机列表
- -m MODULE_NAME #模块名称
- --syntax-check # 语法检查
- 4. ansible hosts
查看 ansible 生成的文件
ansible/hosts 文件
- # This is the default ansible 'hosts' file.
- #
- # It should live in /etc/ansible/hosts
- #
- # - Comments begin with the '#' character # 用 #来表示注释
- # - Blank lines are ignored # 空白行被忽略
- # - Groups of hosts are delimited by [header] elements # 主机组 需要在 [] 下面
- # - You can enter hostnames or ip addresses #可以写主机名或者 ip 地址
- # - A hostname/ip can be a member of multiple groups # 一台主机可以在多个组里面
- [web]
- 192.168.181.133
- 192.168.181.134
- [db]
- 192.168.181.134
- 192.168.181.135
5. 模块
ansible-doc 查看文档
- ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin]
- -j #以 JSON 的方式返回 ansible 的所有模块
- -l, --list #列出所有的 ansible 的模块
- -s #以片段式显示 ansible 的帮助信息
1. ping 模块
host-pattern 格式
2. 系统默认模块 command, 可以不指定 - m
第一个 command 命令
- ansible Web -a 'chdir=/tmp pwd' # 切换目录执行命令, 使用场景是编译安装时使用
- ansible Web -a 'creates=/tmp pwd' # 用来判断 / tmp 目录是否存在, 存在就不执行操作
- ansible Web -a 'creates=/data pwd' # 因为 data 不存在, 所有才会执行 pwd 命令
- ansible Web -a 'removes=/tmp pwd' # 用来判断 tmp 目录是否存在, 存在就执行操作
- ansible Web -a 'removes=/data pwd' # 因为 data 不存在, 所有才不会执行
如果命令里包含特殊符号, 需要同 shell 模块 : $ < > | will not work use shell module
3. shell 执行远程文件
- ansible Web -m shell -a 'echo"123"| passwd --stdin alex' # 批量创建密码
- ansible 192.168.107.131 -m shell -a 'bash a.sh' # 执行远程文件方式一
- ansible 192.168.107.131 -m shell -a '/root/a.sh' # 执行远程文件方式二, 文件必须有执行权限, 需要 chmod +x
- ansible 192.168.107.131 -m shell -a '/root/a.py' # 执行远端的 Python 脚本
被管控机 192.168.107.131
管控机就会创建 bulijngbuling2 文件夹
4.script 执行本地文件
- ansible Web -m script -a '/root/m.sh' # 执行本地的文件, 即管控机上的脚本
- ansible Web -m script -a 'removes=/root/m.sh /root/m.sh' # 用来判断被管控机上是不是存在文件, 如果存在, 存在就执行, 不存在就不执行
- ansible Web -m script -a 'creates=/root/a.sh /root/m.sh' #用来判断被管控机上是不是存在文件, 如果存在, 就不执行
管控机
db 组群 就会创建 hhhhhh 文件夹
5. copy : Copies files to remote locations 把文件批量拷到被管控机上
- backup # 备份, 以时间戳结尾
- src # 源文件
- dest # 目的地址
- group # 文件的属组
- mode # 文件的权限 r 4 w 2 x 1
- owner #文件的属主
- # 通过 md5 码来判断是否需要复制
- ansible Web -m copy -a 'src=/root/m.sh dest=/tmp/a.sh' # 复制本地文件的到远程主机
- ansible Web -m copy -a 'src=/root/m.sh dest=/tmp/a.sh mode=755' # 修改文件的权限
- ansible Web -m copy -a 'src=/root/m.sh dest=/tmp/a.sh mode=755 owner=ryan' # 修改文件的属主
- ansible Web -m copy -a 'src=/etc/init.d dest=/tmp/ mode=755 owner=ryan' # 复制本地目录到远程主机, 如果改变文件的属性, 则文件夹内的文件也会被改变
- ansible Web -m copy -a 'src=/etc/init.d/ dest=/tmp/ mode=755 owner=ryan' # 复制本地目录内的所有文件到远程主机
- ansible Web -m copy -a "content='白云深处有人家 \ n'dest=/tmp/b.txt" # 直接将文本内容注入到远程主机的文件中
6. fetch : Fetches a file from remote nodes 把远程文件传到管控机, 如各机的 log 日志等, 与 copy 相反
- dest # 目的地址 (required) A directory to save the file into
- src # 源地址 (required) The file on the remote system to fetch,This `must' be a file, not a directory
- ansible Web -m fetch -a 'src=/var/log/cron dest=/tmp' # 下载被控节点的文件, 在管控机 / tmp 目录下以每台机器 ip 为名创建一个文件夹, 并保留原来的目录结构
- 7. file
- path: # (required) Path to the file being managed.
- src: # path of the file to link to (applies only to `state=link'and `state=hard')
- state: # directory touch link absent
- owner: # chown'ansible db -m file -a'path=/lzmly2 state=directory' #在远程机器上创建文件夹
- ansible db -m file -a 'path=/root/q.txt state=touch' #用来在远程机器上创建文件
- ansible db -m file -a 'path=/tmp/f src=/etc/fstab state=link' #创建软连接 src 是源地址, path 是目标地址
- ansible db -m file -a 'path=/tmp/f state=absent' #用来删除文件或者文件夹
- gruop /owner /mode
明天继续补充
来源: http://www.bubuko.com/infodetail-2960895.html