78.学习Ansible[1]:安装与配置,78ansible


文章目录

  • 安装
  • 配置Ansible

Ansible是一个功能强大的自动化运维工具。最近工作中需要调研持续部署(CD)的常用工具,本系列博文记录Ansible的学习过程。

安装

安装Ansible后,不会在系统中新安装任何数据库,也不会新启动任何守护进程。运维人员只要在一台机器安装Ansible,就可以凭Ansible主机单点管理非常多的机器。Ansible管理远端机器时,不会在远端机器安装任何软件(agentless),所以更新Ansible版本非常方便(不需要在所有远端机器更新agent软件版本)。
Windows操作系统无法安装Ansible,只能作为被Ansible主机管理的远端机器。如果使用Red Hat企业版Linux(TM)、Cent OS、Fedora、Debian、Ubuntu,推荐使用操作系统的包管理工具安装Ansible,以Ubuntu 16.04系统为例:

$ sudo apt-get update
$ sudo apt-get install software-properties-common #安装add-apt-repository
$ sudo apt-add-repository --yes --update ppa:ansible/ansible #添加ansible/ansible PPA
$ sudo apt-get install ansible

如果使用其他操作系统(例如Mac OS),推荐使用pip安装Ansible:

pip install --user ansible

Ansible主机要求Python版本为Python2.7或Python3(>=3.5)。
如果在Mac系统上遇到文件描述符数量的限制(Too many open files),可以用sudo launchctl limit maxfiles unlimited放开限制。

远端机器依赖:
各个Ansible模块可能有自己独有的依赖,这需要按照各个模块的手册在远端机器提前部署好。其他通用的依赖如下:

  • Python2(>=2.6)或Python3(>=3.5)。
  • 默认使用sftp。如果要使用scp,需要修改主机ansible.cfg中的transfer_method项的配置。
  • 如果远端机器开启SELinux,Ansible主机使用copyfiletemplate等模块前,需要保证远端机器安装了libselinux-python
  • 默认远端机器的Python解释器是/usr/bin/python。如果远端机器上没有/usr/bin/python,要么在远端机器准备/usr/bin/python的环境,要么在Ansible主机的机器列表定义中指定ansible_python_interpreter,例如:
a.example.com ansible_python_interpreter=/home/mars/bin/python2.6
  • 有些Ansible模块无需Python就能运行,比如raw,所以可以用raw模块在远端机器安装Python:ansible a.example.com -m raw -a "yum install -y python2"

配置Ansible

Ansible的默认配置一般无需修改。如果要配置个性化参数,Ansible按照如下顺序寻找配置文件,找到后不再继续寻找:

ansible.cfg是INI的变种。注释的行首的第一个字符可以是#;,如果注释出现在非行首,必须以;开头,例如:

# this is the first comment
inventory=/home/mars/hosts ; this is the second comment

通过包管理器安装的Ansible的默认配置文件在/etc/ansible目录中。如果Ansible是通过pip安装的,可以访问https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg下载配置文件示例。所有配置项的解释可以参考:https://docs.ansible.com/ansible/latest/reference_appendices/config.html#ansible-configuration-settings。

ansible-config命令可以查看Ansible主机的当前配置。如果通过环境变量设置了某个配置项的值,该配置项在配置文件中的值不再生效。ansibleansible-playbook命令行的参数值,优先级又比环境变量、配置文件中的值高。

安装配置Ansible完成后,建议继续阅读下一篇博文:学习Ansible[2]:小试牛刀,敲出通过Ansible在远端机器执行的第一条命令。

相关内容

    暂无相关文章