1、inventory文件
1、inventory文件说明:
目标主机的组织编排,灵活的将ad-hoc或plays传递给不同的主机。
主机、plays较少,直接在ansible 根目录使用hosts文件即可,
如果较为复杂,可以创建inventory文件夹,分不同inventory文件:
inventory- hosts01- hosts02- test
inventory文件的编写例子:
mail.example.com[webservers]foo.example.combar.example.com[dbservers]one.example.comtwo.example.comthree.example.combadwolf.example.com:5309jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50[webservers]www[01:50].example.com[databases]db-[a:f].example.com[targets]localhost ansible_connection=localother1.example.com ansible_connection=ssh ansible_ssh_user=mpdehaanother2.example.com ansible_connection=ssh ansible_ssh_user=mdehaan[atlanta]host1 http_port=80 maxRequestsPerChild=808host2 http_port=303 maxRequestsPerChild=909[atlanta]host1host2[atlanta:vars]ntp_server=ntp.atlanta.example.comproxy=proxy.atlanta.example.com[atlanta]host1host2[raleigh]host2host3[southeast:children]atlantaraleigh[southeast:vars]some_server=foo.southeast.example.comhalon_system_timeout=30self_destruct_countdown=60escape_pods=2[usa:children]southeastnortheastsouthwestnorthwest
2、inventory中的变量
1、inventory文件中定义
在主机之后: key=value
inventory文件例子:
some_host ansible_ssh_port=2222 ansible_ssh_user=manageraws_host ansible_ssh_private_key_file=/home/example/.ssh/aws.pemfreebsd_host ansible_python_interpreter=/usr/local/bin/pythonruby_module_host ansible_ruby_interpreter=/usr/bin/ruby.1.9.3
参数说明:
ansible_ssh_host将要连接的远程主机名.与你想要设定的主机的别名不同的话,可通过此变量设置.ansible_ssh_portssh端口号.如果不是默认的端口号,通过此变量设置.ansible_ssh_user默认的 ssh 用户名ansible_ssh_passssh 密码(这种方式并不安全,我们强烈建议使用 --ask-pass 或 SSH 密钥)ansible_sudo_passsudo 密码(这种方式并不安全,我们强烈建议使用 --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_filessh 使用的私钥文件.适用于有多个密钥,而你不想使用 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 的路径....
2、引用其他文件中的变量
在 inventory 主文件中保存所有的变量并不是最佳的方式.
还可以保存在独立的文件中,这些独立文件与 inventory 文件保持关联.
不同于 inventory 文件(INI 格式),这些独立文件的格式为 YAML.
例如:
/etc/ansible/group_vars/raleigh/etc/ansible/group_vars/webservers/etc/ansible/host_vars/foosball#/etc/ansible/group_vars/raleigh---ntp_server: acme.example.orgdatabase_server: storage.example.org
当变量变得太多时,分文件定义变量更方便我们进行管理和组织:
为一个主机,或一个组,创建一个目录,目录名就是主机名或组名.
目录中的可以创建多个文件, 文件中的变量都会被读取为主机或组的变量.
如下 ‘raleigh’ 组对应于 /etc/ansible/group_vars/raleigh/ 目录,
其下有两个文件 db_settings 和 cluster_settings, 其中分别设置不同的变量:
/etc/ansible/group_vars/raleigh/db_settings/etc/ansible/group_vars/raleigh/cluster_settings
2、动态inventory
使用配置管理系统经常有一种需求,可能要在其他的软件系统中保存自己的 inventory 配置信息.
Ansible 本身通过基于文本的方式来记录 inventory 配置信息,这在前面已介绍过(详见 Inventory文件 ).
除此之外,Ansible 也支持用其他方式保存配置信息.
在其他软件系统保存配置信息的例子有:
1, 从云端拉取 inventory2, LDAP(Lightweight Directory Access Protocol,轻量级目录访问协议)3, `Cobbler <http://cobbler.github.com>`_4, 或者是一份昂贵的企业版的 CMDB(配置管理数据库) 软件.
3、pattern 与 inventory
ad-hoc中 pattern 匹配主机例子:
#命令格式如下:ansible <pattern_goes_here> -m <module_name> -a <arguments>ansible webservers -m service -a "name=httpd state=restarted"#其中的<pattern_goes_here> 还可以有以下写法匹配#所有主机all*#ip,hostnameone.example.comone.example.com:two.example.com192.168.1.50192.168.1.*#组webserverswebservers:dbservers#非webservers:!phoenix#交集webservers:dbservers:&staging:!phoenix#传参数,使用变量webservers:!{{excluded}}:&{{required}}#单个host names, IPs , groups的通配*.example.com*.com#host和group混合使用one*.com:dbservers#匹配组中的某些serverwebservers[0]webservers[0-25]#正则匹配~(web|db).*.example.com
pattern 的一些技能:
适用于ansible和ansible-playbook
#通过 --limit 标记来添加排除条件ansible-playbook site.yml --limit datacenter2#从文件读取hosts,文件名以@为前缀ansible-playbook site.yml --limit @retry_hosts.txt