最近抽时间了解了一下linux一些简单的运维工具,看到网上推荐ansible简单容易上手,学习一下。
简介
Ansilbe是一个部署一群远程主机的工具。远程的主机可以是远程虚拟机或物理机, 也可以是本地主机。Ansible默认通过 SSH 协议管理机器.
安装Ansible之后,不需要启动或运行一个后台进程,或是添加一个数据库.只要在一台电脑(可以是一台笔记本)上安装好,就可以通过这台电脑管理一组远程的机器.在远程被管理的机器上,不需要安装运行任何软件,因此升级Ansible版本不会有太多问题.
Ansible中文版教程 传送门,里面有详细的教程,有兴趣的小伙伴可以深入了解一下,这里只是记录自己学习的一些入门。
安装
这里我使用的是pip
安装,还有别的安装方式,可以参考教程。
- 安装ansible
$ sudo pip install ansible
安装包错了,显示如下:
ollecting ansible
Could not fetch URL https://pypi.python.org/simple/ansible/: There was a problem confirming the ssl certificate: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:590) - skipping
Could not find a version that satisfies the requirement ansible (from versions: )
No matching distribution found for ansible
是不是很眼熟,这个错误以前遇到过,是pip
需要升级版本了
pip install --upgrade --user pip
没有达到预料中的结果,报错了
OSError: [Errno 13] Permission denied: '/Users/zhiliang/Library/Python/2.7'
You are using pip version 9.0.1, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
那就使用sudo
命令
$ sudo pip install --upgrade pip
还是失败了,提示版本过久,建议更新新版本,难道刚才的操作不是更新新版本吗
Could not fetch URL https://pypi.python.org/simple/pip/: There was a problem confirming the ssl certificate: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:590) - skipping
Requirement already up-to-date: pip in /Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg
You are using pip version 9.0.1, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
好吧,还记得以前的更新方案,最后一招了
$ sudo curl https://bootstrap.pypa.io/get-pip.py | python
这次这招也失效了,提示文件夹权限不够
Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/EGG-INFO/dependency_links.txt'
Consider using the `--user` option or check the permissions.
很明显,是权限不够,可是都已经使用sudo
命令了,怎么还会这样,网上查了下,有人给出了修改文件夹权限的方案
$ sudo chown -R $USER /Library/Python/2.7
好了,最后再次执行更新命令: curl https://bootstrap.pypa.io/get-pip.py | python
OK,这次终于成功更新了pip
版本,显示 Successfully installed pip-18.0 wheel-0.31.1
最后一步,安装ansible
,这里使用当前用户安装,如果使用sudo的话,执行ansible命令可能需要sudo来执行:
$ pip install -U ansible
看到提示安装成功,总算放下心了,真怕再出什么幺蛾子。
配置
ansible使用ssh来管理远程机器,配置Ansible管理节点和主机的连接,这里使用基于key的无密登录管理主机。基于电脑上本身已经生成过ssh key
,所以此处跳过,没有配置的小伙伴请搜索一下ssh key
的生成教程,很简单,这里就省略了。一下是基于已经生成ssh key
来进行的。
# 拷贝当前主机的ssh key 到远程主机,ip地址替换为远程地址
$ ssh-copy-id liang@192.169.0.1
# 保存远程主机ip保存到当前主机的known_hosts
$ ssh-keyscan 192.168.0.1 >> ~/.ssh/known_hosts
验证ssh
配置,在管理节点下面执行ssh username@192.168.0.1
命令,不会提示输入密码或者校验key,即可登录远程服务器
$ ssh xxx@192.168.0.1
正常情况下,即可直接登录到远程服务器。
inventory 文件配置(主机清单)
配置文件用来配置服务器地址,用来管理多台机器。默认的文件配置地址:
/etc/ansible/hosts
如果没有,就手动创建,如果存在,只需要编辑即可。
$ cd /etc
$ mkdir ansible
$ cd ansible
$ touch hosts
将需要管理的远程主机地址配置到hosts
文件中:
[test] # test分组
192.168.0.1 # 远程服务器地址
配置完成之后,测试下是否配置生效
$ ansible all -m ping
没有预料中的结果,报错了
192.168.0.1 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).\r\n",
"unreachable": true
}
在网上查了一下,好多人碰到这种情况,也不知道这个是通病么,打印一下详细的错误信息
$ ansible all -m ping -vvv
显示了详细的错误信息
ansible 2.6.2
config file = None
configured module search path = [u'/Users/zhiliang/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /Library/Python/2.7/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 2.7.10 (default, Feb 7 2017, 00:08:15) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)]
No config file found; using defaults
Parsed /etc/ansible/hosts inventory source with ini plugin
META: ran handlers
<192.168.0.1> ESTABLISH SSH CONNECTION FOR USER: None
<192.168.0.1> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/Users/zhiliang/.ansible/cp/377f161fb5 123.57.245.206 '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''
<192.168.0.1> (255, '', 'Permission denied (publickey,password).\r\n')
192.168.0.1 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).\r\n",
"unreachable": true
}
可以看出明显的报错信息
ESTABLISH SSH CONNECTION FOR USER: None
用户名为None,这里我猜测这个是指远程服务器的名字,网上有人给了配置文件方案
[test] # test分组
192.168.0.1 ansible_user=xxx # 远程服务器地址,指定主机用户名
再次验证配置,成功ping通。
当然,也可以通过指定用户名灵活登录:
$ ansible all -m ping -u xxx
登录成功后,显示如下:
192.168.0.1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
OK,截止目前为止,基本配置已完成。
简单测试
现在我们在本地主机上执行一条简单的命令,实际是在配置下的若干台主机上同时执行该命令。
$ ansible all -a "echo hello"
输出:
192.168.0.1 | SUCCESS | rc=0 >>
hello
截止到此,我们已经通过 ansible
成功在本地执行远程服务器命令。
更多的用途,还需要慢慢学习,暂时先记录到此。。。
最近折腾微信公众账号,这一顿费劲,总算是成功申请下来了,贴一张二维码,有兴趣的可以关注下哈,没事在上线分享记录一些所见所得,一切都在摸索中,还很冷清,哈哈。

网友评论