首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在with_items中运行with_items

在with_items中运行with_items
EN

Stack Overflow用户
提问于 2017-05-17 20:02:42
回答 2查看 1.8K关注 0票数 2

我目前运行的命令如下:

代码语言:javascript
复制
name: Generate a timed-based code for user
  command: "{{ item.command }} creates={{ item.file}}"
  with_items:
    - { command: '/usr/bin/google-authenticator -t -f -d --label=user1 --qr-mode=ANSI -r 3 -R 30 -w 1 --secret=/home/user1/.google_authenticator', file: '/home/user1/.google_authenticator' }
    - { command: '/usr/bin/google-authenticator -t -f -d --label=user2 --qr-mode=ANSI -r 3 -R 30 -w 1 --secret=/home/user2/.google_authenticator', file: '/home/user2/.google_authenticator' }

这很好,但根本不是DRY

我正在尝试做第二个像这个角色的with_items

代码语言:javascript
复制
- include_vars: main.yml

- name: Generate a timed-based code for user
  command: "{{ item.command }} creates={{ item.file }}"
  with_items:
    - { command: '/usr/bin/google-authenticator -t -f -d --label="{{ item.username }}" --qr-mode=ANSI -r 3 -R 30 -w 1 --secret=/home/"{{ item.username }}"/.google_authenticator', file: '/home/"{{{ item.username }}"/.google_authenticator' }
  with_items: "{{ users }}"
  become: true

vars/main.yml内部,我有:

代码语言:javascript
复制
---

users:
  username: user1 
  username: user2 
  username: user3

在运行该手册时,我将收到以下提示:

代码语言:javascript
复制
[WARNING]: While constructing a mapping from /Users/bshutter/Dev/server/roles/googlemfa/tasks/main.yml, line 55, column 3, found a duplicate dict key (with_items). Using last
defined value only.

[WARNING]: While constructing a mapping from /Users/bshutter/Dev/server/roles/googlemfa/vars/main.yml, line 4, column 3, found a duplicate dict key (username). Using last defined
value only.

当它在角色中运行并到达任务Generate a timed-based code for user

代码语言:javascript
复制
TASK [googlemfa : Generate a timed-based code for user] **************************************
fatal: [10.1.10.36]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'ansible.vars.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'file'\n\nThe error appears to have been in '/Users/bshutter/Dev/server/roles/googlemfa/tasks/main.yml': line 55, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Generate a timed-based code for user\n  ^ here\n"}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-17 23:06:10

我不确定我完全理解您的问题,因为看起来您只需要运行一种类型的命令。因此,如果您真的只有运行google身份验证器命令,我会这样做:

代码语言:javascript
复制
- name: Generate a timed-based code for user
  command: '/usr/bin/google-authenticator -t -f -d --label="{{item}}" --qr-mode=ANSI -r 3 -R 30 -w 1 --secret=/home/{{item}}' creates='/home/{{{ item }}/.google_authenticator'
  with_items: "{{ users }}"
  become: true
票数 2
EN

Stack Overflow用户

发布于 2017-05-18 02:39:34

你可能在找巢式

但是,使用创建选项将很难实现,因为只有其中一个命令将创建该文件。还请注意,循环必须是不同的--不能引用一个列表和另一个列表。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44033834

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档