溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

集群管理工具ansible常用命令有哪些

發(fā)布時間:2021-11-04 16:42:10 來源:億速云 閱讀:152 作者:柒染 欄目:建站服務器

這篇文章給大家介紹集群管理工具ansible常用命令有哪些,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

集群管理工具ansible常用命令

使用ansible過程如下:

主控端:安裝ansible

# install the epel-release RPM if needed on CentOS, RHEL, or Scientific Linux

[root@tidb01 ~]# yum install epel-release
[root@tidb01 ~]# yum -y install ansible
[root@tidb01 ~]# ansible --version
ansible 2.9.10
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]

配置hosts,添加被管控的主機

[root@tidb01 ~]# cat /etc/ansible/hosts 

[servers]
192.168.30.101
192.168.30.102
#192.168.30.103
#192.168.30.104
192.168.30.105
#192.168.30.106

生成密鑰

[root@tidb01 ~]# ssh-keygen

使用ssh-copy-id命令來復制ansible公鑰到各個節(jié)點

[root@tidb01 ~]# ssh-copy-id root@192.168.30.101
[root@tidb01 ~]# ssh-copy-id root@192.168.30.102
[root@tidb01 ~]# ssh-copy-id root@192.168.30.105

執(zhí)行ping命令

[root@tidb01 ~]# ansible servers -m ping
#或:[root@tidb01 ~]# ansible servers -i /etc/ansible/hosts -m ping
#或:[root@tidb01 ~]# ansible -m ping servers
#或:[root@tidb01 ~]# ansible -m ping all
192.168.30.102 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.30.105 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.30.101 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

指定用戶,執(zhí)行ping命令

[root@tidb01 ~]# ssh-copy-id chen@192.168.30.101
[root@tidb01 ~]# ssh-copy-id chen@192.168.30.102
[root@tidb01 ~]# ssh-copy-id chen@192.168.30.105
[root@tidb01 ~]# ansible all -m ping -u chen
192.168.30.105 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.30.102 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.30.101 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

執(zhí)行命令echo hello 

[root@tidb01 ~]# ansible -a "echo hello" servers
192.168.30.105 | CHANGED | rc=0 >>
hello
192.168.30.101 | CHANGED | rc=0 >>
hello
192.168.30.102 | CHANGED | rc=0 >>
hello

執(zhí)行命令date -R

[root@tidb01 ~]# ansible -m command -a "date -R" servers
192.168.30.102 | CHANGED | rc=0 >>
Sun, 28 Jun 2020 09:15:55 +0800
192.168.30.105 | CHANGED | rc=0 >>
Sun, 28 Jun 2020 09:15:54 +0800
192.168.30.101 | CHANGED | rc=0 >>
Sun, 28 Jun 2020 09:15:56 +0800

執(zhí)行命令uptime

[root@tidb01 ~]# ansible -m command -a "uptime" servers
192.168.30.102 | CHANGED | rc=0 >>
 09:17:21 up 23 min,  2 users,  load average: 0.00, 0.01, 0.05
192.168.30.105 | CHANGED | rc=0 >>
 09:17:21 up 22 min,  2 users,  load average: 0.24, 0.06, 0.07
192.168.30.101 | CHANGED | rc=0 >>
 09:17:22 up 11 min,  2 users,  load average: 0.08, 0.09, 0.07

執(zhí)行命令touch /root/cjc.log

[root@tidb01 ~]# ansible -m command -a "touch /root/cjc.log" servers
[WARNING]: Consider using the file module with state=touch rather than running 'touch'.  If
you need to use command because file is insufficient you can add 'warn: false' to this command
task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
192.168.30.102 | CHANGED | rc=0 >>
192.168.30.105 | CHANGED | rc=0 >>
192.168.30.101 | CHANGED | rc=0 >>

執(zhí)行命令ls /root/cjc.log

[root@tidb01 ~]# ansible -m command -a "ls /root/cjc.log" servers
192.168.30.105 | CHANGED | rc=0 >>
/root/cjc.log
192.168.30.102 | CHANGED | rc=0 >>
/root/cjc.log
192.168.30.101 | CHANGED | rc=0 >>
/root/cjc.log

執(zhí)行命令getenforce

[root@tidb01 ~]# ansible -m command -a "getenforce" servers
192.168.30.102 | CHANGED | rc=0 >>
Disabled
192.168.30.105 | CHANGED | rc=0 >>
Disabled
192.168.30.101 | CHANGED | rc=0 >>
Disabled

或: 

[root@tidb01 ~]# ansible -a "getenforce" servers
192.168.30.105 | CHANGED | rc=0 >>
Disabled
192.168.30.102 | CHANGED | rc=0 >>
Disabled
192.168.30.101 | CHANGED | rc=0 >>
Disabled

指定其中一個主機執(zhí)行命令

[root@tidb01 ~]# ansible -a "getenforce" 192.168.30.101
192.168.30.101 | CHANGED | rc=0 >>
Disabled

執(zhí)行命令df -h

[root@tidb01 ~]# ansible -a "df -h /" servers
192.168.30.102 | CHANGED | rc=0 >>
Filesystem                  Size  Used Avail Use% Mounted on
/dev/mapper/vg_cjc-lv_root   18G  7.1G   11G  40% /
192.168.30.105 | CHANGED | rc=0 >>
Filesystem                  Size  Used Avail Use% Mounted on
/dev/mapper/vg_cjc-lv_root   18G  7.0G   11G  40% /
192.168.30.101 | CHANGED | rc=0 >>
Filesystem                  Size  Used Avail Use% Mounted on
/dev/mapper/vg_srv-lv_root   25G  8.1G   17G  33% /

執(zhí)行命令free -m

[root@tidb01 ~]# ansible -a "free -m" servers
192.168.30.105 | CHANGED | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:            974         411         116           8         446         350
Swap:          2043           0        2043
192.168.30.102 | CHANGED | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:            974         414         156           8         403         350
Swap:          2043           0        2043
192.168.30.101 | CHANGED | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:            974         413          98           8         462         367
Swap:          3071           0        3071

執(zhí)行命令,查看sshd進程

[root@tidb01 ~]# ansible -m shell -a 'ps -ef|grep sshd|grep -v grep' servers
192.168.30.105 | CHANGED | rc=0 >>
root       1374      1  0 08:54 ?        00:00:00 /usr/sbin/sshd -D
root       2090   1374  0 08:56 ?        00:00:00 sshd: root@pts/0
root       8581   1374  0 10:38 ?        00:00:00 sshd: root@pts/1
192.168.30.102 | CHANGED | rc=0 >>
root       1370      1  0 08:54 ?        00:00:00 /usr/sbin/sshd -D
root       2089   1370  0 08:56 ?        00:00:00 sshd: root@pts/0
root       9041   1370  0 10:38 ?        00:00:00 sshd: root@pts/2
root      10181   1370  0 10:41 ?        00:00:00 sshd: root@pts/1
192.168.30.101 | CHANGED | rc=0 >>
root       1009      1  0 09:06 ?        00:00:00 /usr/sbin/sshd -D
root       1242   1009  0 09:06 ?        00:00:00 sshd: root@pts/1
root       8590   1009  0 10:38 ?        00:00:00 sshd: root@pts/2

關于集群管理工具ansible常用命令有哪些就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI