一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - ansible動態Inventory主機清單配置遇到的坑

ansible動態Inventory主機清單配置遇到的坑

2020-04-13 10:13朝魯夢_FQM Python

這篇文章主要介紹了ansible動態Inventory主機清單配置遇到的坑,需要的朋友可以參考下

坑1 : 動態主機清單配置,需要按照ansible的要求的格式返回給ansible命令的

源代碼如下:

但是在ansible-playbook中使用動態主機配置文件的時候,發生了錯誤!!!

ansible動態Inventory主機清單配置遇到的坑

提示沒有匹配的主機信息

分析: 數據庫已配置好,python腳本也能輸出,問題在于輸出的結果不是ansible想要的格式作為ansible的命令輸入,因此排查如下

下面看下我的動態inventory輸出的格式吧

?
1
2
3
4
5
6
7
8
9
10
11
12
[root@ansible fang]# python ansible_inventory.py --list
{
  "all": [
    "192.168.10.104"
  ]
}
[root@ansible fang]# python ansible_inventory.py --host 192.168.10.104
{
  "ansible_ssh_host": "192.168.10.104",
  "ansible_ssh_user": "root",
  "hostname": "clone-node1"
}

在網上找的方法,雖然實現了—list  --host的輸出,但是格式不滿足ansible格式輸出的要求,ansible需求的格式有哪些呢,請看解決辦法中….

輸出結果:

這是出錯的信息,提示還是找不到主機的信息

[root@ansible fang]#
ansible-playbook -i ansible_inventory.py bb.yml運行出錯

解決方法:

先說個知識點(ansible所要求的格式):

動態 Inventory 指通過外部腳本獲取主機列表,并按照 ansible 所要求的格式返回給 ansilbe 命令的

因此,需要清楚ansible需要那種inventory的格式呢

必須輸出為 JSON 格式

同時必須支持兩個參數:--list 和 --host <hostname>。

--list:用于返回所有的主機組信息,每個組所包含的主機列表 hosts、所含子組列表 children、主機組變量列表 vars 都應該是字典形式的,_meta 用來存放主機變量。

正確的輸出格式是什么樣子的呢,請看下面:

以下的是正確的動態inventory的輸出格式,其中就是ansible的第三點要求 每個組所包含的主機列表 hosts、所含子組列表 children、主機組變量列表 vars 都應該是字典形式的,_meta 用來存放主機變量。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@ansible fang]# vim tt.py
[root@ansible fang]# python tt.py
{
  "group1": {
    "hosts": [
      "192.168.10.104"
    ]
  },
  "group2": {
    "hosts": [
      "192.168.10.103",
      "192.168.13.5"
    ],
    "vars": {
      "ansible_ssh_port": 22,
      "ansible_connection": "ssh"
    }
  }
}
[root@ansible fang]#

按照以上的格式,來編寫我們的輸出吧,

SQL表格內容如下:

ansible動態Inventory主機清單配置遇到的坑

我想要輸出的json格式是這樣的

?
1
2
3
4
5
6
7
8
9
{組名:{
hosts:[‘ip1','ip2'],
vars:{
  “ansible_ssh_port”:22,
“ansilble_connection”:'ssh'
……….
}
}
}

腳本代碼列出來了如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#_*_coding:utf-8_*_
__author__ = 'fang'
import pymysql
import json
import argparse
import sys
def execude_sql(table): #定義一個執行SQL的函數
  sql = 'select * from {0};'.format(table)
  cur.execute(sql) #args即要傳入SQL的參數
  sys_result = cur.fetchall()
  #index = cur.description
  hostlist = {}#放入主機清單的信息
  for i in sys_result:
    hostlist[i[2]] = []
  for i in sys_result:
    hostlist[i[2]].append([i[1], i[5], i[6]])
  host_lists = dict()
  for i in hostlist.iteritems():
    dict_item = dict()
    for index in i[1]:
      dict_item[index[0]] = {'ansible_connection': index[1], 'ansible_ssh_port': index[2]}
    host_lists[i[0]] = dict_item
  # print json.dumps(host_lists, indent=4)
return host_lists
def group(data):
  '''
  all hostip
  :param data:
  :return:
  '''
  count_ip = dict()
  count_ip['all'] = {}
  count_ip['all']['hosts'] = []
  index = []
  for i in data:
    index.extend(data[i].keys())
  count_ip['all']['hosts'].extend(list(set(index)))
  print json.dumps(count_ip, indent=4)
def host(data, ip):
  dict_host = {}
  for i in data:
    if data[i].keys() == [ip]:
      dict_host[i] = {}
      dict_host[i]['hosts'] = [ip]
      dict_host[i]['vars'] = data[i][ip]
      print json.dumps(dict_host, indent=4)
      break
if __name__ == "__main__":
  global file, con, cur #文件對象,連接和游標對象
  #連接數據庫
  con = pymysql.connect('127.0.0.1', 'root', '', 'ansible', charset='utf8') # 連接數據庫
  cur = con.cursor() # 定義一個游標 
  data = execude_sql('hosts_table')
# parser = argparse.ArgumentParser()#定義參數解析器
#獲取參數的方法1:
#以下是參數解析器添加參數格式,有—list和—host dest表示都可以通過args.list或者args.host來獲取到可變參數的值,action中store_true表存儲的是布爾值,當沒有—list的時候默認false,當有—list的時候,但是沒有值,默認則為true,help表示幫助時候提示的信息,argparse很好用,在這里恰當好處
  # parser.add_argument('--list',action='store_true',dest='list',help='get all hosts')
  # parser.add_argument('--host',action='store',dest='host',help='get sigle host')
  # args = parser.parse_args()
  # if args.list:
  #   group(data)
  # if args.host:
  #   host(data, args.host)
#獲取參數的方法2:
   if len(sys.argv) == 2 and (sys.argv[1] == '--list'):
      group(data)
   elif len(sys.argv) == 3 and (sys.argv[1] == '--host'):
       host(data, sys.argv[2])
   else:
     print "Usage %s --list or --host <hostname>"% sys.argv[0]
     sys.exit(1)

坑 2: 動態inventory腳本要制定python的解釋器,否則無法執行

問題分析:

Ansible-playbook –I ansbile_inventory.py bb.yml執行

提示:無法識別host,還是出現了問題

對比ansible要求的格式,沒有差別,最后進行代碼的比對,問題出現在腳本沒有制定Python解釋器,導致出現的問題

解決辦法:

添加python解釋器的路徑

ansible動態Inventory主機清單配置遇到的坑

執行結果:

Yml文件

ansible動態Inventory主機清單配置遇到的坑

命令執行結果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@ansible fang]# ansible-playbook -i ansible_inventory.py bb.yml
PLAY [192.168.10.104] *********************************************************************
TASK [debug] *********************************************************************
ok: [192.168.10.104] => {
  "msg": "this is test block"
}
TASK [file] *********************************************************************
ok: [192.168.10.104]
TASK [debug] *********************************************************************
ok: [192.168.10.104] => {
  "msg": "this is always"
}
PLAY RECAP *********************************************************************
192.168.10.104       : ok=3  changed=0  unreachable=0  failed=0
[root@ansible fang]# python ansible_inventory.py --host 192.168.10.104
{
  "xiaoming": {
    "hosts": [
      "192.168.10.104"
    ],
    "vars": {
      "ansible_ssh_port": 22,
      "ansible_connection": "ssh"
    }
  }
}

另外注意點:  --list    --host 正是通過yml中的hosts指定的內容,即為腳本中命令行的參數的內容

 總結

以上所述是小編給大家介紹的ansible動態Inventory主機清單配置遇到的坑,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!

原文鏈接:https://www.cnblogs.com/chaolumeng/p/ansible01.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 动漫美女被吸乳 | 我的美女奴隶 | 亚洲国产欧美在线人成aaaa20 | 亚洲欧美精品天堂久久综合一区 | 亚洲日本va中文字幕 | 亚洲免费网站在线观看 | 久久理论片迅播影院一级 | 麻豆网站视频国产在线观看 | 故意短裙公车被强好爽在线播放 | 亚洲欧美日韩综合在线播放 | 美女啪啪国产 | 隔壁老王国产精品福利 | 国语自产拍在线观看7m | 青青青青青国产费线在线观看 | 久久精品国产亚洲AV蜜臀 | 美女吃男生鸡鸡 | 91久久线看在观草草青青 | 四虎在线观看 | bt天堂在线观看国产 | 国内自拍2019| 东北疯狂xxxxbbbb中国 | 波多野结衣小说 | 久久亚洲精品中文字幕60分钟 | 99视频在线观看免费视频 | x8x8在线观看 | 高清视频在线播放ww | 亚洲精品电影天堂网 | 美女无内裤下部黄 | 免费看又黄又爽又猛的视频软件- | 校园刺激全黄H全肉细节文 校草让我脱了内裤给全班看 | 九九99香蕉在线视频免费 | 精品乱lun小说 | 日韩一级在线观看 | 王者荣耀瑶白色液体 | 国产一级特黄aa大片在线 | 欧美一卡2卡3卡四卡海外精品 | 日本亚洲欧洲高清有码在线播放 | 精品成人一区二区三区免费视频 | 日韩精品视频在线观看免费 | 91麻豆精品 | 国产在线一区二区视频 |