with_items的Ansible剧本循环

时间:2020-04-13 19:37:12

标签: linux ansible jinja2 ansible-2.x sudoers

我必须使用import React from 'react'; import Messages from '../components/messagesList'; import fetchMock from "fetch-mock"; import MESSAGES from './data/messages'; fetchMock.get('/messages', MESSAGES); export default { title: 'Messages', component: Messages }; export const ToStorybook = () => <Messages />; ToStorybook.story = { name: 'Messages list', }; 剧本用很少的行/命令来更新sudoers.d个用户文件

users.yml

ansible

main.yml

user1:
   - Line1111
   - Line2222
   - Line3333

user2:
   - Line4444
   - Line5555
   - Line6666

以上内容仅适用于- hosts: "{{ host_group }}" vars_files: - ../users.yml tasks: - name: Add user "user1" to sudoers.d lineinfile: path: /etc/sudoers.d/user1 line: '{{ item }}' state: present mode: 0440 create: yes validate: 'visudo -cf %s' with_items: - "{{ user1 }}"

如果我还想包含user2->如何更改文件名:路径:user1

我在下面尝试了并且它不起作用

在运行时将以下用户作为变量传递给/etc/sudoers.d/user1

main.yml

因此,基本上,我想将用户作为users: - "user1" - "user2" - name: Add user "{{users}}" to sudoers.d lineinfile: path: /etc/sudoers.d/{{users}} line: '{{ item }}' state: present mode: 0440 create: yes validate: 'visudo -cf %s' with_items: - "{{ users }}" {{users}}传递给变量user1,并希望为user2中的每个用户使用这些行并将其添加到相应的用户文件(users.yml/etc/sudoers.d/user1)。

所以/etc/sudoers.d/user2应该看起来像

/etc/sudoers.d/user1

Line1111 Line2222 Line3333 应该看起来像

/etc/sudoers.d/user2

3 个答案:

答案 0 :(得分:0)

尝试添加引号:

users:
   - "user1"
   - "user2"

- name: "Add user {{users}} to sudoers.d"
  lineinfile:
    path: "/etc/sudoers.d/{{users}}"
    line: "{{ item }}"
    state: present
    mode: 0440
    create: yes
    validate: 'visudo -cf %s'
  with_items:
      - "{{ users }}"

根据使用变量的Ansible Documentation

YAML语法要求,如果您以{{ foo }}开头的值,请用引号将整个行引起来,因为它希望确保您没有尝试启动YAML字典。 YAML Syntax文档中对此进行了介绍。

这行不通:

- hosts: app_servers
  vars:
      app_path: {{ base_path }}/22

这样做,您会没事的:

- hosts: app_servers
  vars:
       app_path: "{{ base_path }}/22"

答案 1 :(得分:0)

cat users.yml
---
users:
  - user1:
    filename: user1sudoers
    args:
      - Line1111
      - Line2222
      - Line3333
  - user2:
    filename: user2sudoers
    args:
      - Line4444
      - Line5555
      - Line6666

我在这里使用模板,而不是lineinfile

---
cat sudoers.j2
{% if item.args is defined and item.args %}
{%     for arg in item.args %}
{{ arg }}
{%     endfor %}
{% endif %}

任务内容

---
- hosts: localhost
  vars_files: ./users.yml
  tasks:
    - name: sync sudoers.j2 to localhost
      template:
        src: sudoers.j2
        dest: "/tmp/{{ item.filename }}"
      loop: "{{ users_list }}"
      when: "users_list is defined and users_list"

运行task.yml后,在/ tmp目录下生成两个文件。

cat /tmp/user1sudoers
Line1111
Line2222
Line3333

cat /tmp/user2sudoers
Line4444
Line5555
Line6666

答案 2 :(得分:0)

我为users.yml尝试了以下内容

users: 
    - user1: 
          - "Line1111" 
          - "Line2222" 
          - "Line3333" 
    - user2: 
          - "Line4444" 
          - "Line5555"

以及main.yml

- name: Add user "wasadmin" to sudo avsrelmgmt 
      lineinfile:  ```
        path: /etc/sudoers.d/"{{ users }}"  
        line: '{{ item}}'  
        state: present  
        mode: 0440  
        create: yes  
        validate: 'visudo -cf %s'  
      with_items:  
        - "{{ users }}"

上面的行路径:/etc/sudoers.d /“ {{users}}” 指定使用名称(“-user1:-” Line1111“)创建文件夹