Python uses ansible to distribute and process tasks

Source: Internet
Author: User
Tags ssh port
This article mainly introduces the distribution system for Python to use ansible to distribute and process tasks. For more information, see the distribution system for users familiar with python who can use paramiko to write tasks, combined with the gevent coroutine, asynchronous processing can be implemented.

If you only want to use tools, you can use some tools, such as {puppet, saltstack, fabric, ansible, chef}. In fact, these tools are very useful, but they are not at the cost of learning, I suggest you use ansible. this module is well encapsulated and has complete functions.

First install ansible

The code is as follows:

Pip install ansible # In fact, I suggest you use ubuntu systems. ubuntu systems support open-source products well, and apt functions are much better than yum.

The code is as follows:

Mkdir-p/etc/ansible/# Create a configuration file directory for 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, add the password to the end. if your SSH port has been changed, add the port number behind the IP address. If an error is reported, install apt-get install sshpass first.

After completing the above preparations, our ansible will be able to run.

Ansible Boss + Merchant + Web-m shell-a "hostname; ip a "the Boss + Merchant + Web here is the host group I defined in the hosts file-m is followed by the module-a, followed by 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 inet 127.0.0.1/8 scope host lo2: eth0:
  
   
Mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00: 0c: 29: 96: 06: 4e brd ff: ff inet 192.168.6.210/24 brd 192.168.6.255 scope global eth0
  
 

The module information can be carefully viewed from the tutorials on the official website. I usually use shell anyway...

To play playbook now is to save the command to the file and execute it again. you don't have to use it. we can also write code to encapsulate these commands.

Mkdir-p/etc/ansible/playbookstouch boss. ymlmore boss. in the yml-hosts: Boss + Merchant + Web hosts file, the host group remote_user: root tasks:-name: update_boss task code shell: source/etc/profile; whoami; uptime; cat/etc/issue module + command # shell: echo "xxxxxxxxxxxxxxxxx"

Run

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 does the cowsay print it out? what is the situation?

Use-vvvv to view the specific execution status

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+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 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
  
 

Similar to debug.

The above is all the content of this article. I hope you will like it.

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.