【问题标题】:ansible-playbook unable to use regexansible-playbook 无法使用正则表达式
【发布时间】: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


    【解决方案1】:

    假设用户已经创建并且 “the requirement is to add users whose uid begins with '1' to group 'webserver'”。

    下面的任务完成了这项工作。

    - name: Adding users to webserver group
      user:
        name: "{{ item.username }}"
        groups: webserver
      loop: "{{ users }}"
      when:  item.uid|string|first == '1'
    

    正则表达式

    1) 下面的条件是等价的

      when: item.uid|string is match('^1.*$')
    

    2) 要匹配用户“where userid ends with 5”,请使用

      when: item.uid|string is match('^.*5$')
    

    【讨论】:

    • 谢谢弗拉基米尔,但我最初的问题是关于正则表达式的使用如何在这说我有另一个任务,其中用户 ID 以 5 结尾呢!
    • 感谢 Vladimir 下面的行帮助了我
    • when: ( 'webserver' in group_names and item.uid | string is search("^1.*"))
    【解决方案2】:

    In the official documentation for Ansible 2.7,没有对regex 过滤器的引用。它只出现在documentation for Ansible 2.8 中,所以我认为它是该版本的新增内容。

    根据同一文档,regex("1.*") 不会匹配以“1”开头的 UID,而是匹配所有包含“1”的 UID。您将希望改用 match("1.*"),这应该适用于您的 Ansible 版本。

    【讨论】:

    • 谢谢@kevin 下面的行已经为我工作了,我们需要在评估之前将变量转换为字符串
    • when: ( 'webserver' in group_names and item.uid | string is search("^1.*"))
    【解决方案3】:

    有了这个任务。

    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'"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-13
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多