Ansible-安装 (Centos7 通过Yum安装最新发布版本)

来源:互联网 发布:有经商软件吗 编辑:程序博客网 时间:2024/06/05 17:16

Ansible-安装 (Centos7 通过Yum安装最新发布版本)

1.环境准备

操作系统:Contos7Python版本: Python 2.6Python 2.7

2.设置EPEL仓库

通过Yum安装RPMs适用于 EPEL 6, 7。Ansible仓库默认不在yum仓库中,因此我们需要使用下面的命令启用epel仓库

rpm -iUvh http://dl.Fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm

3.使用yum安装Ansible

安装 : yum install ansible

[root@localhost myron01]# yum install ansibleLoaded plugins: fastestmirror, langpacksRepository base is listed more than once in the configurationRepository updates is listed more than once in the configuration..............Installed:  ansible.noarch 0:2.2.1.0-1.el7                                                Dependency Installed:  PyYAML.x86_64 0:3.10-11.el7             libtomcrypt.x86_64 0:1.17-23.el7       libtommath.x86_64 0:0.42.0-4.el7        libyaml.x86_64 0:0.1.4-11.el7_0        python-babel.noarch 0:0.9.6-8.el7       python-httplib2.noarch 0:0.7.7-3.el7   python-jinja2.noarch 0:2.7.2-2.el7      python-keyczar.noarch 0:0.71c-2.el7    python-markupsafe.x86_64 0:0.11-10.el7  python-six.noarch 0:1.9.0-2.el7        python2-crypto.x86_64 0:2.6.1-13.el7    python2-ecdsa.noarch 0:0.13-4.el7      python2-paramiko.noarch 0:1.16.1-2.el7  python2-pyasn1.noarch 0:0.1.9-7.el7    sshpass.x86_64 0:1.06-1.el7            Complete![root@localhost myron01]# 

版本检查:ansible –version(两个-)

[root@localhost myron01]# ansible --versionansible 2.2.1.0  config file = /etc/ansible/ansible.cfg  configured module search path = Default w/o overrides

4.设置用于节点鉴权的SSH密钥

进入~/.ssh生成秘钥 ssh-keygen

[root@localhost ansible]# cd ~/.ssh[root@localhost .ssh]# ssh-keygenGenerating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa.Your public key has been saved in /root/.ssh/id_rsa.pub.The key fingerprint is:5b:ae:1e:90:b9:85:90:f2:0a:c1:ef:e7:88:23:f5:a6 root@localhost.localdomainThe key's randomart image is:+--[ RSA 2048]----+|                 ||.    .           ||... o            || ..o . +         ||.  .. = S .      || .o.   + +       || ..o .. o .      ||... *    o       ||..E+ . .o        |+-----------------+[root@localhost .ssh]# 

使用ssh-copy-id命令来复制Ansible公钥到节点中

[root@localhost .ssh]# ssh-copy-id myron@192.168.150.136The authenticity of host '192.168.150.136 (192.168.150.136)' can't be established.ECDSA key fingerprint is f6:c2:20:dc:ec:28:71:4a:fe:4d:d9:5d:39:39:65:8f.Are you sure you want to continue connecting (yes/no)? yes/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keysmyron@192.168.150.136's password: Number of key(s) added: 1Now try logging into the machine, with:   "ssh 'myron@192.168.150.136'"and check to make sure that only the key(s) you wanted were added.[root@localhost .ssh]#

测试免密操作 ansible all -m ping -u myron

[root@localhost .ssh]# ansible all -m ping -u myron192.168.150.136 | SUCCESS => {    "changed": false,     "ping": "pong"}[root@localhost .ssh]# 

5.基本功能

Ansible提供两种方式去完成任务,ad-hoc 命令和写 Ansible playbook

  1. ad-hoc 命令—可以解决一些简单的任务
    ad-hoc 命令是什么?

    ad-hoc这其实是一个概念性的名字,是相对于写 Ansible playbook 来说的.类似于在命令行敲入shell命令和 写shell scripts两者之间的关系

    示例:查看托管节点的主机名

    [root@localhost ansible]# ansible 192.168.150.138 -m command -a "hostname" -u myron192.168.150.138 | SUCCESS | rc=0 >>localhost.localdomain[root@localhost ansible]#

    示例:查看托管节点主机的Java版本

    [root@localhost ansible]# ansible 192.168.150.138 -a "java -version" -u myron192.168.150.138 | SUCCESS | rc=0 >>java version "1.7.0_51"OpenJDK Runtime Environment (rhel-2.4.5.5.el7-x86_64 u51-b31)OpenJDK 64-Bit Server VM (build 24.51-b03, mixed mode)[root@localhost ansible]# 

    说明:
    192.168.150.138 是管理主机配置的托管节点 /etc/ansible/hosts 中配置
    -m 模块名 用户执行对应的功能 (默认:command) 所以执行ansible 192.168.150.138 -a “hostname” -u myron 效果一样

    Ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。

  2. 写 Ansible playbook—后者解决较复杂的任务.
    Playbooks是什么?

    Playbooks 是 Ansible的配置,部署,编排语言.他们可以被描述为一个需要希望远程主机执行命令的方案,或者一组IT程序运行的命令集合.

//TODO ansible playbook 用例

6.参考文档或博客

  1. 自动化运维工具Ansible详细部署:http://sofar.blog.51cto.com/353572/1579894

0 0