ansible批量执行命令

批量管理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模块

    1. 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

猜你喜欢

转载自blog.51cto.com/395469372/2133486