gpt4 book ai didi

ansible 条件检查项目的属性是否存在

转载 作者:行者123 更新时间:2023-12-04 13:37:06 25 4
gpt4 key购买 nike

我想使用 ansible 创建用户并想设置他们的 shell 和 sudo 权限。

现在我有 vars/main.yml如下:

users: [{'name': 'user1', 'shell': '/bin/bash', 'sudo': 'user1 ALL=(ALL) NOPASSWD: ALL'}, {'name': 'user2', 'shell': '/bin/zsh', 'sudo': 'user2 ALL=NOPASSWD:/bin/systemctl *start nginx'}, {'name': 'user3', 'shell': '/bin/fish'}]

关于任务 Set sudo permission for users因为不是每个用户都有 sudo 权限,我需要检查 sudo属性是否存在。
- name: Set sudo permission for users
lineinfile:
dest: /etc/sudoers
state: present
regexp: '^{{ item.name }}'
line: "{{ item.sudo }}"
backup: true
when: "{{ item.sudo }}"
with_items:
- "{{ users }}"

我得到如下错误:
TASK [createUsers : Set sudo permission for users] ***************************
fatal: [ubuntu]: FAILED! => {"failed": true, "msg": "The conditional check '{{ item.sudo }}' failed. The error was: expected token 'end of statement block', got 'ALL'\n line 1\n\nThe error appears to have been in '/Users/csj/proj/roles/createUsers/tasks/main.yml': line 26, 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: Set sudo permission for users\n ^ here\n"}

我尝试了很多关于引用的东西,但没有帮助。

最佳答案

它应该工作

 when: item.sudo is defined

所以任务是
  - name: Set sudo permission for users
lineinfile:
dest: /etc/sudoers
state: present
regexp: '^{{ item.name }}'
line: "{{ item.sudo }}"
backup: true
when: item.sudo is defined
with_items:
- "{{ users }}"

关于ansible 条件检查项目的属性是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42599262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com