日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python利用ansible分發處理任務

系統 1903 0

其實對python熟悉的人都可以自己用paramiko來寫任務的分發系統,再結合gevent的協程就能實現異步的處理。

如果只想用工具的朋友可以使用一些工具,類似{puppet,saltstack,fabric,ansible,chef}等,其實這些工具的都是很好用的,不過于學習的成本,我建議大家使用ansible,這個模塊封裝的不錯,功能也很齊全。

我們首先先安裝ansible把

復制代碼 代碼如下:
pip install ansible?????????????????????????? #其實我建議大家用ubuntu的系統,ubuntu的系統對開源的東西都支持的很好,而且apt的功能要比yum強不少。

復制代碼 代碼如下:
mkdir -p /etc/ansible/??????????????????????? #給ansible創建一個配置文件的目錄

            
more hosts 
[Web]
192.168.6.210:22 ansible_ssh_user=root ansible_ssh_pass=123 
[Dubbo+Zookeeper]
192.168.6.212:22 ansible_ssh_user=root ansible_ssh_pass=123
[Mysql]
192.168.6.213:22 ansible_ssh_user=root ansible_ssh_pass=123


          

如果你的機器沒有做無密碼訪問,那么就在后面加上密碼吧,如果你的SSH端口做過改動,那么請在IP后面加上端口號。如果有報錯的話,你先安裝一下apt-get install sshpass

做完上面這些準備工作,我們的ansible就可以運行了

            
ansible Boss+Merchant+Web -m shell -a "hostname;ip a"                這里的Boss+Merchant+Web是我在hosts文件中定義的主機組  -m后面是接的模塊 -a 就、后面是索要執行的命令
192.168.6.210 | success | rc=0 >>
Pay-Boss+Merchant+Web
1: lo: 
            
               mtu 16436 qdisc noqueue 
  link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  inet 127.0.0.1/8 scope host lo
2: eth0: 
              
                 mtu 1500 qdisc pfifo_fast qlen 1000
  link/ether 00:0c:29:96:06:4e brd ff:ff:ff:ff:ff:ff
  inet 192.168.6.210/24 brd 192.168.6.255 scope global eth0

              
            
          

模塊的信息可以從官網的教程下仔細查看,反正我一般都用shell。。。

現在來玩玩playbook,其實就是把命令保存到文件中,再執行,也就是一回事,不必非要用這個東西,我們自己也能寫代碼來封裝這些命令

            
mkdir -p /etc/ansible/playbooks
touch boss.yml
more boss.yml
- hosts : Boss+Merchant+Web        hosts 文件中的主機組
 remote_user : root
 tasks :
    - name : update_boss       任務代號
     shell : source /etc/profile;whoami;uptime;cat /etc/issue        模塊+命令
#     shell : echo "xxxxxxxxxxxxxxxxx"

          

執行

            
ansible-playbook boss.yml
 __________________________
< PLAY [Boss+Merchant+Web] >
 --------------------------
    \  ^__^
     \ (oo)\_______
      (__)\    )\/\
        ||----w |
        ||   ||

 _________________
< GATHERING FACTS >
 -----------------
    \  ^__^
     \ (oo)\_______
      (__)\    )\/\
        ||----w |
        ||   ||

ok: [192.168.6.210]
 ___________________
< TASK: update_boss >
 -------------------
    \  ^__^
     \ (oo)\_______
      (__)\    )\/\
        ||----w |
        ||   ||

changed: [192.168.6.210]
 ____________
< PLAY RECAP >
 ------------
    \  ^__^
     \ (oo)\_______
      (__)\    )\/\
        ||----w |
        ||   ||

192.168.6.210       : ok=2  changed=1  unreachable=0  failed=0


          

這個是結果,不過這個??? cowsay是怎么打印出來的,我了個去,這是什么情況?

利用-vvvv來查看具體的執行情況

            
ansible Boss+Merchant+Web -m shell -a "hostname;ip a" -vvvv
<192.168.6.210> ESTABLISH CONNECTION FOR USER: root
<192.168.6.210> REMOTE_MODULE command hostname;ip a #USE_SHELL
<192.168.6.210> EXEC sshpass -d6 ssh -C -tt -vvv -o ControlMaster=auto -o ControlPersist=60s -o ControlPath="/root/.ansible/cp/ansible-ssh-%h-%p-%r" -o GSSAPIAuthentication=no -o PubkeyAuthentication=no -o ConnectTimeout=10 192.168.6.210 /bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1435819148.95-1730630738440 && echo $HOME/.ansible/tmp/ansible-tmp-1435819148.95-1730630738440'
<192.168.6.210> PUT /tmp/tmpRfkD3R TO /root/.ansible/tmp/ansible-tmp-1435819148.95-1730630738440/command
<192.168.6.210> EXEC sshpass -d6 ssh -C -tt -vvv -o ControlMaster=auto -o ControlPersist=60s -o ControlPath="/root/.ansible/cp/ansible-ssh-%h-%p-%r" -o GSSAPIAuthentication=no -o PubkeyAuthentication=no -o ConnectTimeout=10 192.168.6.210 /bin/sh -c 'LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1435819148.95-1730630738440/command; rm -rf /root/.ansible/tmp/ansible-tmp-1435819148.95-1730630738440/ >/dev/null 2>&1'
192.168.6.210 | success | rc=0 >>
Pay-Boss+Merchant+Web
1: lo: 
            
               mtu 16436 qdisc noqueue 
  link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  inet 127.0.0.1/8 scope host lo
2: eth0: 
              
                 mtu 1500 qdisc pfifo_fast qlen 1000
  link/ether 00:0c:29:96:06:4e brd ff:ff:ff:ff:ff:ff
  inet 192.168.6.210/24 brd 192.168.6.255 scope global eth0

              
            
          

類似于debug。

以上所述就是本文的全部內容了,希望大家能夠喜歡。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 东辽县| 蚌埠市| 舟山市| 宁明县| 隆尧县| 张家港市| 上蔡县| 汕尾市| 五华县| 台安县| 象山县| 平遥县| 格尔木市| 东兰县| 比如县| 双峰县| 江西省| 葵青区| 邹平县| 左权县| 临安市| 高安市| 红河县| 容城县| 胶州市| 彭泽县| 永嘉县| 兰西县| 信阳市| 武宣县| 青州市| 磐石市| 北川| 酉阳| 横山县| 南溪县| 营山县| 宁阳县| 海门市| 图木舒克市| 宁晋县|