标签:ansible
ansible server服务端 安装之后需要执行的步骤:
1、创建密钥:
ssh-keygen -t rsa
/root/.ssh目录下生成:
id_rsa 为公钥
id_rsa.pub 为私钥
# cat id_rsa.pub >> authorized_keys
2、分发公钥到其他客户端,实现无密钥登录(执行命令是追加不会覆盖之前的内容)
ssh-copy-id -i /root/.ssh/id_rsa.pub root@客户端IP
3、客户端批量安装推送密钥所需依赖包
ansible all -m command -a ‘yum -y install libselinux-python‘
运行简单的ping测试
[root@localhost ansible]# ansible all -m ping
[WARNING]: provided hosts list is empty, only localhost is available
[WARNING]: No hosts matched, nothing to do
出现警告是因为,hosts还没配置主机列表;
1.1 主机ip列表方式:
vi /etc/ansible/hosts
192.168.20.12
[root@localhost ansible]# ansible all -m ping
192.168.20.12 | SUCCESS => {
"changed": false,
"ping": "pong"
}
SUCCESS:表示成功
false:表示未进行改变
pong:返回值,表示成功
批量执行命令
[root@localhost ansible]# ansible all -m command -a ‘w‘
192.168.20.12 | SUCCESS | rc=0 >>
14:16:05 up 3:17, 2 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.20.1 10:58 7:46 0.32s 0.32s -bash
root pts/1 192.168.20.14 14:16 0.00s 0.11s 0.00s /bin/sh -c /usr
1.2 分组方式:
vi /etc/ansible/hosts
[server]
192.168.20.14
[root@localhost ansible]# ansible server -m command -a ‘uptime‘
192.168.20.14 | SUCCESS | rc=0 >>
16:37:00 up 5:36, 2 users, load average: 0.08, 0.02, 0.00
1.3 正则匹配
vi /etc/ansible/hosts
[server]
192.168.20.1[2:4]
匹配 12 13 14 三台机器;
[root@localhost ansible]# ansible server -m command -a "uptime"
192.168.20.12 | SUCCESS | rc=0 >>
16:43:23 up 5:45, 2 users, load average: 0.00, 0.00, 0.00
192.168.20.14 | SUCCESS | rc=0 >>
16:43:23 up 5: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服务器没有开机所以提示错误;
单独指定主机列表文件
[root@localhost ansible]# ansible -i a.txt all -m command -a ‘w‘
192.168.20.14 | SUCCESS | rc=0 >>
14:18:43 up 3:17, 2 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.20.1 11:06 1.00s 0.92s 0.49s /usr/bin/python
root pts/3 192.168.20.14 14:18 0.00s 0.10s 0.00s /bin/sh -c /usr
192.168.20.12 | SUCCESS | rc=0 >>
14:18:49 up 3:20, 2 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.20.1 10:58 10:30 0.32s 0.32s -bash
root pts/1 192.168.20.14 14:18 0.00s 0.07s 0.00s /bin/sh -c /usr
[root@localhost ansible]# cat a.txt
192.168.20.12
192.168.20.14
分发文件:
[root@localhost ansible]# ansible all -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
}
本文出自 “模范生的学习博客” 博客,请务必保留此出处http://mofansheng.blog.51cto.com/8792265/1913919
标签:ansible
原文地址:http://mofansheng.blog.51cto.com/8792265/1913919