【问题标题】:ansible regex_replace in command命令中的 ansible regex_replace
【发布时间】:2021-03-14 20:51:59
【问题描述】:

我正在使用 ansible 在 GCP 上部署文件存储,我需要从实例获取 IP 并使用它来创建挂载点。 gcloud 工作正常,但它是返回括号和带有 ip 的简单引用。 有人可以帮我删除这些字符吗?我的正则表达式命令不起作用,我是正则表达式的新手。

错误任务挂载无法解决来自 '' in ip.stdout

可靠的代码:

- name: get info
  shell: gcloud filestore instances describe "{{nfs_id}}" --project=xxxx-xxxx --zone=xxxxx-b --format='get(networks.ipAddresses)'
  register: ip


 - name: master_setup.yml --> DEBUG REGEX
   debug:
     var: "{{ 'ip.stdout' | regex_replace('([^\\.]*)\\.(.+)$', '\\1') }}"
    
- name: print mount point test
  debug:
    msg: "{{ip.stdout}}:/{{nfs_name }}"

- name:  Mount an NFS volume
  mount:
    fstype: nfs
    state: mounted
    opts: rw,sync,hard,intr
    src: "{{ip.stdout}:/{{nfs_name }}"
    path: /mnt/nexus-storage

ansible playbook 执行结果

TASK [install_nexus : master_setup.yml --> DEBUG REGEX] ********************************************************
ok: [nexus-xxxx.xxx.xxxxx] => {
    "ip": {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python3"
        },
        "changed": true,
        "cmd": "ggcloud filestore instances describe "{{nfs_id}}" --project=xxxx-xxxx --zone=xxxxx-b --format='get(networks.ipAddresses)'",
        "delta": "0:00:01.013823",
        "end": "2021-03-14 21:23:32.398266",
        "failed": false,
        "rc": 0,
        "start": "2021-03-14 21:23:31.384443",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "['1xx.xxx.xx.2']",
        "stdout_lines": [
            "['1xx.xxx.xx.2']"
        ]
    }
}

TASK [install_nexus : print mount point test] ******************************************************************
ok: [nexus-xxxx.xxx.xxxxx] => {
    "msg": "['1xx.xxx.xx.2']:/nfsnexusnew"
}

TASK [install_nexus : Mount an NFS volume] *********************************************************************
[WARNING]: sftp transfer mechanism failed on [nexus-ppd.preprod.d-aim.com]. Use ANSIBLE_DEBUG=1 to see detailed
information
fatal: [nexus-xxxx.xxx.xxxxx]: FAILED! => {"changed": false, "msg": "Error mounting /mnt/nexus-storage: mount.nfs: Failed to resolve server '1xx.xxx.xx.2': Name or service not known\n"}

谢谢

【问题讨论】:

    标签: regex linux ansible


    【解决方案1】:

    解决了这个问题,它不是很性感,但它正在工作。如果有人找到解决方案,请转发给我。 我使用了 2 个正则表达式,因为我不会在一行中删除简单的引号和括号:

    - name: get info
      shell: gcloud filestore instances describe "{{nfs_id}}" --project=xxxx-xxxx --zone=xxxxx-b --format='get(networks.ipAddresses)' > /tmp/nfs-ip.txt
    
    - name: sed regex to delete []
      shell: sed -i 's/[][]//g' /tmp/nfs-ip.txt
    
    - name: sed regex to delete ''
      shell: sed -i 's|["'\'']||g'  /tmp/nfs-ip.txt
    
    - name: register result in var ip
      shell: cat /tmp/nfs-ip.txt
      register: ip
    
    
    - name:  Mount an NFS volume
      mount:
        fstype: nfs
        state: mounted
        opts: rw,sync,hard,intr
        src: "{{ip.stdout}}:/{{nfs_name }}"
        path: /mnt/nexus-storage
    

    【讨论】:

      【解决方案2】:

      问:无法解析 ip.stdout

      A:ip.stdout中存储的值是一个字符串

         "ip": {
                ...
              "stdout": "['1xx.xxx.xx.2']",
                ...
         }
      

      使用过滤器 from_yamlfirst 来获取列表的第一项,例如

          src: "{{ ip.stdout|from_yaml|first }}:/{{ nfs_name }}"
      

      【讨论】:

      • 您好,亲爱的,谢谢,我会试试这个。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-29
      • 2019-04-01
      • 1970-01-01
      • 2019-11-21
      相关资源
      最近更新 更多