自动化运维利器之 ansible 入门

阅读本文大概需要 8 分钟。

ansible 简介

ansible 是一款强大的配置管理工具,诣在帮助系统管理员高效率地管理成百上千台主机。设想一个主机是一个士兵,那么有了 ansible ,作为系统管理员的你就是一个将领,你可以通过口头命令,即一次下发一条命令(ansible ad-hoc 模式)方式让一个或一组或全部的士兵按你的指令行事,也可以将多条命令写在纸上(ansible playbook 模式), 需要执行命令时只需要提供这张纸即可。你可以让多个士兵同时做相同或不同的事情,可以方便的让新加入的士兵快速加入已有的兵种队伍,也以快速改变兵种(配置管理),一句话,士兵都严格听你的,你做好命令的设计,ansible 自动帮你发布和执行。

我们只需要在一台机器(类 unix 系统)上安装 ansible,即可在这台机器上管理其他主机,ansible 使用 ssh 协议与被管理的主机通讯,只要能 ssh 连接这些主机,ansible 便可以控制他们。

ansible 安装

ansible 的安装轻而易举,安装方法如下:

  1. 使用 pip 安装

pip 是 python 的包管理工具,使用起来非常方便,只要操作系统安装有 pip,直接 pip install 包名即可,安装 ansible 的方法如下:

pip install ansible 
  1. 使用 apt-get 安装

在基于 Debian/Ubuntu Linux 的系统中可使用 apt-get 安装 ansible

sudo apt-get install software-properties-common
sudo apt-add-repository ppa:ansible/ansible
sudo apt-get update
sudo apt-get install ansible
  1. 使用 yum 安装
    在基于 RHEL/CentOS Linux 的系统中可使用 yum 安装 ansible
sudo yum install ansible
  1. 使用源码安装
    可以从 github 上安装最新版本
cd ~
git clone git://github.com/ansible/ansible.git
cd ./ansible
source ./hacking/env-setup

ansible 配置文件

ansible 的配置文件有多个位置,查找顺序如下:

  1. 环境变量 ANSIBLE_CONFIG 所指向的位置
  2. 当前目录下的 ansible.cfg
  3. HOME 目录下的配置文件 ~/.ansible.cfg
  4. /etc/ansible/ansible.cfg

在大多数场景下默认的配置就能满足大多数用户的需求,在一些特殊场景下,用户还是需要自行修改这些配置文件,
如果安装后没有在以上 3 个位置找到配置文件的话,自己在 HOME 目录新建一个 .ansible.cfg 即可。

ansible 常见的配置参数如下所示:

inventory = ~/ansible_hosts      #这个参数表示主机清单 inventory 文件的位置
forks = 5        #并发连接数,默认为5
sudo_user = root        #设置默认执行命令的用户
remote_port = 22        #指定连接被管节点的管理端口,默认为22端口,建议修改,能够更加安全
host_key_checking = False #设置是否检查SSH主机的密钥,值为True/False。关闭后第一次连接不会提示配置实例
timeout = 60          #设置SSH连接的超时时间,单位为秒
log_path = /var/log/ansible.log     #指定一个存储ansible日志的文件(默认不记录日志)

更为详细的配置参数详见

ansible 详细配置文件

Inventory文件

Ansible 可同时操作属于一个组的多台主机,组和主机之间的关系通过 inventory 文件配置. 默认的文件路径为 /etc/ansible/hosts,我们也可以通过 ansible 的配置文件来指定 inventory 文件位置。
除默认文件外,你还可以同时使用多个 inventory 文件,也可以从动态源,或云上拉取 inventory 配置信息。

一个简单的 Inventory文件示例

192.168.0.111

也可以对主机进行分组

mail.example.com

[webservers]
foo.example.com
bar.example.com

[dbservers]
one.example.com
two.example.com
three.example.com

方括号[]中是组名,用于对系统进行分类,便于对不同系统进行个别的管理。一个系统可以属于不同的组,比如一台服务器可以同时属于 webserver 组和 dbserver 组。这时属于两个组的变量都可以为这台主机所用。

主机变量
前面已经提到过,分配变量给主机很容易做到,这些变量定义后可在 playbooks 中使用:

[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909

组的变量
也可以定义属于整个组的变量:

[atlanta]
host1
host2

[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com

把一个组作为另一个组的子成员
可以把一个组作为另一个组的子成员,以及分配变量给整个组使用. 这些变量可以给 /usr/bin/ansible-playbook 使用,但不能给 /usr/bin/ansible 使用:

[atlanta]
host1
host2

[raleigh]
host2
host3

[southeast:children]
atlanta
raleigh

[southeast:vars]
some_server=foo.southeast.example.com
halon_system_timeout=30
self_destruct_countdown=60
escape_pods=2

[usa:children]
southeast
northeast
southwest
northwest

对于每一个 host,你还可以选择连接类型和连接用户名:

[targets]

localhost              ansible_connection=local
other1.example.com     ansible_connection=ssh        ansible_ssh_user=mpdehaan
other2.example.com     ansible_connection=ssh        ansible_ssh_user=mdehaan

Inventory 参数的说明

如同前面提到的,通过设置下面的参数,可以控制 ansible 与远程主机的交互方式,如下:

ansible_ssh_host
      将要连接的远程主机名.与你想要设定的主机的别名不同的话,可通过此变量设置.

ansible_ssh_port
      ssh端口号.如果不是默认的端口号,通过此变量设置.

ansible_ssh_user
      默认的 ssh 用户名

ansible_ssh_pass
      ssh 密码(这种方式并不安全,我们强烈建议使用 --ask-pass 或 SSH 密钥)

ansible_sudo_pass
      sudo 密码(这种方式并不安全,我们强烈建议使用 --ask-sudo-pass)

ansible_sudo_exe (new in version 1.8)
      sudo 命令路径(适用于1.8及以上版本)

ansible_connection
      与主机的连接类型.比如:local, ssh 或者 paramiko. Ansible 1.2 以前默认使用 paramiko.1.2 以后默认使用 'smart','smart' 方式会根据是否支持 ControlPersist, 来判断'ssh' 方式是否可行.

ansible_ssh_private_key_file
      ssh 使用的私钥文件.适用于有多个密钥,而你不想使用 SSH 代理的情况.

ansible_shell_type
      目标系统的shell类型.默认情况下,命令的执行使用 'sh' 语法,可设置为 'csh''fish'.

ansible_python_interpreter
      目标主机的 python 路径.适用于的情况: 系统中有多个 Python, 或者命令路径不是"/usr/bin/python",比如  \*BSD, 或者 /usr/bin/python
      不是 2.X 版本的 Python.我们不使用 "/usr/bin/env" 机制,因为这要求远程用户的路径设置正确,且要求 "python" 可执行程序名不可为 python以外的名字(实际有可能名为python26).

      与 ansible_python_interpreter 的工作方式相同,可设定如 ruby 或 perl 的路径....

一个主机文件的例子

some_host         ansible_ssh_port=2222     ansible_ssh_user=manager
aws_host          ansible_ssh_private_key_file=/home/example/.ssh/aws.pem
freebsd_host      ansible_python_interpreter=/usr/local/bin/python
ruby_module_host  ansible_ruby_interpreter=/usr/bin/ruby.1.9.3

执行 ansible 命令(ad-hoc 命令)

接下来我们展示如何 ansible 命令,配置文件如下所示:

(py37env) aaron
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值