首页 技术 正文
技术 2022年11月17日
0 收藏 375 点赞 4,228 浏览 4473 个字

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详解(一)基础安装和配置

二、管理协议

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

Ansible详解(一)基础安装和配置

三、配置

inventory

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

2.配置文件格式

  • ini
  • yaml

3.配置文件路径

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

4.配置内容

4.1基本配置

host_v1.ini

127.0.0.1ip

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=localmystest ansible_connect=smartansible_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=smartansible_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=smartansible_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: ipansible_user:silence

group_vars

group_vars/webserver.yaml

---ansible_connect:smartansible_port:22ansible_become_user: rootansible_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

Ansible详解(一)基础安装和配置

未完待续……

作者:KK

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

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

详情咨询QQ Ada:279312229

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,493
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297