首页 > 其他 > 详细

Ansible安装并带您入门

时间:2020-03-09 20:21:46      阅读:51      评论:0      收藏:0      [点我收藏+]

一、简介

       Puppet, SaltStack, Chef, Ansible都是服务器批量运维常用工具,其中Ansible最大的亮点在于”无客户端、简单易用和日志集中控管。”,不用帮每台机器 (instance) 预载 agent,只要有 SSH 和 Python 就可以。,本文介绍一下Ansible的基本使用。

 

二、结构

技术分享图片

 

  1、当 Control Machine (主控端) 可以用 SSH 连上 Managed node,且被连上的机器里有预载 Python 时,Ansible 就可以运作了!

  2、Managed node 则是被 Ansible 操纵的机器。

 

三、在Control Machine中安装Ansible

Ubuntu (Apt)

    安装 add-apt-repository 必要套件。
    $ sudo apt-get install -y python-software-properties software-properties-common

    使用 Ansible 官方的 PPA 套件来源。
    $ sudo add-apt-repository -y ppa:ansible/ansible; sudo apt-get update

    安装 Ansible。
    $ sudo apt-get install -y ansible


CentOS (Yum)

    新增 epel-release 第三方套件来源。
    $ sudo yum install -y epel-release

    安装 Ansible。
    $ sudo yum install -y ansible


macOS (Homebrew)

    请先安装 homebrew,已安装者请略过。
    $ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

    安装 Ansible。
    $ brew install ansible

Python (Pip)
    Ansible 近来的释出速度很快,若想追求较新的版本可改用 Pip 的方式进行安装,较不建议初学者使用。
    需请先安装 pip,已安装者请略过。
    # Debian, Ubuntu
    $ sudo apt-get install -y python-pip


    # CentOS
    $ sudo yum install -y python-pip
    
    # macOS
    $ sudo easy_install pip
    
    升级 pip。
    $ sudo pip install -U pip
    
    安装 Ansible。
    $ sudo pip install ansible

 

 

四、在Managed node 中安装Python和SSH(一盘系统都自带了,因此可忽略此步骤)

Ubuntu.
$ sudo apt-get install -y openssh-server python2.7

CentOS.
$ sudo yum install -y openssh-server python

 

五、修改配置

  1、安装好Ansible后,我们可以在/etc/ansible目录下看到配置文件。修改:ansible.cfg,例如:

inventory = hosts 
remote_user = root 
host_key_checking=False

   2、修改/etc/ansible/hosts文件,参考下图,可以使用如下变量:ansible_ssh_host、ansible_ssh_port、ansible_ssh_user、ansible_ssh_pass、ansible_ssh_private_key_file。

  技术分享图片

  例如我测试使用的配置:

  技术分享图片

 

六、测试执行命令

  1、执行:

 

$ ansible localhost -m command -a echo Hello World.
localhost | SUCCESS | rc=0 >>
Hello World.

 

 

六、测试playbook

  1、在/etc/ansible目录下,执行:vim ./helloworld.yml 内容如下:

---

- name: say ‘hello world‘
  hosts: huawei
  tasks:

    - name: echo ‘hello world‘
      command: echo ‘hello world‘
      register: result

    - name: print stdout
      debug: var=result
                            

  2、执行:ansible-playbook ./helloworld.yml

  技术分享图片

 

Ansible安装并带您入门

原文:https://www.cnblogs.com/songxingzhu/p/12450275.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!