Python uses ansible to distribute processing tasks

Source: Internet
Author: User
Tags ssh port
In fact, Python is familiar with the people can use their own Paramiko to write the task of the distribution system, combined with the gevent of the process can be asynchronous processing.

If only want to use tools friends can use some tools, like {Puppet,saltstack,fabric,ansible,chef}, and so on, in fact, these tools are very useful, but at the cost of learning, I suggest you use Ansible, this module package is good, The function is also very complete.

Let's first install the Ansible

The code is as follows:

Pip Install Ansible #其实我建议大家用ubuntu的系统, the Ubuntu system supports everything from open source, and apt is much more powerful than Yum.

The code is as follows:

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

If your machine does not have password-free access, then add the password to it, if your SSH port has been changed, then add the port number behind the IP. If there is an error, you first install Apt-get installed Sshpass

With all these preparations, our ansible will be able to run.

Ansible boss+merchant+web-m shell-a "Hostname;ip a"                here Boss+merchant+web is the host group I defined in the Hosts file-  m is followed by module-A, Following is the command for execution 192.168.6.210 | Success | Rc=0 >>pay-boss+merchant+web1: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 Lo2:eth0: 
  
   
    
    MTU-Qdisc pfifo_fast Qlen-  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
  
   
 
  

Module information can be viewed from the official website of the tutorial carefully, anyway, I generally use the shell ...

Now to play playbook, is actually to save the command to a file, then execute, that is one thing, do not have to use this, we can also write code to encapsulate these commands

Mkdir-p/etc/ansible/playbookstouch Boss.ymlmore        The host group in the Boss.yml-hosts:boss+merchant+web Hosts file Remote_user:roo T tasks:    -name:update_boss       Task code     shell:source/etc/profile;whoami;uptime;cat/etc/issue        module + command #     Shell:echo "Xxxxxxxxxxxxxxxxx"

Perform

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

This is the result, but how this cowsay print out, I went, what is this situation?

Use-VVVV to view specific executions

Ansible boss+merchant+web-m shell-a "Hostname;ip a"-vvvv<192.168.6.210> establish CONNECTION for user:root<19 2.168.6.210> remote_module Command hostname;ip a #USE_SHELL <192.168.6.210> EXEC sshpass-d6 ssh-c-tt-vvv-o Co Ntrolmaster=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+web1: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 Lo2:e Th0: 
   
    MTU qdisc pfifo_fast qlen-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
    

  
 

Similar to debug.

The above mentioned is the whole content of this article, I hope you can like.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.