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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章