I have a customized requirement.
Check if user
tomuserbelongs to grouptomuser& exists no matter what the uid, gid is; then simply do nothing i.e. we are good.if group
tomuserdoes not exist create grouptomuserwithgid1900.if user
tomuserdoes not exist create usertomuserwithgid1900and assign in the the grouptomuser.Finally if
uid, gid1900is already in use while creating user and group then preferuid,gidas2020and if that too is in use then any random unique number is fine for both.
Below, is something I could think off which I understand is not the ideal solution; but i also end-up with issues
playbook below:
- 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
Output:
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"
Kindly suggest.
Got it. Should be idempotent as well.