Ansible批量在远程主机执行命令

  Ansible直接执行远程命令,不用ssh登陆交互执行。
  
  如下:
  
  ansible all -i 192.168.199.180, -m shell -a "ifconfig" -u supermap
  
  参数解释:
  
  -i 连接到远程主机“192.168.199.180,”,注意这里的“,”直接指定主机名列表,没有“,”该参数则被解释为主机名的列表文件。
  
  -m 为ansible模块名称,执行shell命令。
  
  -a 远程shell命令,为ifconfig(获取网络配置参数)。
  
  -u 指定执行的远程主机用户名。
  
  -k 可以指定是否交互输入参数,如果没有,则需要在本机有预存的访问密钥。
  
  可以先使用ssh登录,选择保存,即可自动保存下来。
  
  主机文件格式,样例参考( host-list):
  
  [local]
  
  192.168.199.173 ansible_ssh_port=22 ansible_ssh_host=192.168.199.173 ansible_ssh_user=supermap ansible_ssh_pass=SuperMap
  
  192.168.199.180 ansible_ssh_port=22 ansible_ssh_host=192.168.199.180 ansible_ssh_user=supermap ansible_ssh_pass=SuperMap
  
  192.168.199.249 ansible_www.yongshi123.cn ssh_port=22 www.dfgjpt.com ansible_ssh_www.yongshiyule178.com host=192.168.199.249 ansible_ssh_user=openswitch ansible_ssh_pass=SuperMap
  
  使用主机文件,自动登录后,批量执行命令:
  
  ansible all -i host-list -m shell -a "ifconfig" -u my-username
  
  这个还是很方便的。
  
  最近遇到一些FreeBSD的故障,在这种操作系统上我们平时用的一些命令无法执行,FreeBSD的一些配置文件也和其他很多Linux系统不一样。为了方便以后查阅,我特别整理了这篇文档。
  
  1. 网络
  
  重启网络的命令:
  
  /etc/rc.d/netif restart , /etc/netstart restart
  
  网络配置文件:
  
  root@mhxy-compile02:~ # cat /etc/rc.conf
  
  ifconfig_em0="DHCP"
  
  sshd_enable="NO"
  
  ntpd_enable="YES"
  
  # Set dumpdev to "AUTO" to enable crash dumps, "NO" to disable
  
  dumpdev="AUTO"
  
  zfs_enable="YES"
  
  puppet_enable="YES"
  
  ipv6_enable="NO"
  
  hostname="mhxy-compile02.i.nease.net"
  
  nfs_client_enable="YES"
  
  rpc_lockd_enable="YES"
  
  rpc_statd_enable="YES"
  
  2. SSH
  
  命令:
  
  /etc/rc.d/sshd start , /etc/rc.d/sshd restart
  
  SSH配置文件:
  
  root@mhxy-compile02:~ # cat /etc/ssh/sshd_config
  
  # THIS FILE IS AUTOMATICALLY GENERATED BY PUPPET.
  
  # DO NOT EDIT THIS FILE.
  
  # 1. Basic
  
  Port 32200
  
  Protocol 2
  
  # 2. Authentication
  
  HostKey /etc/ssh/ssh_host_rsa_key
  
  HostKey www.boyunylpt1.com /etc/ssh/ssh_host_dsa_key
  
  KeyRegenerationInterval 3600
  
  ServerKeyBits 768
  
  UsePrivilegeSeparation yes
  
  LoginGraceTime 120
  
  PermitRootLogin no
  
  StrictModes yes
  
  RSAAuthentication yes
  
  PubkeyAuthentication yes
  
  PermitEmptyPasswords no
  
  ChallengeResponseAuthentication no
  
  PasswordAuthentication no
  
  IgnoreRhosts yes
  
  RhostsRSAAuthentication no
  
  HostbasedAuthentication no
  
  # 3. Features
  
  UseDNS no
  
  X11Forwarding www.maituyul1.cn yes
  
  X11DisplayOffset 10
  
  PrintMotd no
  
  PrintLastLog yes
  
  TCPKeepAlive yes
  
  AcceptEnv LANG LC_*
  
  # 4. Logging
  
  SyslogFacility AUTH
  
  LogLevel INFO
  
  # 5. x509
  
  Subsystem sftp www.enzuovip.com /usr/libexec/sftp-server
  
  #CACertificateFile www.huarenyl.cn /etc/ssh/ca/ca.cer
  
  3. 防火墙
  
  1)停止ipfw防火墙:
  
  ipfw disable firewall
  
  /etc/rc.d/ipfw stop
  
  2)开启ipfw防火墙:
  
  ipfw enable firewall
  
  /etc/rc.d/ipfw start
  
  3)清防火墙
  
  ipf -Fa
  
  4.赋予用户root权限
  
  1)pw groupmod www.furongpt.com wheel -m
  
  2)pw user mod -g wheel
  
  5.查看CPU和内存
  
  1)sysctl kern.smp.cpus或dmesg |grep cpu #查看CPU个数
  
  2)dmesg|more或top #查看内存
  
  参考:
  
  Ansible快速开始-指挥集群
  
  Ansible :一个配置管理和IT自动化工具(1/3)
  
  Ansible :一个配置管理和IT自动化工具(2/3)
  
  Ansible :一个配置管理和IT自动化工具 (3/3)
  
  部署生产级的 Kubernetes 集群,使用kubespray
  
  Kubeflow等镜像部署到集群多节点

猜你喜欢

转载自www.cnblogs.com/qwangxiao/p/10464066.html