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

22次阅读

共计 4721 个字符,预计需要花费 12 分钟才能阅读完成。

一、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):

正文完
 0