首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ansible:更改嵌套vars的默认值

Ansible:更改嵌套vars的默认值
EN

Stack Overflow用户
提问于 2022-06-16 11:09:46
回答 3查看 181关注 0票数 2

我写了一个角色来创建用户。现在,我想修改角色,让它也可以删除用户。一开始看起来很简单的是,现在让我头痛不已。

以下是我的角色:

代码语言:javascript
复制
---
- name: Dictionary playbook example
  hosts: localhost
  vars:
    my_default_values:
        users: &def
            state: 'present'
            force: 'yes'
            remove: 'no'
    my_values:
        users:
            - username: bla
              <<: *def
              state: absent
            - username: bla2
              <<: *def

  tasks:
    - debug: var=my_default_values
    - debug: var=my_values

这是输出:

代码语言:javascript
复制
PLAY [Dictionary playbook example] ********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
        
        TASK [Gathering Facts] ********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
        ok: [localhost]
        
        TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
        ok: [localhost] => {
            "my_default_values": {
                "users": {
                    "force": "yes",
                    "remove": "no",
                    "state": "present"
                }
            }
        }
        
        TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
        ok: [localhost] => {
            "my_values": {
                "users": [
                    {
                        "force": "yes",
                        "remove": "no",
                        "state": "absent",
                        "username": "bla"
                    },
                    {
                        "force": "yes",
                        "remove": "no",
                        "state": "present",
                        "username": "bla2"
                    }
                ]
            }
        }
        
PLAY RECAP ********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0
rescued=0    ignored=0

只要我总是在我的角色中的每个<<: *def下面定义- username,它就能正常工作。但是,如果我想在外部文件users中定义users.yml变量,这是有问题的。我想我需要某种循环?

最后,我希望在Katello中使用这个角色,这样用户就可以在Katello内部定义。这就是为什么我需要让它以一种更动态的方式工作。

EN

回答 3

Stack Overflow用户

发布于 2022-06-16 11:52:16

我可以看到两种方法:

如果您计划对这些用户进行循环并使用字典的值,那么只需使用指定的过滤器定义适当的默认值即可。

角色/演示/任务/main.yml

代码语言:javascript
复制
- debug:
    msg:
      user:
        username: "{{ item.username }}"
        state: "{{ item.state | default('present') }}"
        force: "{{ item.force | default('yes') }}"
        remove: "{{ item.remove | default('no') }}"
  loop: "{{ users }}"
  loop_control:
    label: "{{ item.username }}"

剧本:

代码语言:javascript
复制
- hosts: localhost
  gather_facts: no

  roles:
    - name: demo
      users:
        - username: bla
          state: absent
        - username: bla2

这将产生:

代码语言:javascript
复制
ok: [localhost] => (item=bla) => 
  msg:
    user:
      force: 'yes'
      remove: 'no'
      state: absent
      username: bla
ok: [localhost] => (item=bla2) => 
  msg:
    user:
      force: 'yes'
      remove: 'no'
      state: present
      username: bla2

如果确实需要角色内部的列表,请重新创建一个新列表,将默认值与提供给角色的输入组合起来,同时利用以下事实:当组合两个包含相同键的字典时,第二个字典的值将覆盖第一个字典的值:

角色/演示/任务/main.yml

代码语言:javascript
复制
- set_fact:
    users_with_default: >-
      {{
        users_with_default | default([])
        + [user_defaults | combine(item)]
      }}
  loop: "{{ users }}"
  vars:
    user_defaults:
      state: 'present'
      force: 'yes'
      remove: 'no'

- debug:
    var: users_with_default

剧本:

代码语言:javascript
复制
- hosts: localhost
  gather_facts: no

  roles:
    - name: demo
      users:
        - username: bla
          state: absent
        - username: bla2

这将产生:

代码语言:javascript
复制
ok: [localhost] => 
  users_with_default:
  - force: 'yes'
    remove: 'no'
    state: absent
    username: bla
  - force: 'yes'
    remove: 'no'
    state: present
    username: bla2
票数 1
EN

Stack Overflow用户

发布于 2022-06-16 16:26:13

使用https://docs.ansible.com/ansible/latest/user_guide/playbooks_module_defaults.html#module-defaults。没有必要包括状态:目前删除:否,因为它们是默认的。在列表中,my_users只包括必需的参数名称和非默认值的参数。

代码语言:javascript
复制
- hosts: localhost
  module_defaults:
    ansible.builtin.user:
      force: yes
  vars:
    my_users:
      - name: foo
        state: absent
      - name: bar

在该任务中,默认情况下,要使用的https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#making-variables-optional可选参数

代码语言:javascript
复制
  tasks:
    - ansible.builtin.user:
        name: "{{ item.name }}"
        state: "{{ item.state|default(omit) }}"
        force: "{{ item.force|default(omit) }}"
        remove: "{{ item.remove|default(omit) }}"
      loop: "{{ my_users }}"
票数 1
EN

Stack Overflow用户

发布于 2022-06-20 11:37:10

我的角色终于起作用了。也许这不是最好的解决方案,但对于我想要实现的目标来说,它确实很有效:

代码语言:javascript
复制
---
# tasks file for securehost

- set_fact:
    users_with_default: >-
      {{
        users_with_default | default([])
        + [user_defaults | combine(item)]
      }}
  loop: "{{ users }}"
  vars:
    user_defaults:
      state: 'present'
      force: 'yes'
      remove: 'no'

- name: Allow 'wheel' group to have passwordless sudo
  lineinfile:
    dest: /etc/sudoers
    state: present
    regexp: '^%wheel'
    line: '%wheel ALL=(ALL) NOPASSWD: ALL'
    validate: 'visudo -cf %s'

- name: create base homedir /lhome with mode 775
  file:
    path: /lhome
    state: directory
    mode: "u=rwx,g=rx,o=rx"

- name: Add / Remove users
  environment: umask=077
  user: name={{ item.username }} uid={{ item.uid }} home="/lhome/{{ item.username }}" groups={{ item.groups }} state={{ item.state }} remove={{ item.remove }} force={{ item.force }}
  with_items: "{{ users_with_default }}"

- name: change mode of all homedirs under /lhome to mode 0700
  file:
    path: "/lhome/{{ item.username }}"
    state: directory
    mode: 0700
  with_items: "{{ users_with_default }}"
  when:
    - item.state != 'absent'

- name: Add authorized keys for users
  authorized_key: user={{ item.username }} key="{{ item.key }}" exclusive={{ pubkey_exclusive }}
  with_items: "{{ users_with_default }}"
  when:
    - item.state != 'absent'

- name: Change root password
  user:
   name: root
   password: "{{ root_password_hash }}"
  when: change_root_pw | bool

再次感谢你在这方面的快速帮助。

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

https://stackoverflow.com/questions/72644875

复制
相关文章

相似问题

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