关于ansible:ansible使用笔记一ansible安装及常用配置说明

一、ansible介绍

ansible是2013年推出的一款IT自动化和DevOps软件,2015年被RedHat收买。是基于Python研发,糅合很多老运维工具的长处,实现了批量操作系统配置,批量程序部署,批量运行命令等性能

ansible能够实现

  • 自动化部署APP
  • 自动化治理配置项
  • 自动化继续交付
  • 自动化(AWS)云服务治理

ansible长处

  • 主从工作模式 只须要SSH和Python即可应用,无客户端
  • ansible功能强大,反对自定义模块 模块丰盛 反对playbook
  • 上手容易,门槛低
  • 基于Python开发,做二次开发更容易
  • 应用公司比拟多,社区沉闷

ansible个性

  • 模块化设计,调用特定的模块实现特定工作
  • 基于Python语言实现
  • paramiko
  • PyYAML(半结构化语言)
  • Jinja2
  • 其模块反对JSON等规范输入格局,能够采纳任何编程语言重写

治理主机

  • Ansible能够在装置了Python 2(2.7版)或Python 3(3.5版及更高版本)的任何计算机上运行。这包含Red Hat,Debian,CentOS,macOS,任何BSD等。管制节点不反对Windows
  • ansible 应用以下模块,都须要装置
    paramiko
    PyYAML(半结构化语言)
    Jinja2
    httplib2
    six

对于被托管的主机

  • ansible默认通过SSH协定治理机器
  • 被治理主机要开启SSH服务,容许ansible主机登录
    在托管节点上也须要装置Python 2(2.7版)或Python 3(3.5版及更高版本)
    如果托管节点上开启中了SElinux,须要装置libselinux-Python

部署证书文件
ansible 是通过SSH在近程执行命令的,SSH近程执行命令必须通过认证才行,明码写入配置文件安生性很差,个别会应用key形式认证,给所有主机公钥
没有秘钥命令执行会出错

二、ansible装置及罕用配置阐明

环境筹备
6台主机,1台治理主机,5台托管主机,以实现批量程序部署,批量运行命令等性能,具体要求如下表

ansible罕用配置参数阐明

  • ansible配置文件查找程序
  • 首先检测 ANSIBLE_CONFIG变量定义的配置文件
  • 其次查看当前目录下 ./ansible.cfg文件
  • 再次查看以后用记家目录下 ~/ansible.cfg 文件
  • 最初查看/etc/ansible/ansible.cfg文件
  • /etc/ansible/ansible.cfg是ansible默认配置文件门路
  • /etc/ansible/hosts 是ansible默认host文件门路

ansible.cfg配置文件
inventory 定义托管主机地址配置host文件路径名 指定的配置文件,写入近程主机的地址
host_key_checking = False ssh主机key验证配置参数
-如果为False,不须要输出yes
-如果为Ture,期待输出yes
ansible_ssh_prot
ssh端口号:如果不是默认的端口号,通过此变量设置
ansible_ssh_user
默认的ssh用户名
ansible_ssh_pass
ssh明码(这种形式并不平安,强烈建议应用SSH密钥)
ansible_ssh_private_key_file
ssh应用的私钥文件,实用于有多个密钥,而你不想应用SSH代理的状况

ansible 托管主机地址配置host文件
格局

# 示意正文
[组名称]
主机名称或IP地址, 其它参数
- vars变量定义,用于组名前面
例如
[all:vars]    //指定所有组key的寄存地位
ansible_ssh_private_key_file="/root/keyfile/id_dsa"

- children子组定义,用于援用其它组名称
例如
[app:children]    //其中web、db别离为不同分组
web               
db
[root@ansible myansible]# cat myhost 
[app1]
web1
db1
[app2]
web2
db2
[app:children]
app1
app2
[other]
cache
[all:vars]
ansible_ssh_private_key_file="/root/keyfile/id_dsa"
  • ansiblew命令根底
    列出要执行主机
    ansible all –list-hosts
  • 批量检测主机
    ansible all -m ping -k
  • ansible主机汇合 -m 模块名称 -a 模块参数
    主机汇合 主机名或分组名,多个应用”逗号”分隔
    -m 模块名称,默认command模块
    -a or –args模块参数
    其它参数
    -i inventory文件门路,或可执行脚本
    -k 应用交互式登陆密码
    -e 定义变量
    -v 显示详细信息

1 )治理主机 装置EPEL源 EPEL源蕴含下面ansible所须要的所有模块

[root@ansible ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@ansible ~]# yum clean all
[root@ansible ~]# yum repolist

2)装置ansible

[root@ansible ~]# yum -y install ansible              
[root@ansible ~]# ansible --version  //查看ansible版本
ansible 2.9.13

3)生成秘钥 配置免秘登陆托管主机

[root@ansible ~]# vim /etc/ansible/hosts
[web]
web1
web2
[db]
db[1:2]
[other]
cache

[root@ansible ~]# ssh-keygen  //一路回车生成秘钥
[root@ansible .ssh]# ansible all -m ping  //测试 失败
web1 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).", 
    "unreachable": true
}
db1 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).", 
    "unreachable": true
}
......

[root@ansible .ssh]# for i in {40..45};do ssh-copy-id root@192.168.4.1$i; done   //应用for循环给托管主机传送秘钥 免秘登陆
[root@ansible .ssh]# ansible all -m ping  //胜利
db2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
db1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
......

4 )指定秘钥寄存地位

[root@ansible ~]# mkdir keyfile/
[root@ansible ~]# mv .ssh/id_dsa keyfile/  //扭转key的寄存地位
[root@ansible ~]# ansible all -m ping    //失败
web1 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).", 
    "unreachable": true
}
......

[root@ansible ~]# cat /etc/ansible/hosts
[web]
web1
web2
[db]
db[1:2]
[other]
cache
[all:vars]
ansible_ssh_private_key_file="/root/keyfile/id_dsa"   //指定key的寄存地位
[root@ansible ~]# ansible all -m ping   //胜利
db2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
......

[root@ansible ~]# mkdir myansible
[root@ansible ~]# cd myansible/
[root@ansible myansible]# vim myhost
[app1]
web1
db1
[app2]
web2
db2
[app:children]
app1
app2
[other]
cache
[all:vars]
ansible_ssh_private_key_file="/root/keyfile/id_dsa"

[root@ansible myansible]# vim ansible.cfg
[defaults]
inventory = myhost
host_key_checking = False

[root@ansible myansible]# ls
ansible.cfg  myhost

[root@ansible myansible]# ansible app1 -m ping
web1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
db1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

[root@ansible myansible]# ansible app -m ping
db1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
......

5 ) 测试ansible.cfg文件

[root@ansible myansible]# ansible app --list-hosts   //首先查看本目录 下ansible.cfg文件 胜利
  hosts (4):
    web1
    db1
    web2
    db2
[root@ansible myansible]# cd ..
[root@ansible ~]# ansible app --list-hosts    //本目录下无ansible.cfg文件  默认的/etc/ansible/hosts 也没有定义app 组 报错
[WARNING]: Could not match supplied host pattern, ignoring: app
[WARNING]: No hosts matched, nothing to do
  hosts (0):

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理