Reputation: 21
I am using ansible version 2.7. I was practicing a task where I have to add users whose username and uid stored in var file named /home/automation/plays/vars/user_list.yml and password for the same is stored in secrets.yml having correct content. I am also able to add user from it with with_item but with next requirement of task I need to add users whose uid stats with 1* on specified hostgroup which is called with "group_names" i was trying match regex for the same but getting error
My requirement is add a user whose udi begins with ^1 to hostgroup "webserver"
I have used with explict uids such 1201 / 1201 which is working but not working with regex
- hosts: all
become: yes
gather_facts: no
vars_files:
- /home/automation/plays/vars/user_list.yml
- secrets.yml
tasks:
- debug:
msg: "{{users | type_debug}},{{user_password}}"
with_items: "{{ users }}"
- name: Adding Users to webserver groupwith List
user:
name: "{{item.username}}"
uid: "{{item.uid}}"
state: absent
remove: yes
password: "{{ user_password }}"
shell: /bin/bash
groups: wheel
generate_ssh_key: yes
ssh_key_bits: 2048
ssh_key_file: .ssh/id_rsa
when: item.uid is regex("1.*")
with_items: "{{users}}"
fatal: [ansnode_1]: FAILED! => {"msg": "The conditional check 'item.uid is regex(\"1.*\")' failed. The error was: Unexpected templating type error occurred on ({% if item.uid is regex(\"1.*\") %} True {% else %} False {% endif %}): expected string or buffer\n\nThe error appears to have been in '/home/automation/plays/users.yml': line 12, column 8, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n with_items: \"{{ users }}\"\n - name: Adding Users to webserver groupwith List\n ^ here\n"}
fatal: [ansnode_4]: FAILED! => {"msg": "The conditional check 'item.uid is regex(\"1.*\")' failed. The error was: Unexpected templating type error occurred on ({% if item.uid is regex(\"1.*\") %} True {% else %} False {% endif %}): expected string or buffer\n\nThe error appears to have been in '/home/automation/plays/users.yml': line 12, column 8, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n with_items: \"{{ users }}\"\n - name: Adding Users to webserver groupwith List\n ^ here\n"}
fatal: [ansnode_2]: FAILED! => {"msg": "The conditional check 'item.uid is regex(\"1.*\")' failed. The error was: Unexpected templating type error occurred on ({% if item.uid is regex(\"1.*\") %} True {% else %} False {% endif %}): expected string or buffer\n\nThe error appears to have been in '/home/automation/plays/users.yml': line 12, column 8, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n with_items: \"{{ users }}\"\n - name: Adding Users to webserver groupwith List\n ^ here\n"}
fatal: [ansnode_3]: FAILED! => {"msg": "The conditional check 'item.uid is regex(\"1.*\")' failed. The error was: Unexpected templating type error occurred on ({% if item.uid is regex(\"1.*\") %} True {% else %} False {% endif %}): expected string or buffer\n\nThe error appears to have been in '/home/automation/plays/users.yml': line 12, column 8, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n with_items: \"{{ users }}\"\n - name: Adding Users to webserver groupwith List\n ^ here\n"}
Upvotes: 1
Views: 2662
Reputation: 68074
Let's assume the users have already been created and
"the requirement is to add users whose uid begins with '1' to group 'webserver'
".
The task below does the job
- name: Adding users to webserver group
user:
name: "{{ item.username }}"
groups: webserver
loop: "{{ users }}"
when: item.uid|string|first == '1'
Regex
when: item.uid|string is match('^1.*$')
where userid ends with 5
" use when: item.uid|string is match('^.*5$')
Upvotes: 2
Reputation: 1
With this task.
tasks:
- name: Create users
user:
name: "{{ item.username }}"
group: wheel
password: "{{ user_password | password_hash('sha512') }}"
shell: /bin/bash
update_password: on_create
with_items: "{{ users }}"
when:
- ansible_fqdn in groups['webservers']
- "item.uid|string|first == '1'"
Upvotes: 0
Reputation: 338
In the official documentation for Ansible 2.7, there is no reference to the regex
filter. It appears only in the documentation for Ansible 2.8, so I assume it is a new addition of that version.
According to that same documentation, regex("1.*")
will not match the UIDs starting with "1", but all the UIDs containing "1". You will want to use match("1.*")
instead, that should work with your version of Ansible.
Upvotes: 0