我写了一个角色来创建用户。现在,我想修改角色,让它也可以删除用户。一开始看起来很简单的是,现在让我头痛不已。
以下是我的角色:
---
- 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这是输出:
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内部定义。这就是为什么我需要让它以一种更动态的方式工作。
发布于 2022-06-16 11:52:16
我可以看到两种方法:
如果您计划对这些用户进行循环并使用字典的值,那么只需使用指定的过滤器定义适当的默认值即可。
角色/演示/任务/main.yml
- 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 }}"剧本:
- hosts: localhost
gather_facts: no
roles:
- name: demo
users:
- username: bla
state: absent
- username: bla2这将产生:
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
- 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剧本:
- hosts: localhost
gather_facts: no
roles:
- name: demo
users:
- username: bla
state: absent
- username: bla2这将产生:
ok: [localhost] =>
users_with_default:
- force: 'yes'
remove: 'no'
state: absent
username: bla
- force: 'yes'
remove: 'no'
state: present
username: bla2发布于 2022-06-16 16:26:13
使用https://docs.ansible.com/ansible/latest/user_guide/playbooks_module_defaults.html#module-defaults。没有必要包括状态:目前和删除:否,因为它们是默认的。在列表中,my_users只包括必需的参数名称和非默认值的参数。
- 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可选参数
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 }}"发布于 2022-06-20 11:37:10
我的角色终于起作用了。也许这不是最好的解决方案,但对于我想要实现的目标来说,它确实很有效:
---
# 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再次感谢你在这方面的快速帮助。
https://stackoverflow.com/questions/72644875
复制相似问题