ansible安裝後的簡單使用

ansible server服務端 安裝以後須要執行的步驟:python

一、建立密鑰:linux

ssh-keygen -t rsabash

/root/.ssh目錄下生成:服務器

id_rsa 爲公鑰ssh

id_rsa.pub 爲私鑰ide

# cat id_rsa.pub >> authorized_keys測試


二、分發公鑰到其餘客戶端,實現無密鑰登陸(執行命令是追加不會覆蓋以前的內容)ui

ssh-copy-id -i /root/.ssh/id_rsa.pub root@客戶端IPspa


三、客戶端批量安裝推送密鑰所需依賴包server

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

}

相關文章
相關標籤/搜索