虽然以下命令在 shell 中输入时有效
echo -ne "myser\nmypass\n" | smbpasswd -a -s myuser
Run Code Online (Sandbox Code Playgroud)
以下任务在 ansible 中失败
- name: add dms samba user
command: echo -ne "myuser\nmypass\n" | smbpasswd -a -s myuser
notify: restart samba
Run Code Online (Sandbox Code Playgroud)
它不会产生任何错误,但不会创建用户。
与工作ansible 2.3.0.0
有关Ubuntu 16.0.4.
siw*_*wyd 10
如前所述,管道不适用于命令模块。我过去使用过类似的东西来创建 Samba 用户:
- name: Configure Samba users.
shell: >
(pdbedit --user={{ item.username }} 2>&1 > /dev/null)
|| (echo '{{ item.password }}'; echo '{{ item.password }}')
| smbpasswd -s -a {{ item.username }}
register: smbpasswd
changed_when: "'Added user' in smbpasswd.stdout"
with_items: "{{ samba_users }}"
loop_control:
label: "{{ item.username }}"
Run Code Online (Sandbox Code Playgroud)
只有当用户不存在时,任务才会运行。因此,更改密码不适用于此示例。
我稍微改进了siwyd和Tormod Macleod的代码。感谢你们俩!
- name: shell - create samba users
ansible.builtin.shell: >
set -e -o pipefail
&& (pdbedit --user={{ item.username }} 2>&1 > /dev/null)
|| (echo '{{ item.password }}'; echo '{{ item.password }}')
| smbpasswd -s -a {{ item.username }}
args:
executable: /bin/bash
register: samba_create_users
changed_when: "'Added user' in samba_create_users.stdout"
loop: "{{ samba_users }}"
no_log: true
- name: shell - set samba passwords correctly
ansible.builtin.shell: >
set -e -o pipefail
&& (smbclient -U {{ item.username }}%{{ item.password }} -L 127.0.0.1 2>&1 > /dev/null)
|| (echo '{{ item.password }}'; echo '{{ item.password }}')
| smbpasswd {{ item.username }}
args:
executable: /bin/bash
register: samba_verify_users
changed_when: "'New SMB password' in samba_verify_users.stdout"
loop: "{{ samba_users }}"
no_log: true
Run Code Online (Sandbox Code Playgroud)
变化:
归档时间: |
|
查看次数: |
4536 次 |
最近记录: |