ansible hosts配置


  1. 編輯/etc/ansible/hosts
  2. 添加本機的public SSH key到目標機器的authorized_keys   #ssh-copy-id
  3. 添加本機的私鑰到Ansible
  4. 運行ansible all -m ping 測試是否添加成功
Inventory 分組
    Ansible可同時操作屬於一個組的多台主機,組和主機之間的關系通過inventory文件配置,默認文件路徑為/etc/ansible/hosts
 
常用參數配置:
    ansible_ssh_host                    # 目標主機地址
    ansible_ssh_port                    # 目標主機端口,默認22
    ansible_ssh_user                    # 目標主機用戶
    ansible_ssh_pass                    # 目標主機ssh密碼
    ansible_sudo_pass                 # sudo密碼
    ansible_sudo_exe                    
    ansible_connection               # 與主機的連接類型,比如:local,ssh或者paramiko
    ansible_ssh_private_key_file  # 私鑰地址
    ansible_shell_type                 # 目標系統的shell類型
    ansible_python_interpreter   # python版本
 
格式:[組名] 
    例如 : 
      [test]     # 組名  
      10.0.0.1  # 主機ip  或者10.0.0.1:65522 自定義端口
別名
    s1 ansible_ssh_port=65522 ansible_ssh_host=10.0.0.1 ansible_ssh_user=simon    # 別名s1
 
連續的主機
  [g1]
         g[1:50].example.com
          g[a-f].example.com
 
[all:vars]         # *:vars 塊變量,all:vars 全局變量
ansible_ssh_private_key_file=/root/.ssh/id_rsa
ansible_ssh_port=22
ansible_ssh_user=root

[t3:vars]  # t3 使用python解釋器是python2
ansible_python_interpreter=/usr/bin/python2
nginx_port=80               # 私有變量在playbooks中使用

[t3]
192.168.11.162

[k8s:children]  # 使用children 定義主機組之間繼承關系
master
node

[master]
k8s-master01 ansible_ssh_host=192.168.11.101
k8s-master01 ansible_ssh_host=192.168.11.102

[node]
k8s-node01 ansible_ssh_host=192.168.11.105
k8s-node01 ansible_ssh_host=192.168.11.106



 Inventory 分文件管理(文件名即是組名)

  分文件:
          /etc/ansible/group_vars/ vim
          /etc/ansible/group_vars/ webservers
          /etc/ansible/host_vars/ foosball
          vim 文件中的內容: 變量
              ntp_server: acme.example.org
              db_server: 10.0.0.1
 
  分文件夾:(文件夾就是組名)
          /etc/ansible/group_vars/ vim/install
          /etc/ansible/group_vars/ vim/init
          /etc/ansible/host_vars/foosball

 

 1 #!/usr/bin/env python
 2 # _*_coding:utf-8_*_
 3 
 4 """
 5 @Time :    2019/11/8 15:54
 6 @Author:  Simon
 7 @File: get_ansible_hosts.py
 8 @Software: PyCharm
 9 """
10 
11 try:
12     from ansible.parsing.dataloader import DataLoader
13     from ansible.inventory.manager import InventoryManager
14     from ansible.vars.manager import VariableManager
15 except ImportError as e:
16     print("請安裝ansible模塊, 安裝方式pip install ansible")
17 
18 
19 def main(sources):
20     """
21     # 獲取ansible hosts 列表
22     :param sources:
23     :return:
24     """
25     hosts_list = []
26     loader = DataLoader()
27     inventory = InventoryManager(loader=loader, sources=sources)
28     variable_manager = VariableManager(loader=loader, inventory=inventory)
29     get_groups_dict = inventory.get_groups_dict()
30     host_list = inventory.hosts
31     for _host in host_list:
32         host = inventory.get_host(hostname=_host)
33         extra_vars = variable_manager.get_vars(host=host)
34         extra_vars_group = extra_vars.get("groups")
35         ans_port = extra_vars.get("ansible_ssh_port") if extra_vars.get("ansible_ssh_port") else extra_vars.get(
36             "ansible_port")
37         ans_host = extra_vars.get("ansible_ssh_host") if extra_vars.get("ansible_ssh_host") else extra_vars.get(
38             "ansible_host")
39         hosts_list.append({
40             "hostname": extra_vars.get("inventory_hostname"),
41             "host": ans_host,
42             "port": ans_port if ans_port else 22,
43             "group_name": extra_vars.get("group_names")
44         })
45 
46     return hosts_list
47 
48 
49 if __name__ == "__main__":
50     sources = ["/etc/ansible/hosts"]
51     print(main(sources))
Python 獲取host列表

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM