ansible inventory

1. inventory file

1. Inventory file description:

The organization and arrangement of the target host, and the flexible delivery of ad-hoc or plays to different hosts.
There are few hosts and plays, just use the hosts file directly in the ansible root directory,
If it is more complicated, you can create an inventory folder and divide it into different inventory files:
  1. inventory
  2. - hosts01
  3. - hosts02
  4. - test
 An example of writing an inventory file:
  1. mail.example.com
  2. [webservers]
  3. foo.example.com
  4. bar.example.com
  5. [dbservers]
  6. one.example.com
  7. two.example.com
  8. three.example.com
  9. badwolf.example.com:5309
  10. jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50
  11. [webservers]
  12. www[01:50].example.com
  13. [databases]
  14. db-[a:f].example.com
  15. [targets]
  16. localhost ansible_connection=local
  17. other1.example.com ansible_connection=ssh ansible_ssh_user=mpdehaan
  18. other2.example.com ansible_connection=ssh ansible_ssh_user=mdehaan
  19. [atlanta]
  20. host1 http_port=80 maxRequestsPerChild=808
  21. host2 http_port=303 maxRequestsPerChild=909
  22. [atlanta]
  23. host1
  24. host2
  25. [atlanta:vars]
  26. ntp_server=ntp.atlanta.example.com
  27. proxy=proxy.atlanta.example.com
  28. [atlanta]
  29. host1
  30. host2
  31. [raleigh]
  32. host2
  33. host3
  34. [southeast:children]
  35. atlanta
  36. raleigh
  37. [southeast:vars]
  38. some_server=foo.southeast.example.com
  39. halon_system_timeout=30
  40. self_destruct_countdown=60
  41. escape_pods=2
  42. [usa:children]
  43. southeast
  44. northeast
  45. southwest
  46. northwest

2. Variables in inventory

1. Defined in the inventory file

After host: key=value
Inventory file example:
  1. some_host ansible_ssh_port=2222 ansible_ssh_user=manager
  2. aws_host ansible_ssh_private_key_file=/home/example/.ssh/aws.pem
  3. freebsd_host ansible_python_interpreter=/usr/local/bin/python
  4. ruby_module_host ansible_ruby_interpreter=/usr/bin/ruby.1.9.3
Parameter Description:
  1. ansible_ssh_host
  2. 将要连接的远程主机名.与你想要设定的主机的别名不同的话,可通过此变量设置.
  3. ansible_ssh_port
  4. ssh端口号.如果不是默认的端口号,通过此变量设置.
  5. ansible_ssh_user
  6. 默认的 ssh 用户名
  7. ansible_ssh_pass
  8. ssh 密码(这种方式并不安全,我们强烈建议使用 --ask-pass SSH 密钥)
  9. ansible_sudo_pass
  10. sudo 密码(这种方式并不安全,我们强烈建议使用 --ask-sudo-pass)
  11. ansible_sudo_exe (new in version 1.8)
  12. sudo 命令路径(适用于1.8及以上版本)
  13. ansible_connection
  14. 与主机的连接类型.比如:local, ssh 或者 paramiko. Ansible 1.2 以前默认使用 paramiko.1.2 以后默认使用 'smart','smart' 方式会根据是否支持 ControlPersist, 来判断'ssh' 方式是否可行.
  15. ansible_ssh_private_key_file
  16. ssh 使用的私钥文件.适用于有多个密钥,而你不想使用 SSH 代理的情况.
  17. ansible_shell_type
  18. 目标系统的shell类型.默认情况下,命令的执行使用 'sh' 语法,可设置为 'csh' 'fish'.
  19. ansible_python_interpreter
  20. 目标主机的 python 路径.适用于的情况: 系统中有多个 Python, 或者命令路径不是"/usr/bin/python",比如 \*BSD, 或者 /usr/bin/python
  21. 不是 2.X 版本的 Python.我们不使用 "/usr/bin/env" 机制,因为这要求远程用户的路径设置正确,且要求 "python" 可执行程序名不可为 python以外的名字(实际有可能名为python26).
  22. ansible_python_interpreter 的工作方式相同,可设定如 ruby perl 的路径....
 

2. Reference variables in other files

It is not the best way to keep all variables in the main inventory file.
It can also be stored in separate files that remain associated with the inventory file. 
Unlike  inventory files (INI format), these stand-alone files are in YAML format.
E.g:
  1. /etc/ansible/group_vars/raleigh
  2. /etc/ansible/group_vars/webservers
  3. /etc/ansible/host_vars/foosball
  4. #/etc/ansible/group_vars/raleigh
  5. ---
  6. ntp_server: acme.example.org
  7. database_server: storage.example.org
When variables become too many, it is more convenient for us to manage and organize variables by defining them in separate files:
For a host, or a group, create a directory, the directory name is the host name or group name.
Multiple files can be created in the directory, and the variables in the files will be read as host or group variables.
The following 'raleigh' group corresponds to the /etc/ansible/group_vars/raleigh/ directory,
There are two files db_settings and cluster_settings under it, which set different variables respectively:
  1. /etc/ansible/group_vars/raleigh/db_settings
  2. /etc/ansible/group_vars/raleigh/cluster_settings

2. Dynamic inventory

There is often a need to use a configuration management system to store your own inventory configuration information in other software systems.

Ansible itself records inventory configuration information in a text-based way, which was introduced earlier (see the inventory  file for details  ).

In addition, Ansible also supports other ways to save configuration information.

Examples of saving configuration information in other software systems are:

 

  1. 1, 从云端拉取 inventory
  2. 2, LDAPLightweight Directory Access Protocol,轻量级目录访问协议)
  3. 3, `Cobbler <http://cobbler.github.com>`_
  4. 4, 或者是一份昂贵的企业版的 CMDB(配置管理数据库) 软件.

3. Pattern and inventory

Example of pattern matching host in ad-hoc:
  1. #命令格式如下:
  2. ansible <pattern_goes_here> -m <module_name> -a <arguments>
  3. ansible webservers -m service -a "name=httpd state=restarted"
  4. #其中的<pattern_goes_here> 还可以有以下写法匹配
  5. #所有主机
  6. all
  7. *
  8. #ip,hostname
  9. one.example.com
  10. one.example.com:two.example.com
  11. 192.168.1.50
  12. 192.168.1.*
  13. #组
  14. webservers
  15. webservers:dbservers
  16. #非
  17. webservers:!phoenix
  18. #交集
  19. webservers:dbservers:&staging:!phoenix
  20. #传参数,使用变量
  21. webservers:!{{excluded}}:&{{required}}
  22. #单个host names, IPs , groups的通配
  23. *.example.com
  24. *.com
  25. #host和group混合使用
  26. one*.com:dbservers
  27. #匹配组中的某些server
  28. webservers[0]
  29. webservers[0-25]
  30. #正则匹配
  31. ~(web|db).*\.example\.com
Some skills of pattern:
Works with ansible and ansible-playbook
  1. #通过 --limit 标记来添加排除条件
  2. ansible-playbook site.yml --limit datacenter2
  3. #从文件读取hosts,文件名以@为前缀
  4. ansible-playbook site.yml --limit @retry_hosts.txt
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324932053&siteId=291194637