【发布时间】:2021-01-17 05:03:14
【问题描述】:
我使用的是 ansible 2.7 版。我正在练习一项任务,我必须添加用户名和 uid 存储在名为 /home/automation/plays/vars/user_list.yml 的 var 文件中,并且相同的密码存储在具有正确内容的 secrets.yml 中。我还可以使用 with_item 从其中添加用户,但下一个任务要求我需要在指定的主机组上添加其 uid 统计信息为 1* 的用户,该主机组使用“group_names”调用我正在尝试匹配正则表达式但得到错误
我的要求是将一个 udi 以 ^1 开头的用户添加到主机组“webserver”
我已经使用了 1201 / 1201 等显式 uid,它可以正常工作,但不能使用正则表达式
- 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"}
【问题讨论】:
标签: ansible