python中在自动化运维的应用

写得比较乱,前后语境不好理解,仅作自己留存使用。

一 python-paramiko
python ../ssh.py hosts_vertical_new cmd  root


python ../sftp.py -p hosts_vertical_new root /root/python/audit/audit.rules  /etc/audit/audit.rules
python ../sftp.py -p hosts_vertical_new root /root/python/audit/auditd.conf   /etc/audit/auditd.conf  
python ../sftp.py -p hosts_vertical_new root /root/python/audit/audit.tar /opt/
python ../ssh.py hosts_vertical_new cmd_tarx_bash  root
python ../sftp.py -g hosts_vertical_new root /opt/audit/audit-check.log ./result/

[root@bigdataclient sudo]# python /root/python/ssh.py hosts.hiv01  cmd_hive  root  
Password:
scchdphiv02101
SCCHDPHIV02101

[root@bigdataclient sudo]# python /root/python/ssh.py hosts_hive  cmd_chpasswdomm root
Password:
scchdphiv02124
Changing password for user omm.
passwd: all authentication tokens updated successfully.
Adjusting aging data for user omm.
passwd: Success

二 ansible
yum install ansible
[root@istsvr01:/root/python]#ansible all -i host_stormtag  --list-hosts   从文本host_stromtag中list机器信息
[root@istsvr01:/root/python]#ansible all -i host_stormtag -m shell -a "hostname" -k -u adminzhy--默认是root用户
[root@istsvr01:/root/python]#ansible all -i host_stormtag -m command -a "whoami"  -k -u adminzhy--默认是root用户
ansible  hadoopservers -m shell -a 'touch a'  -k -u adminzhy -K -s #使用adminzhy执行sudo touch一个文件a,hadoopservers 是/etc/ansible/hosts中的一个服务器组
ansible all -i host_stormtag -m command -a "whoami"  -k -u adminzhy  #host_stormtag 是一个独立的host文件,不在 /etc/ansible/hosts中
ansible all -i host_tagsvr -m shell -a "df -h"  -k -u adminzhy  -K -s #host_stormtag 是一个独立的host文件,不在 /etc/ansible/hosts中

1.vim /etc/ansible/ansible.cfg
2.主机组定义
vim /etc/ansible/hosts
[storm_cluster]
10.223.55.100
10.223.55.101
3 复制文件到远程机器
backup:在覆盖之前,将源文件备份,备份文件包含时间信息。有两个选项:yes|no
content:用于替代“src”,可以直接设定指定文件的值
dest:必选项。要将源文件复制到的远程主机的绝对路径,如果源文件是一个目录,那么该路径也必须是个目录
directory_mode:递归设定目录的权限,默认为系统默认权限
force:如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes
others:所有的file模块里的选项都可以在这里使用
src:被复制到远程主机的本地文件,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用“/”来结尾,则只复制目录里的内容,如果没有使用“/”来结尾,则包含目录在内的整个内容全部复制,类似于rsync。

示例
## 将本地文件“/etc/ansible/ansible.cfg”复制到远程服务器
# ansible storm_cluster -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=root group=root mode=0644"
4远程执行shell脚本
# ansible storm_cluster -m shell -a "/tmp/rocketzhang_test.sh"
 
建立互信:
 ansible all -m copy -a "src=/root/.ssh/id_rsa.pub dest=/root" -k
 ansible all -m shell -a "cat /root/id_rsa.pub >> /root/.ssh/authorized_keys"
 ansible all -m shell -a "rm -f /root/id_rsa.pub"
istory |grep -A 3 -B 3 ansible

原文地址:https://www.cnblogs.com/dotagg/p/6364288.html