簡體   English   中英

AWS / Ansible-如何從動態清單中定義的主機跨角色/主機訪問事實?

[英]AWS/Ansible - How to access facts across roles/hosts from host defined in dynamic-inventory?

我目前正在設置許多Ansible角色來設置Kubernetes集群。 到目前為止,我已經負責提供冪等的EC2(1個Master / 2x Worker)以及后續角色,以設置具有Docker / Kubernetes依賴性的這些master / worker節點。 我正在使用AWS ec2.ini/.py dynamic-inventory來發現由我的create_ec2角色配置的實例的IP。

嘗試使用從主節點檢索的join命令將我的工作人員加入集群時遇到問題。 我有2個單獨的角色,負責主服務器和輔助服務器的配置。 在主服務器的任務中,我得到帶有以下內容的join命令:

kubeadm token create --print-join-command

然后注冊一個變量,然后將其用於設置主機事實:

set_fact:
  join_command: "{{ join_command_stdout.stdout_lines[0] }}"

我遇到的問題是當我在運行工作角色時嘗試在工作節點上訪問此事實時。 我正在嘗試通過以下方式訪問事實:

"{{ hostvars['tag_Type_master'].join_command }} --ignore-preflight-errors all  >> node_joined.txt"

但是它失敗了,因為我為hostvars提供的主機顯然是不確定的。

作為參考,我將這個值保存在動態庫存中(省略了IP):

  "tag_Type_master": [
    "1.2.3.4"

我收到的錯誤是:

"{"msg": "The task includes an option with an undefined variable. The error was: \"hostvars['tag_Type_master']\" is undefined"

我正在努力弄清楚如何訪問動態清單中定義的EC2實例的宿主事實。

我曾嘗試將EC2 IP直接添加到hostvars( hostvars['1.2.3.4'].join_command )中,但是任務只是掛起,什么也不做。

我也嘗試將Magic變量( hostvars['inventory_hostname].join_command )無效。

人們似乎已經成功地從靜態清單文件中定義的主機訪問主機事實,但是由於EC2服務器的動態特性,將無法使用該方法來創建群集。

run.yml:

  name: Setup K8s master node
  hosts: tag_Name_kube_master    
  gather_facts: true    
  roles:    
  - setup_kube_master


  name: Setup K8s worker nodes
  hosts: tag_Name_kube_worker
  gather_facts: true
  roles: 
  - setup_kube_worker

setup_kube_master / tasks / set_fact.yml:

  name: Get join command for workers    
  shell: kubeadm token create --print-join-command    
  register: join_command_stdout    
  name: Persist variable for workers    
  set_fact:    
   join_command: "{{ join_command_stdout.stdout_lines[0] }}"

setup_kube_worker / tasks / get_fact.yml:

  name: join cluster
  shell: "{{ hostvars['tag_Type_master'].join_command }} --ignore-preflight-errors all  >> node_joined.txt"    
  args:    
   chdir: $HOME    
   creates: node_joined.txt

因此,您自己解決此問題的方法是使用debug:任務顯示整個事實緩存並為您自己找到關系:

- name: show the state of affairs
  debug: var=hostvars verbosity=0

不過,話說回來,我敢肯定, tag_Type_master被定義為一 ,因此不會出現hostvars因為-顧名思義-它是vars對於主機vars 團體

您必須執行一個間接級別來獲得屬於該組成員的主機:

- hosts: tag_Name_kube_worker
  tasks:
  - name: promote the "join_command" fact from the masters group
    set_fact:
      join_command: '{{ some_master.join_command }}'
    vars:
      some_master: '{{ hostvars[groups["tag_Type_master"][0]] }}'

為了簡潔起見,我對some_master定義進行了一些自由處理-在生產代碼中,您實際上想檢查該組是否存在,並且其內容不為空等,等等,但是我大約80%確信它可以工作即使寫成

您希望將其顯示在run.yml hosts: tag_Type_master之間的run.ymlhosts: tag_Type_masterhosts: tag_Type_worker以彌合兩組之間的事實差距,並使其看起來好像工人在整個時間中都具有join_command事實


另外,雖然這不是您要的內容,但是如果您用"kubernetes.io/role/master": ""和/或"kubernetes.io/role": "master"標記這些實例,您將已經受益匪淺具有cloud-provider期望的標簽。 我不知道在ec2.pyec2.py ,但我敢肯定,使用ansible-inventory -i ec2.py --list來查找會很便宜

我與相應的標記工人kubernetes.io/role: worker即使我敢肯定,在AWS雲提供商不關心它,而是選擇只使用metadata.labels上現有的節點做ELB注冊等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM