AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 714403
Accepted
Dax
Dax
Asked: 2015-08-16 07:50:55 +0800 CST2015-08-16 07:50:55 +0800 CST 2015-08-16 07:50:55 +0800 CST

根据 Ansible 中的组更改要创建的用户的变量

  • 772

我有以下任务

- name: Create users
  user:
    uid: "{{ item.uid }}"
    name: "{{ item.username }}"
    comment: "{{ item.comment }}"
    shell: /bin/bash
    groups: "{{ item.groups }}"
  with_items: users

users包含在公司工作的所有用户,我想在 samba 服务器上创建所有用户。但是也有 3 个管理员,我只想在不属于 samba 组的其他服务器中创建它们。

如果我添加 when: item.username in admin_users,我可以使用上述创建管理员用户,但这将对所有用户都这样做,我希望新服务器的默认设置是只创建 admin_users,但如果服务器在 samba 组中,则创建所有用户。

会有一个简单的方法来做到这一点吗?我正在考虑将用户分成两组并通过运行两个任务来创建 admin_user 和 other_users,但我想知道是否有针对此问题的 DRY 解决方案。

linux
  • 1 1 个回答
  • 928 Views

1 个回答

  • Voted
  1. Best Answer
    yaegashi
    2015-08-16T19:02:43+08:002015-08-16T19:02:43+08:00

    剧本.yml

    ---
    - hosts: all
      gather_facts: no
      vars:
        users:
          - { uid: user1, username: username1, comment: comment1, shell: /bin/bash, groups: group1 }
          - { uid: user2, username: username2, comment: comment2, shell: /bin/bash, groups: group2 }
        admin_users: [ username1 ]
        users_admin:  |
          {%- set o=[] %}
          {%- for i in users %}
            {%- if i.username in admin_users %}
              {%- if o.append(i) %}
              {%- endif %}
            {%- endif %}
          {%- endfor %}
          {{ o }}
        users_filtered: "{{ users if 'samba' in group_names else users_admin }}"
      tasks:
        - debug:
            var: users_filtered
    

    主机

    host1 ansible_ssh_host=localhost
    [samba]
    host2 ansible_ssh_host=localhost
    

    示例会话:

    $ ansible-playbook -i hosts playbook.yml 
    
    PLAY [all] ******************************************************************** 
    
    TASK: [debug ] **************************************************************** 
    ok: [host1] => {
        "var": {
            "users_filtered": [
                {
                    "comment": "comment1", 
                    "groups": "group1", 
                    "shell": "/bin/bash", 
                    "uid": "user1", 
                    "username": "username1"
                }
            ]
        }
    }
    ok: [host2] => {
        "var": {
            "users_filtered": [
                {
                    "comment": "comment1", 
                    "groups": "group1", 
                    "shell": "/bin/bash", 
                    "uid": "user1", 
                    "username": "username1"
                }, 
                {
                    "comment": "comment2", 
                    "groups": "group2", 
                    "shell": "/bin/bash", 
                    "uid": "user2", 
                    "username": "username2"
                }
            ]
        }
    }
    
    PLAY RECAP ******************************************************************** 
    host1                      : ok=1    changed=0    unreachable=0    failed=0   
    host2                      : ok=1    changed=0    unreachable=0    failed=0   
    
    • 2

相关问题

  • Linux 主机到主机迁移

  • 如何在 Linux 机器上找到有关硬件的详细信息?

  • 如何在 Linux 下监控每个进程的网络 I/O 使用情况?

  • 在 RHEL4 上修改 CUPS 中的现有打印机设置

  • 为本地网络中的名称解析添加自定义 dns 条目

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve