Mac下Ansible初探

作者: MisterCH | 来源:发表于2017-10-28 15:48 被阅读12次

安装环境

ansible需求python2.6以上,正好之前研究python的时候安装了2.7,省去了一个步骤。
ansible官方建议使用pip安装,对mac来说用Homebrew也行,命令行是:
$ brew install ansible
然后就报错了……

/usr/local/Homebrew/Library/Homebrew/brew.rb:12:in `<main>': Homebrew must be run under Ruby 2.3! You're running 2.0.0. (RuntimeError)

需求ruby2.3,可是机器里是2.0,我一度想去搜一下怎么升级ruby。然后意识到我去安装一下pip是不是更简单。。。

pip的安装方法:
$ sudo easy_install pip
然后安装ansible

$ sudo pip install ansible

Collecting ansible
  Downloading ansible-2.4.1.0.tar.gz (6.7MB)
    100% |████████████████████████████████| 6.7MB 15kB/s 
Collecting jinja2 (from ansible)
  Downloading Jinja2-2.9.6-py2.py3-none-any.whl (340kB)
    100% |████████████████████████████████| 348kB 13kB/s 
Collecting PyYAML (from ansible)
  Downloading PyYAML-3.12.tar.gz (253kB)
    100% |████████████████████████████████| 256kB 17kB/s 
Collecting paramiko (from ansible)
  Downloading paramiko-2.3.1-py2.py3-none-any.whl (182kB)
    100% |████████████████████████████████| 184kB 20kB/s 
Collecting cryptography (from ansible)
  Downloading cryptography-2.1.2-cp27-cp27m-macosx_10_6_intel.whl (1.5MB)
    100% |████████████████████████████████| 1.5MB 5.7kB/s 
Requirement already satisfied: setuptools in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from ansible)
Collecting MarkupSafe>=0.23 (from jinja2->ansible)
  Downloading MarkupSafe-1.0.tar.gz
Collecting pynacl>=1.0.1 (from paramiko->ansible)
  Downloading PyNaCl-1.1.2-cp27-cp27m-macosx_10_6_intel.whl (178kB)
    100% |████████████████████████████████| 184kB 3.8kB/s 
Collecting bcrypt>=3.1.3 (from paramiko->ansible)
  Downloading bcrypt-3.1.4-cp27-cp27m-macosx_10_6_intel.whl (51kB)
    100% |████████████████████████████████| 61kB 4.5kB/s 
Collecting pyasn1>=0.1.7 (from paramiko->ansible)
  Downloading pyasn1-0.3.7-py2.py3-none-any.whl (63kB)
    100% |████████████████████████████████| 71kB 5.2kB/s 
Collecting idna>=2.1 (from cryptography->ansible)
  Downloading idna-2.6-py2.py3-none-any.whl (56kB)
    100% |████████████████████████████████| 61kB 9.3kB/s 
Collecting cffi>=1.7; platform_python_implementation != "PyPy" (from cryptography->ansible)
  Downloading cffi-1.11.2-cp27-cp27m-macosx_10_6_intel.whl (238kB)
    100% |████████████████████████████████| 245kB 2.3kB/s 
Collecting enum34; python_version < "3" (from cryptography->ansible)
  Downloading enum34-1.1.6-py2-none-any.whl
Requirement already satisfied: six>=1.4.1 in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from cryptography->ansible)
Collecting asn1crypto>=0.21.0 (from cryptography->ansible)
  Downloading asn1crypto-0.23.0-py2.py3-none-any.whl (99kB)
    100% |████████████████████████████████| 102kB 1.4kB/s 
Collecting ipaddress; python_version < "3" (from cryptography->ansible)
  Downloading ipaddress-1.0.18-py2-none-any.whl
Collecting pycparser (from cffi>=1.7; platform_python_implementation != "PyPy"->cryptography->ansible)
  Downloading pycparser-2.18.tar.gz (245kB)
    100% |████████████████████████████████| 256kB 2.3kB/s 
Installing collected packages: MarkupSafe, jinja2, PyYAML, pycparser, cffi, pynacl, idna, enum34, asn1crypto, ipaddress, cryptography, bcrypt, pyasn1, paramiko, ansible
  Running setup.py install for MarkupSafe ... done
  Running setup.py install for PyYAML ... done
  Running setup.py install for pycparser ... done
  Running setup.py install for ansible ... done
Successfully installed MarkupSafe-1.0 PyYAML-3.12 ansible-2.4.1.0 asn1crypto-0.23.0 bcrypt-3.1.4 cffi-1.11.2 cryptography-2.1.2 enum34-1.1.6 idna-2.6 ipaddress-1.0.18 jinja2-2.9.6 paramiko-2.3.1 pyasn1-0.3.7 pycparser-2.18 pynacl-1.1.2

自动下载并安装最新版本的ansible,2.4.1.0。
而后直接在命令行中写入ansible就可以看到帮助了。

$ ansible

Usage: ansible <host-pattern> [options]

Define and run a single task 'playbook' against a set of hosts

Options:
  -a MODULE_ARGS, --args=MODULE_ARGS
                        module arguments
  --ask-vault-pass      ask for vault password
Usage: ansible <host-pattern> [options]

Define and run a single task 'playbook' against a set of hosts

Options:
  -a MODULE_ARGS, --args=MODULE_ARGS
                        module arguments
  --ask-vault-pass      ask for vault password
……

第一条命令

  1. 配置inventory,在/etc/ansible/hosts中配置上
127.0.0.1 ansible_user=root

然后执行
$ ansible all -m ping

CASE 1

127.0.0.1 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: ssh: connect to host 127.0.0.1 port 22: Connection refused\r\n", 
    "unreachable": true
}

看起来是22端口不同,直接telnet也显示拒绝,那应该是mac的限制,查了下发现需要开启mac的22端口。
开启方法:系统偏好设置——共享——远程登录勾选。
顺便我把用户换成了root用户,避免低权限用户环境变量的问题。

CASE 2

检查端口开了以后,再次执行,发现还有报错

The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:6YbD5pl+7sP9ZqUPXQFh3gyB71U/XZosWrSUWbX2aRk.
Are you sure you want to continue connecting (yes/no)? yes
127.0.0.1 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: Warning: Permanently added '127.0.0.1' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,keyboard-interactive).\r\n", 
    "unreachable": true
}

看起来是密钥问题,但是其实在我们的环境下我们需要用用户名+密码的方式登录,所以重新配置一下hosts文件

$ vi /etc/ansible/hosts
127.0.0.1 ansible_user=root ansible_ssh_pass=sssmmm

再一次执行会出现如下的提示:

127.0.0.1 | FAILED! => {
    "failed": true, 
    "msg": "to use the 'ssh' connection type with passwords, you must install the sshpass program"
}

原来是需要sshpass,按照这篇攻略安装一下sshpass,然后再次执行:

127.0.0.1 | SUCCESS => {
    "changed": false, 
    "failed": false, 
    "ping": "pong"
}

这总算成功了。简单执行一个“echo hello”的命令:

$ ansible all -a "echo hello"
127.0.0.1 | SUCCESS | rc=0 >>
hello

而后我重启了个terminal,用低权限用户试了试

$ ansible all -a "/bin/echo a"
127.0.0.1 | FAILED | rc=-1 >>
Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host.

切成root以后又没问题= =。。。。还没太弄明白

相关文章

  • Mac下Ansible初探

    安装环境 ansible需求python2.6以上,正好之前研究python的时候安装了2.7,省去了一个步骤。a...

  • React Native初探(三)- Mac

    在React Native初探(一) - Mac和React Native初探(二)- Mac中,很简陋但是能用的...

  • 【Ansible学习】- Ansible初探

    安装 Ansible基础 Ansible架构图 Ansible核心组件说明 Ansible:Ansible的核心程...

  • Mac 下初探nginx

    首先,nginx是什么? 然后,nginx的作用? Mac下如何使用nginx? 1.打开终端执行 安装好后的主要...

  • 1、OpenGL初探之Mac下环境搭建

    OpenGL初探Mac下环境搭建 一、需要准备的文件资源需要准备的资源有 libGLTool.a GLTools ...

  • DevOps 之 Ansible入门实践(一)

    本文将介绍在Ansible 以及 Galaxy 的基本使用。 本文默认操作环境为 Mac. 安装Ansible[^...

  • React Native初探(二)- Mac

    在React Native初探(一)- Mac中,我们运行起来了Welcome App,简单的走了下iOS的App...

  • Ansible 2 -- 2 安装与配置

    ansible 的安装有很多方式,这里以Mac为例 安装 brew install ansible他会顺便安装依赖...

  • mac下ansible安装配置

    最近抽时间了解了一下linux一些简单的运维工具,看到网上推荐ansible简单容易上手,学习一下。 简介 Ans...

  • Mac下Android反编译初探

    工作第四天,被要求学习逆向开发方面的知识,于是先将自己之前写的未经混淆的apk反编译,记录之。 感谢该博文提供的思...

网友评论

    本文标题:Mac下Ansible初探

    本文链接:https://www.haomeiwen.com/subject/dchspxtx.html