我有一个定制的要求。
无论uid,gid是什么,检查用户是否
tomuser
属于组并且存在;tomuser
然后什么也不做,即我们很好。如果组
tomuser
不存在,则tomuser
使用. 创建组gid
1900
。如果用户
tomuser
不存在创建用户tomuser
并gid
1900
在组中分配tomuser
。最后,如果
uid, gid
1900
在创建用户和组时已经在使用,那么更喜欢uid,gid
as2020
并且如果它也在使用中,那么任何随机唯一数字都适用于两者。
下面是我能想到的,我理解这不是理想的解决方案;但我也遇到了问题
剧本如下:
- name: Check tomuser user in passwd file
tags: always
ignore_errors: yes
block:
- group:
name: tomuser
gid: "{{ item }}"
loop:
- "1900"
- "2020"
register: groupcreated
when: "tomuser" in groups
- debug:
msg: "GROUP tomuser does not exists or is empty"
when: 'tomuser' not in groups and not groups['tomuser']
- debug:
msg: "GROUP tomuser does not exists"
when: 'tomuser' not in groups
- debug:
msg: "GROUP tomuser is empty"
when: not groups['tomuser']
- raw: "cat /etc/passwd |grep -i tomuser"
register: tomusercheck
输出:
TASK [Check tomcat USER on server] *************************************************************************************************************************************
task path: /app/patch/patch.yml:81
fatal: [10.9.9.44]: FAILED! => {
"reason": "Syntax Error while loading YAML.\n did not find expected key\n\nThe error appears to be in '/app/patch/checktomuser.yml': line 11, column 30, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n gid: '1900'\n when: \"tomuser\" in groups\n ^ here\nThis one looks easy to fix. It seems that there is a value started\nwith a quote, and the YAML parser is expecting to see the line ended\nwith the same kind of quote. For instance:\n\n when: \"ok\" in result.stdout\n\nCould be written as:\n\n when: '\"ok\" in result.stdout'\n\nOr equivalently:\n\n when: \"'ok' in result.stdout\"\n"
请建议。