v#invalid expression \ nsre_constants.error:错误的字符范围,Ansible

时间:2018-01-17 04:54:55

标签: python regex ansible ansible-2.x

我正在使用Ansible将内容写入文件,如下所示:

[localhost]
localhost

[es-master]

[es-data]

[es-client]

该剧是:

 - name: Debug the variable name
        debug:
              msg={{ ec2_details | type_debug }}

      - name: Add Instances to local inventory file
        local_action:
          lineinfile path="~/ansible-environments/hosts"
            regexp="^[es-master]"
            insertafter="^#?[es-master]"
            line="{{ item.private_ip }}"
            state="present"
        with_items: "{{ ec2_details.instances }}"

我可以看到结果是字典并且值存在但我无法选择特定键并将其写入文件。现在我不确定我是否正确使用了reg-ex,但这是我所看到的

完整的追溯是:

Traceback (most recent call last):
  File "/tmp/ansible_b91Bg5/ansible_module_lineinfile.py", line 461, in <module>
    main()
  File "/tmp/ansible_b91Bg5/ansible_module_lineinfile.py", line 453, in main
    ins_aft, ins_bef, create, backup, backrefs)
  File "/tmp/ansible_b91Bg5/ansible_module_lineinfile.py", line 251, in present
    bre_m = re.compile(to_bytes(regexp, errors='surrogate_or_strict'))
  File "/usr/lib/python2.7/re.py", line 194, in compile
    return _compile(pattern, flags)
  File "/usr/lib/python2.7/re.py", line 251, in _compile
    raise error, v # invalid expression
sre_constants.error: bad character range

failed: [localhost -> localhost] (item={u'kernel': None, u'root_device_type': u'ebs', u'private_dns_name': u'ip-172-16-9-68.ap-southeast-1.compute.internal', u'public_ip': None, u'private_ip': u'172.16.9.68', u'id': u'i-0d4310cf82c52fd0b', u'ebs_optimized': False, u'state': u'running', u'virtualization_type': u'hvm', u'architecture': u'x86_64', u'ramdisk': None, u'block_device_mapping': {u'/dev/sda1': {u'status': u'attached', u'delete_on_termination': True, u'volume_id': u'vol-04be65eead21e611f'}}, u'key_name': u'ansible-managed', u'image_id': u'ami-325d2e4e', u'tenancy': u'default', u'groups': {u'sg-1e3c9d79': u'elasticsearch-mayassar-production', u'sg-86018ce2': u'mayassar-es-external'}, u'public_dns_name': u'', u'state_code': 16, u'tags': {u'Name': u'Kill-master-es'}, u'placement': u'ap-southeast-1b', u'ami_launch_index': u'0', u'dns_name': u'', u'region': u'ap-southeast-1', u'launch_time': u'2018-01-17T04:49:28.000Z', u'instance_type': u'm4.4xlarge', u'root_device_name': u'/dev/sda1', u'hypervisor': u'xen'}) => {
    "changed": false, 
    "failed": true, 
    "item": {
        "ami_launch_index": "0", 
        "architecture": "x86_64", 
        "block_device_mapping": {
            "/dev/sda1": {
                "delete_on_termination": true, 
                "status": "attached", 
                "volume_id": "vol-04be65eead21e611f"
            }
        }, 
        "dns_name": "", 
        "ebs_optimized": false, 
        "groups": {
            "sg-1e3c9d79": "elasticsearch-mayassar-production", 
            "sg-86018ce2": "mayassar-es-external"
        }, 
        "hypervisor": "xen", 
        "id": "i-0d4310cf82c52fd0b", 
        "image_id": "ami-325d2e4e", 
        "instance_type": "m4.4xlarge", 
        "kernel": null, 
        "key_name": "ansible-managed", 
        "launch_time": "2018-01-17T04:49:28.000Z", 
        "placement": "ap-southeast-1b", 
        "private_dns_name": "ip-172-16-9-68.ap-southeast-1.compute.internal", 
        "private_ip": "172.16.9.68", 
        "public_dns_name": "", 
        "public_ip": null, 
        "ramdisk": null, 
        "region": "ap-southeast-1", 
        "root_device_name": "/dev/sda1", 
        "root_device_type": "ebs", 
        "state": "running", 
        "state_code": 16, 
        "tags": {
            "Name": "Kill-master-es"
        }, 
        "tenancy": "default", 
        "virtualization_type": "hvm"
    }, 
    "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_b91Bg5/ansible_module_lineinfile.py\", line 461, in <module>\n    main()\n  File \"/tmp/ansible_b91Bg5/ansible_module_lineinfile.py\", line 453, in main\n    ins_aft, ins_bef, create, backup, backrefs)\n  File \"/tmp/ansible_b91Bg5/ansible_module_lineinfile.py\", line 251, in present\n    bre_m = re.compile(to_bytes(regexp, errors='surrogate_or_strict'))\n  File \"/usr/lib/python2.7/re.py\", line 194, in compile\n    return _compile(pattern, flags)\n  File \"/usr/lib/python2.7/re.py\", line 251, in _compile\n    raise error, v # invalid expression\nsre_constants.error: bad character range\n", 
    "module_stdout": "", 
    "msg": "MODULE FAILURE", 
    "rc": 0
}

请帮我调试这部分代码,非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

  

7.2.1. Regular Expression Syntax

     

[]有些字符,例如&#39; |&#39;或者&#39; (&#39;,很特别。特殊字符或者代表普通字符的类,或者影响它们周围的正则表达式的解释方式。 []

     

特殊字符是:[]

     

[]用于表示一组字符。

因此,您无法使用^[es-master]来匹配[es-master],因为[] 是特殊字符

你需要逃脱它们。