wzh789 发表于 2018-7-30 09:42:12

自动运维工具ansible-11062323

  准备两台机器
  192.168.220.140a.com   ##server端
  192.168.220.145b.com   ##client端
  1. 安装   ##a.com上安装即可
  yum install -y epel-release
  yum install -y ansible
  2.配置
  (1) ssh密钥配置
  首先生成密钥对

  ssh-keygen -t rsa    ##直接回车即可,不用设置密钥密码,这样会在root家目录下生成.ssh目录,这里面也会生成两个文件>  cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys   ##把公钥(id_rsa.pub)内容放到对方机器的/root/.ssh/authorized_keys里面,包括本机
  chmod 600 /root/.ssh/authorized_keys    ##配置好client端 authorized_keys文件的权限
  (2) ansible 配置
  vi/etc/ansible/hosts##增加
      ##testhost为主机组名字,自定义的。 下面两个ip为组内的机器ip。
  127.0.0.1
  b.com
  3. 远程执行命令
  ansibletesthost -m command -a 'w'   ##testhost 为主机组名,也可以针对某一台机器来执行命令。
  错误: "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
  解决: yum install -y libselinux-python
  4. 拷贝文件或者目录
  ansible testhost-m copy -a "src=/etc/passwd dest=/tmp/ owner=root group=root mode=0644"##源目录会放到目标目录下面去。
  5. 远程执行shell脚本
  vim/tmp/test.sh##增加
  #!/bin/bash
  echo `11111111` > /tmp/1.txt
  ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mod=0755"##然后把该脚本分发到各个机器上
  ansible testhost -m shell -a "/tmp/test.sh"   ##批量执行该shell脚本并且shell模块,还支持远程执行命令并且带管道
  6. cron
  ansible testhost -m cron -a "name='test cron' job='/bin/touch /tmp/123.txt'weekday=6"##若要删除该cron 增加state=absent
  7. yum和service
  ansible testhost -m yum -a "name=httpd"
  ansible testhost -m service -a "name=httpd state=started enabled=yes"
  文档使用:
  ansible-doc -l   ##列出所有的模块
  ansible-doc cron   ##查看指定模块的文档
页: [1]
查看完整版本: 自动运维工具ansible-11062323