Ansible安装并带您入门

摘要:
,本文介绍一下Ansible的基本使用。$sudoapt-getinstall-ypython-software-propertiessoftware-properties-common使用Ansible官方的PPA套件来源。$sudoyuminstall-yansiblemacOS请先安装homebrew,已安装者请略过。$brewinstallansiblePythonAnsible近来的释出速度很快,若想追求较新的版本可改用Pip的方式进行安装,较不建议初学者使用。$sudopipinstallansible四、在Managednode中安装Python和SSHUbuntu.$sudoapt-getinstall-yopenssh-serverpython2.7CentOS.$sudoyuminstall-yopenssh-serverpython五、修改配置1、安装好Ansible后,我们可以在/etc/ansible目录下看到配置文件。

一、简介

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

二、结构

Ansible安装并带您入门第1张

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 installansible

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


    # CentOS
    $ sudo yum install -y python-pip
    
    # macOS
    $ sudoeasy_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。

Ansible安装并带您入门第2张

例如我测试使用的配置:

Ansible安装并带您入门第3张

六、测试执行命令

如:执行:

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

Ansible安装并带您入门第4张

七、测试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安装并带您入门第5张

八、模板测试

Ansible安装并带您入门第6张

1、新建template文件,vihelloworld2.txt.j2,内容如下:

Hello "{{ dynamic_world }}"

2、新建playbook文件,vihelloworld2.yml,内容如下:

---
-name: Play the template module
  hosts: all
  vars:
    dynamic_world: "World"
  tasks:
    - name: generation the hello_world.txt filetemplate:
        src: helloworld2.txt.j2
        dest: /tmp/hello_world.txt

    - name: show filecontext
      command: cat /tmp/hello_world.txt
      register: result

    -name: print stdout
      debug:
        msg:

3、执行playbook:

ansible-playbook helloworld2.yml

Ansible安装并带您入门第7张

4、指定环境变量

ansible-playbook helloworld2.yml -e "dynamic_world=SongXingzhu"

Ansible安装并带您入门第8张

免责声明:文章转载自《Ansible安装并带您入门》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇百度富文本编辑器ueditor使用总结css如何设置首行文字缩进?下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

ubuntu环境ceph配置入门(一)

环境:ubuntu server 14.04 64bit,安装ceph版本号0.79 正常情况下应有多个主机,这里为了高速入门以一台主机为例,多台主机配置方式类似。 1. 配置静态IP及主机名 静态IP配置: #vi /etc/network/interfaces 改动eth0配置为 auto eth0 iface eth0 inet static a...

centos 7-8 安装 ms sql server 2019

https://docs.microsoft.com/zh-cn/sql/linux/sql-server-linux-overview?view=sql-server-ver15 ###下载安装 sudo curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com...

配置SecureCRT连接VirtualBox虚拟机中的Linux环境

在实际的运维中我们常常使用SecuriteCRT来远程控制Linux服务器。下面将详细介绍windows 7下通过VirtualBox搭建linux开发环境,并最终通过SecurityCRT来远程访问linux服务器。 主要分为以下几个步骤 1、VirtualBox安装 2、Ubuntu安装 3、SecuriteCRT安装 4、配置Linux网络 一、我使...

树莓派更换阿里云源

我的树莓派使用的jessie系统: 1、打开文件 sudo nano /etc/apt/sources.list 2、编辑文件 将原来的deb.......和deb-src......两行注释掉 或者直接在这两行修改 debhttp://mirrors.aliyun.com/raspbian/raspbian/jessie main contrib non...

CentOS6.5安装Tab增强版:bash-completion

CentOS6.5安装Tab增强版:bash-completion,可补全命令参数; 因为CentOS官方源并不带有bash-completion的包,所以,为了可用yum安装,增加epel的源, 首先,使用wget下载epel的rpm包,使用rpm指令安装,之后运行yum安装,代码如下: [adam@ultraera ~]$ wget http:/...

(转)Ubuntu中启用关闭Networkmanager网络设置问题!

【Server版本】 在UbuntuServer版本中,因为只存有命令行模式,所以要想进行网络参数设置,只能通过修改/etc/network/interfaces。具体设置方法如下: (1) UbuntuServer 修改 IP地址 打开/etc/network/interfaces sudo vim/etc/network/interfaces 加入以...