项目作者: MasayukiMiyake97

项目描述 :
Ansible Dynamic Inventory sample
高级语言: Python
项目地址: git://github.com/MasayukiMiyake97/csv_Inventory.git
创建时间: 2018-01-29T13:07:21Z
项目社区:https://github.com/MasayukiMiyake97/csv_Inventory

开源协议:

下载


Ansible Dynamic Inventory サンプル(CSVファイル)

We are publishing a sample of Dynamic Inventory which loads CSV file as Inventory definition.
This sample is realized by reading the CSV file describing the configuration information and returning a character string of JSON format to Ansible.


CSVファイルをInventory定義として読み込む、Dynamic Inventoryのサンプルを公開しています。
このサンプルは、構成情報を記述したCSVファイルを読み込み、Ansibleに対してJSON形式の文字列を返すことで実現しています。

サンプルファイルの説明(Sample file description)

sample file description
playbooks/csv_inventory.py Dynamic Inventory file
playbooks/inventory.csv Inventory file in CSV format.
playbooks/common_val.yml This file defines common settings. Define common data for ALL group and group to which the node belongs.
playbooks/ansible.cfg Ansible configuration file.

playbooks/csv_inventory.py

Read CSV file, convert it to Ansible into JSON format configuration information and return it.
CSVファイルを読み込んで、Ansibleに対してJSON形式の構成情報に変換して返します。

playbooks/inventory.csv

S.group S.host_name S.ansible_host S.backend_ip S.frontend_ip B.is_active I.port_no I.weight F.sample
web_server web001 10.0.2.10 192.168.0.10 true 8080 1 20.1
web_server web002 10.0.2.11 192.168.0.11 true 2 0.22
web_server web003 10.0.2.12 192.168.0.12 true 5 0.1
web_server web004 10.0.2.13 192.168.0.13 true 4 2.6
ha_proxy proxy01 10.0.2.20 192.168.0.20 192.168.10.20 true 1.2

playbooks/common_val.yml

  1. inventory_list:
  2. - inventory.csv
  3. all_vars:
  4. all_test1: 123234
  5. all_test2: 2.13
  6. all_test3: True
  7. all_test4: test_data
  8. group_vars:
  9. ha_proxy:
  10. ha_proxy_conf_path: /etc/haproxy/haproxy.cfg
  11. http_port_no: 80
  12. web_server:
  13. web_conf_path: /etc/httpd/conf/httpd.conf
  14. port_no: 80
  15. specific_vars:
  16. specific_data: specific_val

csv_inventory.pyの出力例(Sample output from csv_inventory.py)

  1. $ ./csv_inventory.py | python -m json.tool
  2. {
  3. "_meta": {
  4. "hostvars": {
  5. "proxy01": {
  6. "ansible_host": "10.0.2.20",
  7. "backend_ip": "192.168.0.20",
  8. "frontend_ip": "192.168.10.20",
  9. "is_active": true,
  10. "sample": 1.2
  11. },
  12. "web001": {
  13. "ansible_host": "10.0.2.10",
  14. "backend_ip": "192.168.0.10",
  15. "is_active": true,
  16. "port_no": 8080,
  17. "sample": 20.1,
  18. "weight": 1
  19. },
  20. "web002": {
  21. "ansible_host": "10.0.2.11",
  22. "backend_ip": "192.168.0.11",
  23. "is_active": true,
  24. "sample": 0.22,
  25. "weight": 2
  26. },
  27. "web003": {
  28. "ansible_host": "10.0.2.12",
  29. "backend_ip": "192.168.0.12",
  30. "is_active": true,
  31. "sample": 0.1,
  32. "weight": 5
  33. },
  34. "web004": {
  35. "ansible_host": "10.0.2.13",
  36. "backend_ip": "192.168.0.13",
  37. "is_active": true,
  38. "sample": 2.6,
  39. "weight": 4
  40. }
  41. }
  42. },
  43. "all": {
  44. "vars": {
  45. "all_test1": 123234,
  46. "all_test2": 2.13,
  47. "all_test3": true,
  48. "all_test4": "test_data"
  49. }
  50. },
  51. "ha_proxy": {
  52. "hosts": [
  53. "proxy01"
  54. ],
  55. "vars": {
  56. "ha_proxy_conf_path": "/etc/haproxy/haproxy.cfg",
  57. "http_port_no": 80,
  58. "web_backend": [
  59. {
  60. "backend_ip": "192.168.0.10",
  61. "host_name": "web001",
  62. "port_no": 8080,
  63. "weight": 1
  64. },
  65. {
  66. "backend_ip": "192.168.0.11",
  67. "host_name": "web002",
  68. "port_no": 80,
  69. "weight": 2
  70. },
  71. {
  72. "backend_ip": "192.168.0.12",
  73. "host_name": "web003",
  74. "port_no": 80,
  75. "weight": 5
  76. },
  77. {
  78. "backend_ip": "192.168.0.13",
  79. "host_name": "web004",
  80. "port_no": 80,
  81. "weight": 4
  82. }
  83. ]
  84. }
  85. },
  86. "web_server": {
  87. "hosts": [
  88. "web001",
  89. "web002",
  90. "web003",
  91. "web004"
  92. ],
  93. "vars": {
  94. "port_no": 80,
  95. "web_conf_path": "/etc/httpd/conf/httpd.conf"
  96. }
  97. }
  98. }

ansibleの出力例(Sample output from ansible)

ansible_host

  1. $ ansible all -m debug -a "msg={{ ansible_host }}"
  2. proxy01 | SUCCESS => {
  3. "changed": false,
  4. "msg": "10.0.2.20"
  5. }
  6. web001 | SUCCESS => {
  7. "changed": false,
  8. "msg": "10.0.2.10"
  9. }
  10. web002 | SUCCESS => {
  11. "changed": false,
  12. "msg": "10.0.2.11"
  13. }
  14. web003 | SUCCESS => {
  15. "changed": false,
  16. "msg": "10.0.2.12"
  17. }
  18. web004 | SUCCESS => {
  19. "changed": false,
  20. "msg": "10.0.2.13"
  21. }

web_backend

web_backendの値は、csv_inventory.pyの処理の中で、読み込んだ構成情報から生成します。
The value of web_backend is generated from the read configuration information in the processing of csv_inventory.py.

  1. $ ansible ha_proxy -m debug -a "msg={{ web_backend }}"
  2. proxy01 | SUCCESS => {
  3. "changed": false,
  4. "msg": [
  5. {
  6. "backend_ip": "192.168.0.10",
  7. "host_name": "web001",
  8. "port_no": 8080,
  9. "weight": 1
  10. },
  11. {
  12. "backend_ip": "192.168.0.11",
  13. "host_name": "web002",
  14. "port_no": 80,
  15. "weight": 2
  16. },
  17. {
  18. "backend_ip": "192.168.0.12",
  19. "host_name": "web003",
  20. "port_no": 80,
  21. "weight": 5
  22. },
  23. {
  24. "backend_ip": "192.168.0.13",
  25. "host_name": "web004",
  26. "port_no": 80,
  27. "weight": 4
  28. }
  29. ]
  30. }

Copyright Copyright (c) 2018 MasayukiMiyake
License Apache License, Version 2.0