ansible 是一款轻量级自动化运维工具,由的 Python 语言开发,结合了多种自动化运维工具的特性,实现了批量系统配置,批量程序部署,批量命令执行等功能; ansible 是基于模块化实现批量操作的。

一、安装

控制机器

pip install ansible==2.5.5

yum install sshpass

受控机器

yum install libselinux-python

yum install python2-simplejson

version<python2.4

测试

echo 127.0.0.1>host

ansible all -m ping -i hosts --ask -pass

二、管理协议

Ansible 通过 ssh 协议对受控机器管理,可使用口令和秘钥对两种方式进行权限验证,默认使用密钥对方式。

秘钥对

1.在控制机器生成秘钥对

ssh -keygen -t rsa -b 4096 -C*kk

2.添加公钥到受控机器

  • 拷贝添加:ssh-copy-id -i ~/.ssh/id_rsa.pub user@host
  • 本地添加:cat ~/.ssh/id_rsa.pub>>~/.ssh/authorized_keys

3.测试

ssh user@host

ansible all -m ping -i hosts

三、配置

inventory

1.ansible 管理主机信息的配置

2.配置文件格式

  • ini
  • yaml

3.配置文件路径

  • 通过命令行参数制定:ansible -i
  • 通过环境变量制定:export ANSIBLE_INVENTORY
  • 默认配置路径:/ect/ansible/hosts

4.配置内容

4.1基本配置

host_v1.ini

127.0.0.1
ip

host_v1.yaml

---
all:
hosts:
 127.0.0.1:
 ip:

测试

ansible all -m ping -ihosts -i host_v1.ini

ansible all -m ping -i hosts -i host_v1.yaml

ansible 127.0.0.1-m ping -ihosts -i host_v1.ini

ansible ip -m ping -ihosts -i host_v1.yaml

主机参数配置

1.参数项

alias 主机别名

ansible_connection

默认 smart

可选值:local、smart、ssh、paramiko

ansilbe_host 登录主机地址

ansilbe_port 默认 22

ansilbe_user 登录主机用户名

ansible_become

是否启用 sudo 权限

默认: false

可选值 :true、false

ansible_become_pass

登录主机用户密码,用于切换 sudo 权限

建议使用 ansible 命令行参数ask_become_pass 替换

ansible_become_user

切换 sudo 后 执行进程中使用的用户名

ansible_ssh_pass

登录主机使用密码

建议使用 ansible 命令行参数ask_pass 替换

ansible_ssh_private_key_file

登录主机使用私钥

ansible_python_interpreter

受控机器执行 Python 解释器

默认 /bin/env/python

hosts_v2.ini

localhost ansible_connect=local
mystest ansible_connect=smart
ansible_host="ip"
ansible_port=
ansible_user="silence"
ansible_become_user="root"
ansible_python_interpreter="/bin/env python2.6"

hosts_v2.yaml

---
all:
 hosts:
   localhost:
     ansible_connect: local
   mytest:
     ansible_connect: smart
     ansible_host: ip
     ansible_port:
     ansible_user: silence
     ansible_become_user: root
     ansible_python_interpreter: "/bin/env python2.6"

组&组变量

  • 可对主机进行分组并命名,批量对主机进行操作
  • 一个主机可属于多个组

host_v3.ini

localhost ansible_connect=local

[webserver]
mytest ansible_host="ip" ansible_user="silence"

[webserver:vars]
ansible_connect=smart
ansible_port=
ansible_become_user="root"
ansible_python_interpreter="/bin/env python2.6"

host_v3.yaml

---
all:
 hosts:
   localhost:
     ansible_connect: local
 children:
     webserver:
         hosts:
           mytest:
             ansible_host: ip
             ansible_user: silence
         vars:
           ansible_connect: smart
           ansible_port:
           ansible_become_user: root
           ansible_python_interpreter: "/bin/env python2.6"

测试

ansible ip -m ping -ihosts -i host_v3.yaml

ansible webserver -m command -a 'sleep 30' -ihost_v3.ini --become --ask-become-pass

组中组

host_v4.ini

localhost ansible_connect=local

[webserver]
mytest ansible_host="ip" ansible_user="silence"

[webserver:vars]
ansible_connect=smart
ansible_port=
ansible_become_user="root"
ansible_python_interpreter="/bin/env python2.6"

[test:children]
webserver

host_v4.yaml

---
all:
 hosts:
   localhost:
     ansible_connect: local
 children:
     webserver:
         hosts:
            mytest:
             ansible_host: ip
             ansible_user: silence
         vars:
           ansible_connect: smart
           ansible_port:
           ansible_become_user: root
           ansible_python_interpreter: "/bin/env python2.6"
     test:
       children:
         webserver:

测试

ansible test --list hosts -i host_v4.yaml

ansible test -m ping -ihosts -i host_v4.yaml

配置分割

  • 在 hosts 文件中值配置主机分组信息,主机配置与组配置分别存储在 host_vars 和 group_vars 目录
  • 主机配置存储在 host_vars 目录中,文件名使用别名.yaml
  • 组配置存储在 group_vars 目录中,文件名使用组名.yaml

host_v5.ini

localhost

[webserver]
mytest

[test:children]
webserver

host_v5.yaml

---
all:
hosts:
 localhost:
children:
  webserver:
    hosts:
     mytestm:
  test:
   children:
    webserver:

host_vars

host_vars/localhost.yaml

---
ansible_connect: local

host_vars/mytest.yaml

---
ansible_host: ip
ansible_user:silence

group_vars

group_vars/webserver.yaml

---
ansible_connect:smart
ansible_port:22
ansible_become_user: root
ansible_python_interpreter:"/bin/env python2.6"

测试

ansible test -m ping -i host_v5.yaml

ansible test -m setup -i host_v5.yaml

ansible test -m command -a 'sleep 30' -ihost_v5.ini --become --ask-become-pass

动态 inventory

文件 inventory.py脚本内容

#!/bin/env python3
#encoding: utf-

inventory = {
   '_meta' : {
       'hostvars' : {
           'localhost' : {
               'ansible_connect' : 'local',
           },
           '51reboot' : {
               'ansible_host' : '112.74.164.107',
               'ansible_user' : 'silence',
           }
       }
   },
   'all' : {
       'hosts' : [
           'localhost'
       ]
   },
   'webserver' : {
       'hosts' : [
           '51reboot'
       ],
       'vars' : {
           'ansible_connect' : 'smart',
           ,
           'ansible_become_user' : 'root',
           'ansible_python_interpreter' : '/bin/env python2.6'
       }
   }
}

if __name__ == '__main__':
   import json, sys
   print(json.dumps(inventory))
   sys.exit()

初始化权限

xhmod +x inventory.py

测试

ansible all --list -hosts -i inventory.py

ansible all -m ping -i inventory.py

ansible.cfg

1.配置文件路径

  • export ANSIBLE_CONFIG=~/ansible.cfg
  • ansible.cfg
  • ~/.ansible.cfg
  • /etc/ansible/ansible.cfg

2.默认配置

3.配置项

host_key_checking

  • 是否检查控制密钥存在于 know_hosts 列表
  • 默认值 :true
  • 可选值:true、false

未完待续......

作者:KK

链接:http://t.cn/RrAjAte

51Reboot 第19期Python实战班、第8期自动化运维班正在招生中

详情咨询QQ Ada:279312229

 

Ansible详解(一)基础安装和配置的更多相关文章

  1. Linux详解(基础、环境配置、项目部署入门)

    Linux(CentOS 7)操作系统 消息队列(Kafka.RabbitMQ.RocketMQ),缓存(Redis),搜索引擎(ES),集群分布式(需要购买多台服务器,如果没有服务器我们就只能使用虚 ...

  2. 【java】详解JDK的安装和配置

    目录结构: contents structure [+] 什么是JDK JDK的三个版本 JDK包含的主要内容 JDK的安装 JDK的配置 配置JAVA_HOME 配置PATH 到底自己需不需要配置C ...

  3. 详解consul的安装和配置

    Consul 简化了分布式环境中的服务的注册和发现流程,通过 HTTP 或者 DNS 接口发现.支持外部 SaaS 提供者等. consul提供的一些关键特性: service discovery:c ...

  4. Linux dts 设备树详解(一) 基础知识

    Linux dts 设备树详解(一) 基础知识 Linux dts 设备树详解(二) 动手编写设备树dts 文章目录 1 前言 2 概念 2.1 什么是设备树 dts(device tree)? 2. ...

  5. 详解Springboot中自定义SpringMVC配置

    详解Springboot中自定义SpringMVC配置 WebMvcConfigurer接口 ​ 这个接口可以自定义拦截器,例如跨域设置.类型转化器等等.可以说此接口为开发者提前想到了很多拦截层面的需 ...

  6. 自动化运维工具——ansible详解(一)

    ansible 简介 ansible 是什么? ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.chef.func.fabric)的优点,实现了批量系统 ...

  7. Ansible 详解

    原文:https://www.cnblogs.com/keerya/p/7987886.html#_label0,有改动 一.Ansible简介 1.ansible是什么 a.ansible是新出现的 ...

  8. linux命名详解及其软件安装实例

    始于cd,ls命令 好啦,步入正题,我使用的linux连接工具为xshell,mRemoteNG,对两款工具不做介绍啦,你可以百度一下,实在不会入左上方群. 进入之后,便是上面的界面黑乎乎一片,对于初 ...

  9. SpringBoot系列教程JPA之query使用姿势详解之基础篇

    前面的几篇文章分别介绍了CURD中的增删改,接下来进入最最常见的查询篇,看一下使用jpa进行db的记录查询时,可以怎么玩 本篇将介绍一些基础的查询使用姿势,主要包括根据字段查询,and/or/in/l ...

  10. NHibernate实战详解(二)映射配置与应用

    关于NHibernate的资料本身就不多,中文的就更少了,好在有一些翻译文章含金量很高,另外NHibernate与Hibernate的使用方式可谓神似,所以也有不少经验可以去参考Hibernate. ...

随机推荐

  1. 鸟哥私房菜vim常用命令

    第一部份:一般模式可用的按钮说明,光标移动.复制贴上.搜寻取代等 移动光标的方法 h 或 向左箭头键(←) 光标向左移动一个字符 j 或 向下箭头键(↓) 光标向下移动一个字符 k 或 向上箭头键(↑ ...

  2. 我在德国做SAP CRM One Order redesign工作的心得

    时间过得很快,今天是我到德国工作的第四周,刚好一个月.Prototype的框架已经搭起来了,现在Order能够在新的框架下正常读写,能跑一些简单的scenario,这些scenario对于end us ...

  3. ui-sref

    angularjs中路由跳转可以在模板页面上使用ui-sref="a-state({param1: value})"; 如果想为当前state的导航按钮添加一个激活class,可以 ...

  4. 2.3 Python语言基础

    2.3 Python语言基础 1 语言语义(Language Semantics) 缩进,而不是括号 Python使用空格(tabs or spaces)来组织代码结构,而不是像R,C++,Java那 ...

  5. Guava包学习--Hash

    我们HashMap会有一个rehash的过程,为什么呢?因为java内建的散列码被限制为32位,而且没有分离散列算法和所作用的数据,所以替代算法比较难做.我们使用HashMap的时候它自身有一个reh ...

  6. 3282. Tree【LCT】

    Description 给定N个点以及每个点的权值,要你处理接下来的M个操作. 操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和 ...

  7. PHP运行模式简单总结

    众所周知,PHP有多种运行模式,那么这些模式各自有什么特点,它们之间又有什么区别呢,本文将作一个简单的总结: CGI 模式 所谓 CGI (Common Gateway Interface) 是指通用 ...

  8. jQuery文字“橡皮圈“特效

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. 理解JavaScript继承(三)

    理解JavaScript继承(三) 通过把父对象的属性,全部拷贝给子对象,也能实现继承. 7.浅拷贝 function extendCopy(p) { var o = {}; for (var pro ...

  10. python获取网站http://www.weather.com.cn 城市 8-15天天气

    参考一个前辈的代码,修改了一个案例开始学习beautifulsoup做爬虫获取天气信息,前辈获取的是7日内天气, 我看旁边还有8-15日就模仿修改了下.其实其他都没有变化,只变换了获取标签的部分.但是 ...