将循环变量传递给 ansible 角色

将循环变量传递给 ansible 角色

有没有办法将值列表传递到 /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

答案1

剧本可能是这样的。我还没有测试过代码,它只是即兴的......

---
- 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

答案2

看看这个例子yum 模块的文档

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

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

---
srv:
  - httpd
  - nginx

相关内容