Variable

將循環變數傳遞給ansible角色

  • February 27, 2020

有沒有辦法將值列表傳遞給 /vars/main.yml 文件,以便隨後將這些變數傳遞給任務進行處理。

這裡的目的是執行一次任務而不是 N 次。

我現在的/vars/main.yml

---
srv:
  httpd
  nginx

解析為TASK [app : Install the httpd nginx server package] ************ yum 模組不喜歡的。

---
   - name: Install the {{ srv }} server package
     yum:
       name: "{{ srv }}"
       state: present
   - name: Check for the installed package
     yum:
       list: "{{ srv }}"
     register: yumout
   - debug:
      var: yumout

劇本可能看起來是這樣的。我沒有測試過程式碼,它只是即興發揮……

---
- hosts: all
 tasks:
 - name: Install the {{ srv }} server package
   yum:
     name: "{{ item }}"
     state: present
   loop: "{{ srv }"
 - name: Check for the installed package
   yum:
     list: "{{ srv }}"
   loop: "{{ srv }"
   register: yumout
 - debug:
   msg: yumout

在 yum 模組的文件中查看此範例:

- name: ensure a list of packages installed
 yum:
   name: "{{ packages }}"
 vars:
   packages:
   - httpd
   - httpd-tools

您只需要更改您的列表/vars/main.yml

---
srv:
 - httpd
 - nginx

引用自:https://unix.stackexchange.com/questions/569936