zpjx 发表于 2018-7-29 12:11:26

ansible-hades

  ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
  ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:
  (1)、连接插件connection plugins:负责和被监控端实现通信;
  (2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;
  (3)、各种模块核心模块、command模块、自定义模块;
  (4)、借助于插件完成记录日志邮件等功能;
  (5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。
  实验环境
  centos6.9_x64
  server192.168.1.128
  slave   192.168.1.135
  实验软件
  ansible
  软件安装
  master python版本2.6+
  slave python版本2.4+
  wget http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm
  rpm -ivh epel-release-6-8.noarch.rpm
  sed -i 's/^mirrorlist=https/mirrorlist=http/' /etc/yum.repos.d/epel.repo
  yum clean all
  yum makecache
  python --version
  Python 2.6.6
  yum install ansible* -y master/salve端操作
  ps -aux | grep ansible
  Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
  root      23720.00.0 103332   884 pts/0    S+   09:37   0:00 grep ansible
  vim /etc/ansible/hosts
  ##
  ## alpha.example.org
  ## beta.example.org
  ## 192.168.1.100
  ## 192.168.1.110
  
  192.168.1.135 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=1
  组名可自定义,添加slave 用户名 端口 密码,也可以使用ssh信任的方式
  生产环境禁止root sudo授权实现 重启服务等操作
  cp /etc/ansible/ansible.cfg /etc/ansible/ansible.cfg.bak
  more /etc/ansible/ansible.cfg| grep host_key_checking
  #host_key_checking = False
  sed -i 's/#host_key_checking = False/host_key_checking = False/g' /etc/ansible/ansible.cfg
  测试
  ansible-doc -l查看功能模块
  ansible-doc -l | grep copy 查看具体功能模块
  -m 添加模块执行
  -a '命令'
  ansible all 为所有主机
  ansible server 为自定义分组批量执行命令
  ansible all -m ping
  192.168.1.135 | SUCCESS => {
  "changed": false,
  "ping": "pong"
  }               提示说明slave可以Ping通
  ansible all -a 'uptime' 以此类推ls cp等
  192.168.1.135 | SUCCESS | rc=0 >>
  09:43:36 up 29 min,3 users,load average: 0.00, 0.02, 0.00
  ansible server -m copy-a"src=/home/test.shdest=/tmp/ owner=root group=root mode=0755" 批量复制脚本
  ansible server -m shell -a   "/bin/sh /tmp/test.sh"                              执行脚本
  ansible java   -m shell -a   "/bin/sh /tmp/test.sh"--sudo                     sudo提权执行脚本
  ansible server -m file-a"dest=/home/test.txt state=touch"    创建文件
  ansible server -m file -a "dest=/home/test.txt state=absent"      删除文件
  ansible server -m file -a "dest=/home/testsmode=755 owner=root group=root state=directory"建立目录
  ansible server -m file -a "dest=/home/tests state=absent"       删除目录
  ansible server -m yum   -a "name=httpd"安装服务
  ansible server -m yum   -a "name=vsftpd"
  ansible server -m service -a "name=httpd state=started/restarted/stopped enabled=yes"
  启动服务
  ansible server -m service -a "name=vsftpd state=started enabled=yes"
  openssl passwd -1 -salt 123.com    123.com 为用户密码,opnenssl生成密文密码
  ansible server -m user -a 'name=test comment="add a test user" password="$1$123.com$6Oaka602q3MP5w4ZaugbB0"'建立用户设置密码
  ansible server -m group -a "name=g1 gid=666 state=present system=yes"新建组 g1位组名
  ansible server -m group -a "name=g1 state=absent"                删除组
  ansible server -m command -a "id test01" 查看用户
  ansible server -m user -a "name=test01 state=absent remove=yes"   删除用户
  ansible server -a "uptime"执行命令
  ansible server -m raw -a "ifconfig eth0"            查看主机IP地址
  ansible server -m raw -a "tail /etc/group | grep g1"查看组
  ansible java   -m setup -a "filter=ansible_eth"查看ip地址
  ansible all-m ping       查看主机存活情况
  ansible server -m setup      系统信息收集
  ansible server --list      查看分组主机
  ansible test -u root -m setup查看系统信息
  ansible server   -a "rpm -qavsftpd"查看安装rpm包
  ansible server   -a "lsof -i:21"      查看服务状态
  ansible server   -a "netstat -tuplna | grep vsftpd"
  ansible all -a "/sbin/reboot" -f 10 --sudo -K重启 --sudo 为普通用户sodu权限
页: [1]
查看完整版本: ansible-hades