批量管理 linux 服务器指的是: 批量执行命令, 下发文件等等
安装 ansible
1.1: 安装 epel 源
- yum install epel-release -y
- #编辑 / etc/yum.repos.d/epel.repo, 注释 mirrorlist, 打开 baseurl
1.2: 安装 ansible
- yum list ansible #ansible 版本, 如果这个命令运行有问题的话, 多运行几次
- yum install ansible -y
1.3: ansible 查看帮助
- ansible-doc -l #查看总帮助
- ansible-doc -s shell #查看 shell 模块的帮助
- ansible-doc -s raw
配置主机组 (/etc/ansible/hosts)
- [testgroup]
- 192.168.56.40
- 192.168.56.41
- 192.168.56.42
- [test41]
- 192.168.56.41
- [test42]
- 192.168.56.42
配置 ssh 免密码登录
- ssh-keygen -t rsa# 一直按回车即可
- ssh-copy-id -i .ssh/id_rsa.pub ip #上传公钥到服务器
ansible 模块
ansible 语法
ansible 主机组或者主机 -m 模块 -a 命令
2 ansible 语法测试
- ansible testgroup -m ping
- ansible testgroup -m command -a "pwd"
3 : ansible 模块 command(不支持管道, 不建议使用)
- ansible testgroup -m command -a "pwd"
- ansible testgroup -m command -a "echo testa|grep a" #这个不能正常使用
- ansible testgroup -m command -a "echo bb>>/tmp/testansible" #重定向也无法正常使用
6: ansible 模块 shell(支持管道)
- ansible testgroup -m shell -a "echo testa|grep a" #支持管道
- ansible testgroup -m shell -a "echo bb>>/tmp/testansible" #支持重定向
- ansible testgroup -m shell -a "cat /etc/passwd|awk -F':''{print \$1}'" #遇到特殊符号需要加入 \ 转义, 这样子 ansible 才能正常运行
7: ansible 模块 raw(如果运行命令, 一般使用 shell 模块)
说明为什么会有 raw, 由于 ansible 依赖 python 环境, 例如需要有 python-simplejson 之类的包, 如果没安装的话, 就无法使用 shell 等模块, 就需要先用 raw 安装一下.
- ansible testgroup -m raw -a "yum install python-simplejson -y"
- ansible testgroup -m raw -a "yum install libselinux-python -y"
8.1 ansible 模块 copy(下放文件)
- ll -h /usr/local/src/
- cat /tmp/testdir/test.txt
- ansible testgroup -m copy -a "src=/tmp/testdir/test.txt dest=/usr/local/src/"
- #src 指定本地的文件
- #dest 指定远程主机的目录或者文件
8.2 拷贝文件夹
- ll -h /usr/local/src/
- ansible testgroup -m copy -a "src=/tmp/testdir/ dest=/usr/local/src/" #testdir 文件夹没拷贝
- ansible testgroup -m copy -a "src=/tmp/testdir dest=/usr/local/src/" #testdir 文件夹也拷贝了
8.3 备份文件
ansible testgroup -m copy -a "src=/tmp/testdir/test.txt dest=/usr/local/src/ backup=yes"?
8.4 指定用户和权限
ansible testgroup -m copy -a "src=/tmp/testdir/test.txt dest=/usr/local/src/ backup=yes owner=nobody group=nobody mode=0600"
9: ansible 模块 script(远程机器运行 ansible 主控端的脚本)
实现的方案: 把脚本下发到所有的服务器, 然后再运行脚本. copy+shell+delete
- cat /usr/local/src/script
- chmod a+x /usr/local/src/script
- ansible testgroup -m script -a "/usr/local/src/script"
- 10: ansible-playbook(把 ansible 执行命令写入文件中)
- #cat /usr/local/src/test.yaml
- - hosts: testgroup
- tasks:
- - name: test ansible
- shell: echo "shell 1">>/tmp/a
- - name: test2
- shell: echo "shell 2">>/tmp/a
运行: ansible-playbook /usr/local/src/test.yaml
- #cat /usr/local/src/test_copy.yaml
- - hosts: testgroup
- tasks:
- - name: test ansible
- shell: echo "shell 1">>/tmp/a
- - name: test copy
- copy: src=/tmp/a dest=/usr/local/src/
- # 下发 nginx 配置, 对 nginx 进行检测
- #cat /usr/local/src/test_nginx.yaml
- - hosts: testgroup
- tasks:
- - name: copy nginx conf
- copy: src=/tmp/nginx.conf dest=/usr/local/nginx/conf/ backup=yes
- - name: nginx conf check
- shell: /usr/local/nginx/sbin/nginx -t
- register: nginx_result
- - debug: var=nginx_result
- # 可只输出 stdout_lines 或者 stderr_lines
来源: http://www.bubuko.com/infodetail-2662309.html