原创运维基础之Ansible简介安装和使用

Posted barneywill

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了原创运维基础之Ansible简介安装和使用相关的知识,希望对你有一定的参考价值。

技术分享图片

官方:https://www.ansible.com/

一 简介

Ansible is a radically simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs.

It uses no agents and no additional custom security infrastructure, so it‘s easy to deploy - and most importantly, it uses a very simple language (YAML, in the form of Ansible Playbooks) that allow you to describe your automation jobs in a way that approaches plain English.

ansible的好处是快速、简单、易用、不需要安装agent,可以方便的用于配置管理和应用部署等自动化场景;

二 安装

1 安装

yum install -y ansible

2 测试

# ansible --version

ansible 2.7.5

  config file = /etc/ansible/ansible.cfg

  configured module search path = [u‘/root/.ansible/plugins/modules‘, u‘/usr/share/ansible/plugins/modules‘]

  ansible python module location = /usr/lib/python2.7/site-packages/ansible

  executable location = /usr/bin/ansible

  python version = 2.7.5 (default, Jul 13 2018, 13:06:57) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]

3 配置目录结构

# ls /etc/ansible/
ansible.cfg hosts roles

三 使用

首先在/etc/ansible/hosts中根据部署结构将server划分到多个group,然后可以针对某个group执行某些操作;

1 添加server分组配置

vi /etc/ansible/hosts

[all-servers]
192.168.0.54
192.168.0.55
192.168.0.56

以上配置了一个server_group名为all-servers,其中包含3台server;

可以通过--list查看某个server_group下的所有server:

# ansible all-servers --list

  hosts (3):

    192.168.0.54

    192.168.0.55

    192.168.0.56

2 执行操作

格式

ansible $server_group -m $module_name -a $module_args

例如ping:

# ansible all-servers -m ping

192.168.0.54 | SUCCESS => {

    "changed": false,

    "ping": "pong"

}

192.168.0.55 | SUCCESS => {

    "changed": false,

    "ping": "pong"

}

192.168.0.56 | SUCCESS => {

    "changed": false,

    "ping": "pong"

}

例如command:

# ansible all-servers -m command -a "date"
192.168.0.54 | CHANGED | rc=0 >>
Sun Jan 13 17:14:06 CST 2019

192.168.0.56 | CHANGED | rc=0 >>
Sun Jan 13 17:14:06 CST 2019

192.168.0.55 | CHANGED | rc=0 >>
Sun Jan 13 17:14:06 CST 2019

具体的module_name有哪些?可以通过命令查看

# ansible-doc -l

某个module_name的帮助也可以通过命令查看

# ansible-doc $module_name

比如:

# ansible-doc shell

> SHELL    (/usr/lib/python2.7/site-packages/ansible/modules/commands/shell.py)

 

        The `shell‘ module takes the command name followed by a list of space-delimited arguments. It is almost exactly like the [command] module but runs the command

        through a shell (`/bin/sh‘) on the remote node. For Windows targets, use the [win_shell] module instead.

 

# ansible-doc copy

> COPY    (/usr/lib/python2.7/site-packages/ansible/modules/files/copy.py)

 

        The `copy‘ module copies a file from the local or remote machine to a location on the remote machine. Use the [fetch] module to copy files from remote locations to

        the local box. If you need variable interpolation in copied files, use the [template] module. For Windows targets, use the [win_copy] module instead.

 

常用module:ping,command,shell,copy,file,script,cron等...

比如向某个group的所有server拷贝文件命令:

# ansible all-servers -m copy -a ‘src=/tmp/1.log dest=/tmp/‘

另外也可以直接指定ip来操作,比如:

# ansible 192.168.0.55 -m ping

 









以上是关于原创运维基础之Ansible简介安装和使用的主要内容,如果未能解决你的问题,请参考以下文章

原创运维基础之Zabbix简介安装使用

自动化运维三贱客之 Ansible 的安装和命令应用基础

自动化运维之ansible-安装部署与基础命令篇

原创运维基础之OpenResty

运维自动化之ANSIBLE

Linux-运维自动化之Ansible