欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用ansible远程管理集群

程序员文章站 2023-12-30 13:12:58
...

使用ansible远程执行命令

1.ansible简介

ansible的官方定义:“Ansible is Simple IT Automation”——简单的自动化IT工具。这个工具的目标:

  • 自动化部署APP
  • 自动化管理配置项
  • 自动化的持续交付
  • 自动化的(AWS)云服务管理。

其本质上就是在远程在多台服务器执行一系列命令和文件同步,和以前的介绍的使用并行ssh提高工作效率功能类似,他们都是使用ssh协议进行远程操作,但ansible比pssh功能更强大,比如支持主机列表分组、支持playbook模板文件等。本文仅仅介绍ansible的Ad-Hoc用法,即默认的command模块,直接在shell执行命令。

2.安装

ubuntu14.04直接使用apt-get安装:

sudo apt-get install -y ansible

也可以使用pip命令安装:

sudo pip install ansible

为了支持输入远程主机用户密码,还需要安装sshpass工具:

sudo apt-get install -y sshpass

安装完成后创建~/.hosts文件,内容如下:

[local]
ceph-0
[mon]
ceph-1
[osd]
ceph-2
ceph-3

以上配置文件定义了三个主机组,分别为localmonosdceph-x是主机名。ansible执行需要指定主机列表文件,默认为/etc/hosts,用户也可以通过-i hosts_file指定,我们修改默认文件为我们刚刚创建的新文件,创建~/.ansible.cfg,增加以下内容:

[defaults]
hostfile=~/.hosts

3.使用ansible

ansible的简单语法为:

ansible <host-pattern> [-f forks] [-m module_name] [-a args]

其中host-pattern指定主机组,比如上面的osdlocal等,-f指定并行数,默认为5-m指定模块名,比如ping表示探测远程主机是否可访问,command表示执行shell命令,copy表示传输文件等,默认为command-a是指定选项参数,不同的模块具有不同的参数,比如ping不需要选项,command需要指定执行的命令,copy需要指定srcdest等。另外还有以上提到的-i指定主机列表文件、-u指定远程执行用户名等。
在所有的osd节点执行uptime操作,远程主机必须有一样的用户名和密码,如果不指定用户名,则默认使用当前登录主机的用户名,否则如果和登录主机用户名不一样,必须通过-u username指定远程主机:

ansible osd -a 'uptime'

输出:

ceph-3 | FAILED => SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue
ceph-2 | FAILED => SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue

命令执行失败,我们使用-vvvv选项查看详细信息:

ansible -vvvv osd -a 'uptime'

输出:

ebug1: Trying private key: /home/fgp/.ssh/id_rsa
debug3: no such identity: /home/fgp/.ssh/id_rsa: No such file or directory
debug1: Trying private key: /home/fgp/.ssh/id_dsa
debug3: no such identity: /home/fgp/.ssh/id_dsa: No such file or directory
debug1: Trying private key: /home/fgp/.ssh/id_ecdsa
debug3: no such identity: /home/fgp/.ssh/id_ecdsa: No such file or directory
debug1: Trying private key: /home/fgp/.ssh/id_ed25519
debug3: no such identity: /home/fgp/.ssh/id_ed25519: No such file or directory
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
Permission denied (publickey,password).

说明我们既没有**文件也没有输入用户密码,因此无法通过ssh认证,需要输入密码,使用-k选项:

➜  ~ ansible  osd -a 'uptime' -k
SSH password:
ceph-2 | success | rc=0 >>
 11:01:17 up 1 day, 6 min,  5 users,  load average: 0.02, 0.02, 0.05

ceph-3 | success | rc=0 >>
 11:01:17 up 1 day, 6 min,  5 users,  load average: 0.03, 0.03, 0.05

执行成功了,输入一次密码后,ansible会保存认证session,在session有效期内,不需要重复输入密码,即在执行了以上命令后,不需要再传递-k参数:

➜  ~ ansible  osd -a 'uptime'
ceph-2 | success | rc=0 >>
 11:02:14 up 1 day, 7 min,  5 users,  load average: 0.01, 0.02, 0.05

ceph-3 | success | rc=0 >>
 11:02:14 up 1 day, 7 min,  5 users,  load average: 0.01, 0.02, 0.05

有效期只有几分钟时间,为了避免每次输入密码,建议还是通过设置**来实现免密码登录,若本地还没有生成**文件,则先使用ssh-******命令生成**文件:

➜  ~ ssh-******                                                                                                                                                              [1/1877]
Generating public/private rsa key pair.
Enter file in which to save the key (/home/fgp/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/fgp/.ssh/id_rsa.
Your public key has been saved in /home/fgp/.ssh/id_rsa.pub.
The key fingerprint is:
49:8b:d0:a0:29:69:c8:32:50:3d:fc:8a:0a:4e:c8:1d [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|...o.            |
|+. o+o           |
|*oo .o. .        |
|oo E ..o o       |
|o ..... S        |
|oo...            |
|+.               |
|..               |
|                 |
+-----------------+

我们把~/.ssh/id_rsa.pub文件拷贝到所有的主机,拷贝文件需要指定-m模块名为copy,指定所有的主机的host-patternall:

ansible all -m copy -a 'src=~/.ssh/id_rsa.pub dest=~' -k
ansible all -a 'ls' -k # 查看是否传输成功

接下来把公钥追加到~/.ssh/authorized_keys中,我们需要执行cat ~/id_rsa.pub >> ~/.ssh/命令,但默认的command模块是不支持重定向和管道的,为了使用重定向和管道,我们使用shell模块:

ansible all -m shell -a 'mkdir -p .ssh' # assure ~/.ssh exist!
ansible all -m shell -a 'cat ~/id_rsa.pub >>  ~/.ssh/authorized_keys' -k

验证下是否工作,注意下面的命令没有指定-k选项:

ansible all -m shell -a 'cat .ssh/authorized_keys'

输出:

➜  ~ ansible all -m shell -a 'cat .ssh/authorized_keys'                                                                                                                      [1/1839]
ceph-0 | success | rc=0 >>
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCxjl++nrmghoRVQnnJALR8Ia6eD87hdewZ9XZP9Ay3ZU1eU9F5MF0A7I7UY08kY7az7+14YJeP0T+zhEl8trc6NDV47LJnMG8ONVePokCeCvFgukUa8QpAhMWXSRSyUFA3Q4LpVmRu2nat$lSrwhu0W7uazq9OA5YxSCZRV/lb6bTsrrywBT4s9Crr5DWKUeZ1uKeUVghz0KmxH/ICWyFGE3v3OsqTMvtWM/R5m6FIgb86bd3CsM4UAP4v5I4FEx4+iqsbtvww3qOkY3Qj91AGOuYq8yNhFmQVN7VZZ9OR/8Vc0iI1wOG+vylbEJjr0/pjX$pPzPrOtW0Q6PjTKZXL [email protected]

ceph-3 | success | rc=0 >>
ssh-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5m6FIgb86bd3CsM4UAP4v5I4FEx4+iqsbtvww3qOkY3Qj91AGOuYq8yNhFmQVN7VZZ9OR/8Vc0iI1wOG+vylbEJjr0/pjX$pPzPrOtW0Q6PjTKZXL [email protected]

可见我们免密码执行远程命令,并且验证了公钥已经追加到~/.ssh/authorized_keys中。
下面我们执行一下更新操作,命令为apt-get update -y:

ansible all -m shell -a 'apt-get update -y'

输出结果:

ceph-1 | FAILED | rc=100 >>
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

ceph-2 | FAILED | rc=100 >>
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

ceph-3 | FAILED | rc=100 >>
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

执行失败了,显然是由于没有root权限,需要使用sudo执行命令,需要`--sudo``选项:

ansible all --sudo -m shell -a 'apt-get update -y'

如果没有**,需要输入sudo密码,需要指定-K选项(大写的K)。

4.总结

ansible的功能非常强大,以上只介绍了如何在命令行远程执行命令,ansible还有更强大的playbook功能,playbook通过yaml文件定义,类似puppet的模板文件,具体可以参考官方文档。

上一篇:

下一篇: