配置文件中大多数都是注释行默认配置项。文件遵循INI格式,分为几个类别的配置,下面会分别介绍。
在那以前,先看一下配置文件存放的位置,以及如何从官网获取配置文件模板。html
Ansible只有一个配置文件ansible.cfg。配置文件能够存在于多个位置,按下面的顺序查找到的第一个生效:linux
只有最早找到的配置文件会生效。另外环境变量的优先级比配置文件高,并且能够一个一个单独设置。每一个配置项对应的环境变量的名称,能够在下面的官方文档中查到:
https://docs.ansible.com/ansible/latest/reference_appendices/config.html#common-options
另外每一个变量的默认值也是在这里查。nginx
还有命令行参数指定的方式,这个是优先级比上面的配置方式高。不过playbook中指定的设置优先级更高,不会被命令行参数覆盖。git
Ansible安装后,默认会在/etc/ansible/目录下生成配置文件。可是若是是经过pip或者是源码安装的话,就没有这个文件,须要手动建立。能够到官方的github上去下一个最新的:github
$ mkdir /etc/ansible $ cd /etc/ansible $ wget https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg
定义常规的链接类配置:web
[defaults] # some basic default values... #inventory = /etc/ansible/hosts # 定义Inventory,用于定义主机列表配置 #library = /usr/share/my_modules/ # 自定了lib库存放目录 #module_utils = /usr/share/my_module_utils/ #remote_tmp = ~/.ansible/tmp # 临时文件远程主机存放目录 #local_tmp = ~/.ansible/tmp # 临时文件本地存放目录 #plugin_filters_cfg = /etc/ansible/plugin_filters.yml #forks = 5 # 默认开启的并发数 #poll_interval = 15 # 默认轮询时间间隔 #sudo_user = root # 默认sudo用户 #ask_sudo_pass = True # 是否须要sudo密码 #ask_pass = True # 是否须要密码 #transport = smart #remote_port = 22 #module_lang = C #module_set_locale = False # additional paths to search for roles in, colon separated #roles_path = /etc/ansible/roles # 默认下载的Roles存在的目录 # uncomment this to disable SSH key host checking #host_key_checking = False # 首次链接是否须要检查key认证,建议放开注释设为False # SSH timeout #timeout = 10 # 默认超时时间 # logging is off by default unless this path is defined # if so defined, consider logrotate #log_path = /var/log/ansible.log # 执行日志存放目录 # default module name for /usr/bin/ansible #module_name = command # 默认执行模块 # set plugin path directories here, separate with colons #action_plugins = /usr/share/ansible/plugins/action # 各种插件的存放目录 #become_plugins = /usr/share/ansible/plugins/become #cache_plugins = /usr/share/ansible/plugins/cache #callback_plugins = /usr/share/ansible/plugins/callback #connection_plugins = /usr/share/ansible/plugins/connection #lookup_plugins = /usr/share/ansible/plugins/lookup #inventory_plugins = /usr/share/ansible/plugins/inventory #vars_plugins = /usr/share/ansible/plugins/vars #filter_plugins = /usr/share/ansible/plugins/filter #test_plugins = /usr/share/ansible/plugins/test #terminal_plugins = /usr/share/ansible/plugins/terminal #strategy_plugins = /usr/share/ansible/plugins/strategy # if set to a persistent type (not 'memory', for example 'redis') fact values # from previous runs in Ansible will be stored. This may be useful when # wanting to use, for example, IP information from one group of servers # without having to talk to them in the same playbook run to get their # current IP information. #fact_caching = memory # getfact 缓存的主机信息存放方式 # retry files # When a playbook fails a .retry file can be created that will be placed in ~/ # You can enable this feature by setting retry_files_enabled to True # and you can change the location of the files by setting retry_files_save_path #retry_files_enabled = False #retry_files_save_path = ~/.ansible-retry # 错误重启文件存放目录
上面这些配置多数都保持默认便可。只有一项host_key_checking = False
能够放开注释。redis
处于安全考虑,有时候不但愿直接以root用户直接部署应用,这时候就须要给普通用户sudo权限,这部分配置主要就是针对sudo用户提权的配置:shell
[privilege_escalation] #become=True # 是否sudo #become_method=sudo # sudo方式 #become_user=root # sudo后变为root用户 #become_ask_pass=False # sodu后是否验证密码
这部分功能不经常使用了,了解下把:apache
[paramiko_connection] # uncomment this line to cause the paramiko connection plugin to not record new host # keys encountered. Increases performance on new host additions. Setting works independently of the # host key checking setting above. #record_host_keys=False # 不记录新主机的key以提高效率 # by default, Ansible requests a pseudo-terminal for commands executed under sudo. Uncomment this # line to disable this behaviour. #pty=False # 禁用sudo功能
Ansible默认使用ssh链接主机,这里是SSH链接的一些配置:数组
# Enabling pipelining reduces the number of SSH operations required to # execute a module on the remote server. This can result in a significant # performance improvement when enabled, however when using "sudo:" you must # first disable 'requiretty' in /etc/sudoers # # By default, this option is disabled to preserve compatibility with # sudoers configurations that have requiretty (the default on many distros). # #pipelining = False # 管道加速功能,需配合requiretty使用方可生效
这块的配置项也很少,多数仍是保持默认便可。
Ansible链接加速相关配置:
[accelerate] #accelerate_port = 5099 # 加速链接端口 #accelerate_timeout = 30 # 命令执行超时时间,单位秒 #accelerate_connect_timeout = 5.0 # 链接超时时间,单位秒 # The daemon timeout is measured in minutes. This time is measured # from the last activity to the accelerate daemon. #accelerate_daemon_timeout = 30 # 上一个活动链接的时间,单位分数 # If set to yes, accelerate_multi_key will allow multiple # private keys to be uploaded to it, though each user must # have access to the system via SSH to add a new key. The default # is "no". #accelerate_multi_key = yes
这里的配置项在提供Ansible链接速度时会涉及,多数仍是保持默认
selinux几乎不会用,配置上仍是保持默认:
# Set this to yes to allow libvirt_lxc connections to work without SELinux. #libvirt_lxc_noseclabel = yes
输出结果颜色的设置。本来的配置已经很好了,几乎不用修改,保持默认:
[colors] #highlight = white #verbose = blue #warn = bright purple #error = red #debug = dark gray #deprecate = purple #skip = cyan #unreachable = red #ok = green #changed = yellow #diff_add = green #diff_remove = red #diff_lines = cyan
Inventory是Ansible管理主机的配置文件,默认位置是/etc/ansible/hosts,这个在ansible.cfg配置文件的开头有定义。
另外在ansible命令行中可使用-i参数来指定要使用的Inventory文件。
注释:使用#来写注释内容。
写在中括号中的内部表示组名。主机名能够是IP地址也能够是Hostname。主机名能够出现屡次,这样能够写到多个组里。
若是主机使用了非默认的SSH端口,则能够在主机名后加冒号指定SSH端口号。
配置示例::
# 配置示例 # 能够用IP地址 192.168.1.1 # 也可使用Hostname www.ansible.com docs.ansible.com:2222 # 使用中括号表示一个分组的开始,主机与主机之间能够有空行不影响分组 [webservers] web1.ansible.com # 用[10:20]表示连续的数组,包括10和20 web[10:20].ansible.com [dbservers] db-a.ansible.com # 也能够用中括号处理连续的字母 db-[b:f].ansible.com
对于一些非标准化的配置需求,能够在Inventory配置中进行设置。这样能够知足对于主机的一些个性化设置要求。
Ansible支持多种方式修改或自定义变量,Inventory是其中的一种修改方式。
定义主机变量
在定义主机时能够同时定义主机变量:
[webserverrs] web1.ansible.com http_port=8000 # 自定义http_port的端口号为8000
定义组变量
还能够定义组变量,同时对一组主机修改或自定义变量:
[groupservers] web1.ansible.com web2.ansible.com [groupservers:vars] http_port=8000
Default groups
Ansible还定义好了两个默认组:
Inventory中的组还能够包含其余的组,就是嵌套。嵌套时要在大组的名字后加上:children,表示嵌套的成员是组名而不是主机名:
[apache] httpd1.ansible.com httpd2.ansible.com [nginx] ngx1.ansible.com ngx2.ansible.com [webservers:children] apache nginx [webservers:vars] ntp_server=ntp1.aliyun.com
对于嵌套的组也是能够设置组变量的,这个和普通的组变量同样。
变量除了能够在Inventory中定义,还能够独立于Inventory文件以外单独定义到一个配置文件中。
下面是设置变量的各类途径,按优先级顺序排列:
比较经常使用的是以下4种:
单独定义vars配置文件,好比有个主机foosball,同时属于raleigh和webservers组,那么变量定义在以主机名或组名定义的以下3个文件中都有效:
Ansible基于SSH链接Inventory中指定远程主机时,还内置了一些参数,用于指定链接时的交互方式,下面列举了比较经常使用的参数:
链接要用到链接插件,插件会用到本身的变量,好比上面的这几个就是。下面的几个是通用的链接变量,这些变量插件也是能够识别的,效果是同样的。
下面3个是通用的链接用的变量:
这里的3个是通用的,优先级上应该会被上面的专用的覆盖掉。
SSH插件一共有哪些变量,这些变量的设置方式和对应的名称,包括默认值、环境变量、全部对应的变量名,能够查官方文档:
https://docs.ansible.com/ansible/latest/plugins/connection/ssh.html#ssh-connection
全部的链接查看,看这里:
https://docs.ansible.com/ansible/latest/plugins/connection.html?highlight=ansible_ssh_host
把设备设置的参数打印出来,进行验证。
local
指定使用链接插件为locol,这样执行时不会链接到远程主机,而是直接在ansible程序的主机上运行。这个效果能够用hostname来验证:
$ ansible HOST -m command -a "hostname" -c local
指定链接为local后,执行command模块的hostname命令,就是在当前的ansible主机上运行,返回的结果也是当前ansible主机的主机名。
echo
而后再用echo命令把参数的值打印出来:
$ ansible HOST -m command -a "echo {{VAR_KEY}}" -c local