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

发布时间:2021-11-04 16:42:10 作者:柒染
来源:亿速云 阅读:141

这篇文章给大家介绍集群管理工具ansible常用命令有哪些,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

集群管理工具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公钥到各个节点

[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

执行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"
}

指定用户,执行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"
}

执行命令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

执行命令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

执行命令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

执行命令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 >>

执行命令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

执行命令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

指定其中一个主机执行命令

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

执行命令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% /

执行命令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

执行命令,查看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常用命令有哪些就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. Ansible常用的模块有哪些?
  2. ansible常用命令使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ansible

上一篇:MyDomain老牌免费域名DNS解析服务是怎么样的

下一篇:自动化运维工具ansible怎么用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》