dsfsfs 发表于 2018-7-29 12:49:30

ansible安装后的简单使用

  ansible server服务端 安装之后需要执行的步骤:
  1、创建密钥:
  ssh-keygen -t rsa
  /root/.ssh目录下生成:
  id_rsa 为公钥
  id_rsa.pub 为私钥

  # cat>  2、分发公钥到其他客户端,实现无密钥登录(执行命令是追加不会覆盖之前的内容)
  ssh-copy-id -i /root/.ssh/id_rsa.pub root@客户端IP
  3、客户端批量安装推送密钥所需依赖包
  ansible all -m command -a 'yum -y install libselinux-python'
  运行简单的ping测试
  # ansible all -m ping
  : provided hosts list is empty, only localhost is available
  : No hosts matched, nothing to do
  出现警告是因为,hosts还没配置主机列表;
  1.1 主机ip列表方式:
  vi/etc/ansible/hosts
  192.168.20.12
  # ansible all -m ping
  192.168.20.12 | SUCCESS => {
  "changed": false,
  "ping": "pong"
  }
  SUCCESS:表示成功
  false:表示未进行改变
  pong:返回值,表示成功
  批量执行命令
  # ansibleall -m command -a 'w'
  192.168.20.12 | SUCCESS | rc=0 >>
  14:16:05 up3:17,2 users,load average: 0.00, 0.00, 0.00

  USER   TTY      FROM            LOGIN@>  root   pts/0    192.168.20.1   10:58    7:46   0.32s0.32s -bash
  root   pts/1    192.168.20.14    14:16    0.00s0.11s0.00s /bin/sh -c /usr
  1.2 分组方式:
  vi/etc/ansible/hosts
  
  192.168.20.14
  # ansible server -m command -a 'uptime'
  192.168.20.14 | SUCCESS | rc=0 >>
  16:37:00 up5:36,2 users,load average: 0.08, 0.02, 0.00
  1.3 正则匹配
  vi/etc/ansible/hosts
  
  192.168.20.1
  匹配 12 13 14 三台机器;
  # ansible server -m command -a "uptime"
  192.168.20.12 | SUCCESS | rc=0 >>
  16:43:23 up5:45,2 users,load average: 0.00, 0.00, 0.00
  192.168.20.14 | SUCCESS | rc=0 >>
  16:43:23 up5:42,2 users,load average: 0.01, 0.01, 0.00
  192.168.20.13 | UNREACHABLE! => {
  "changed": false,
  "msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.20.13 port 22: No route to host\r\n",
  "unreachable": true
  }
  13服务器没有开机所以提示错误;
  单独指定主机列表文件
  # ansible -i a.txt all -m command -a 'w'
  192.168.20.14 | SUCCESS | rc=0 >>
  14:18:43 up3:17,2 users,load average: 0.00, 0.00, 0.00

  USER   TTY      FROM            LOGIN@>  root   pts/0    192.168.20.1   11:06    1.00s0.92s0.49s /usr/bin/python
  root   pts/3    192.168.20.14    14:18    0.00s0.10s0.00s /bin/sh -c /usr
  192.168.20.12 | SUCCESS | rc=0 >>
  14:18:49 up3:20,2 users,load average: 0.00, 0.00, 0.00

  USER   TTY      FROM            LOGIN@>  root   pts/0    192.168.20.1   10:58   10:30   0.32s0.32s -bash
  root   pts/1    192.168.20.14    14:18    0.00s0.07s0.00s /bin/sh -c /usr
  # cat a.txt
  192.168.20.12
  192.168.20.14
  分发文件:
  # ansibleall -m copy -a 'src=./1.sh dest=/root/1.sh'
  192.168.20.12 | SUCCESS => {
  "changed": true,
  "checksum": "5cc8dde04b6f1062c79188a4281f7e07d20cc2cc",
  "dest": "/root/1.sh",
  "gid": 0,
  "group": "root",
  "md5sum": "6bfe4fbfe529c3f56fe061146dc0d693",
  "mode": "0644",
  "owner": "root",
  "size": 33,
  "src": "/root/.ansible/tmp/ansible-tmp-1491458881.34-135958350771796/source",
  "state": "file",
  "uid": 0
  }
页: [1]
查看完整版本: ansible安装后的简单使用