由于变量格式问题,无法获取 Ansible 中现有目录的统计数据

问题描述 投票:0回答:1

我正在尝试将第一个目录的所有者传递给 Ansible 变量

source_files

我们无法控制 Ansible 变量

source_files
的内容,因为它是通过某些 OLAM API 构建的。我已经为此示例对其进行了硬编码。

source_files
变量的两个文件都存在,如下所示:

[wluser@mylocalhost ~]$ ls -ltr '/tmp/my  private.txt' /tmp/files.sh  
-rw-r--r-- 1 wluser mygrp 0 Nov 12 21:52 /tmp/files.sh    
-rw-r--r-- 1 wluser mygrp 0 Nov 12 21:52 /tmp/my  private.txt

剧本

[wluser@mylocalhost ~]$ cat formatstring.yml

---
- name: "Play 1-Find the details here"

  hosts: localhost
  any_errors_fatal: True
  gather_facts: no

  tasks:

   - name: Set source_files variable
     set_fact:
       source_files: "'/tmp/my  private.txt','/tmp/files.sh'"
 
   - name: Print source_files
     debug:
       msg: "source_files: {{ source_files }}"
     
   - name: Loop Print source_files
     debug:
       msg: "{{ item }}"
     loop: "{{ source_files.split(',') }}"

   - name: Single Print source_files
     debug:
       msg: "{{ source_files.split(',')[0] }}"
   
   - name: Get directory owner
     ansible.builtin.stat:
       path: "{{ tomcat_home_item }}"
     register: dir_stat
     vars:
       tomcat_home_item: "{{ source_files.split(',')[0] | trim }}"

   - name: Store directory owner's UID in a variable
     set_fact:
       owner_uid: "{{ dir_stat.stat.pw_name | default('baduser') }}"

   - debug:
       msg: "Detected user is: {{ owner_uid }}"

   - name: Get directory owner
     ansible.builtin.stat:
       path: "{{ tomcat_home_item | regex_replace('^\\s*'|'\\s*$', '') }}"
     register: dir_stat
     vars:
       tomcat_home_item: "{{ source_files.split(',')[0] | trim }}"

   - name: Store directory owner's UID in a variable
     set_fact:
       owner_uid: "{{ dir_stat.stat.pw_name | default('baduser') }}"
 
   - debug:
       msg: "Detected user is: {{ owner_uid }}"

输出

[wluser@mylocalhost ~]$ ansible-playbook formatstring.yml -vvv
ansible-playbook 2.8.4
  config file = /etc/ansible/ansible.cfg
PLAYBOOK: formatstring.yml ****************************************************************************************************
1 plays in formatstring.yml

PLAY [Play 1-Find the details here] *******************************************************************************************
META: ran handlers

TASK [Set source_files variable] **********************************************************************************************
task path: /home/wluser/formatstring.yml:10
Sunday 12 November 2023  22:02:56 -0600 (0:00:00.044)       0:00:00.044 *******
ok: [localhost] => {
    "ansible_facts": {
        "source_files": "'/tmp/my  private.txt','/tmp/files.sh'"
    },
    "changed": false
}

TASK [Print source_files] *****************************************************************************************************
task path: /home/wluser/formatstring.yml:15
Sunday 12 November 2023  22:02:56 -0600 (0:00:00.018)       0:00:00.062 *******
ok: [localhost] => {
    "msg": "source_files: '/tmp/my  private.txt','/tmp/files.sh'"
}

TASK [Loop Print source_files] ************************************************************************************************
task path: /home/wluser/formatstring.yml:19
Sunday 12 November 2023  22:02:56 -0600 (0:00:00.017)       0:00:00.080 *******
ok: [localhost] => (item='/tmp/my  private.txt') => {
    "msg": "'/tmp/my  private.txt'"
}
ok: [localhost] => (item='/tmp/files.sh') => {
    "msg": "'/tmp/files.sh'"
}

TASK [Single Print source_files] **********************************************************************************************
task path: /home/wluser/formatstring.yml:24
Sunday 12 November 2023  22:02:56 -0600 (0:00:00.033)       0:00:00.114 *******
ok: [localhost] => {
    "msg": "'/tmp/my  private.txt'"
}

TASK [Get directory owner] ****************************************************************************************************
task path: /home/wluser/formatstring.yml:29
Sunday 12 November 2023  22:02:56 -0600 (0:00:00.023)       0:00:00.137 *******
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: wluser
ok: [localhost] => {
    "changed": false,
    "invocation": {
        "module_args": {
            "checksum_algorithm": "sha1",
            "follow": false,
            "get_attributes": true,
            "get_checksum": true,
            "get_md5": null,
            "get_mime": true,
            "path": "'/tmp/my  private.txt'"
        }
    },
    "stat": {
        "exists": false
    }
}

TASK [Store directory owner's UID in a variable] ******************************************************************************
task path: /home/wluser/formatstring.yml:36
Sunday 12 November 2023  22:02:57 -0600 (0:00:00.352)       0:00:00.489 *******
ok: [localhost] => {
    "ansible_facts": {
        "owner_uid": "baduser"
    },
    "changed": false
}

TASK [debug] ******************************************************************************************************************
task path: /home/wluser/formatstring.yml:40
Sunday 12 November 2023  22:02:57 -0600 (0:00:00.020)       0:00:00.510 *******
ok: [localhost] => {
    "msg": "Detected user is: baduser"
}

TASK [Get directory owner] ****************************************************************************************************
task path: /home/wluser/formatstring.yml:43
Sunday 12 November 2023  22:02:57 -0600 (0:00:00.019)       0:00:00.529 *******
fatal: [localhost]: FAILED! => {
    "msg": "template error while templating string: expected token 'name', got 'string'. String: {{ tomcat_home_item | regex_replace('^\\\\s*'|'\\\\s*$', '') }}"
}

NO MORE HOSTS LEFT ************************************************************************************************************
PLAY RECAP ********************************************************************************************************************
localhost                  : ok=7    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

如您所见,由于变量

baduser
的引用/格式问题,我得到的是
wluser
而不是正确的用户
source_files

然后我尝试删除引号,但这导致了运行时错误。

考虑到剧本中最初提到的 source_files 的格式无法更改,您能否建议我如何获取文件的统计信息。

ansible runtime-error parameter-passing string-formatting stat
1个回答
0
投票

考虑到

source_files

的格式,如何获取文件的统计信息

假设

ls -ltr
命令的输出是正确的,
source_files
的内容也是正确的。对于文件

~/test$ ls -ltr 'test file' 'test path' 'test path/test file'
-rw-r--r--. 1 ansible_user users 0 Nov 13 08:00 test file
-rw-r--r--. 1 ansible_user users 0 Nov 13 08:00 test path/test file

test path:
total 0
-rw-r--r--. 1 ansible_user users 0 Nov 13 08:00 test file

一个最小的示例手册


```---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    FILE: 'test file'
    PATH: 'test path'

  tasks:

  - set_fact:
      SOURCE_FILES: "'test file','test path/test file'"

  - stat:
      path: 'test file'
    register: result

  - debug:
      msg: "{{ result.stat.path }}"

  - stat:
      path: "{{ FILE }}"
    register: result

  - debug:
      msg: "{{ result.stat.path }}"

  - stat:
      path: "{{ PATH }}/{{ FILE }}"
    register: result

  - debug:
      msg: "{{ result.stat.path }}"

  - debug:
      msg:
        - "Content: {{ SOURCE_FILES }}"
        - "Type: {{ SOURCE_FILES | type_debug }}"
        - "List of files: {{ SOURCE_FILES | split(',') }}"

  - stat:
      path: "{{ SOURCE_FILES | split(',') | first | replace(\"'\",'') }}"
    register: result

  - debug:
      msg: "{{ result.stat.path }}"

  - stat:
      path: "{{ SOURCE_FILES | split(',') | last | replace(\"'\",'') }}"
    register: result

  - debug:
      msg: "{{ result.stat.path }}"

将产生

的输出
TASK [stat] ******************************************************
ok: [localhost]

TASK [debug] *****************************************************
ok: [localhost] =>
  msg: test file

TASK [stat] ******************************************************
ok: [localhost]

TASK [debug] *****************************************************
ok: [localhost] =>
  msg: test file

TASK [stat] ******************************************************
ok: [localhost]

TASK [debug] *****************************************************
ok: [localhost] =>
  msg: test path/test file

TASK [debug] *****************************************************
ok: [localhost] =>
  msg:
  - 'Content: ''test file'',''test path/test file'''
  - 'Type: AnsibleUnicode'
  - 'List of files: [u"''test file''", u"''test path/test file''"]'

TASK [stat] ******************************************************
ok: [localhost]

TASK [debug] *****************************************************
ok: [localhost] =>
  msg: test file

TASK [stat] ******************************************************
ok: [localhost]

TASK [debug] *****************************************************
ok: [localhost] =>
  msg: test path/test file

如果

source_files
的内容是一个逗号分隔值 (CSV) 字符串,具有显式引用值和引号字符
single quote (')
(...注释:它使非 RFC 符合),以制作再次显示文件名列表

  • 通过
    ,
     按分隔符逗号 (
    split(',')
  • ) 进行拆分
  • 通过
    single quote (')
     删除所有值的显式引号字符 
    replace(\"'\",'')

请注意文件不能再包含单引号,否则会再次出现错误。在示例中它将失败

~/test$ touch "andrew's.file"
~/test$ ls -al and*
-rw-r--r--. 1 ansible_user users 0 Nov 13 09:30 andrew's.file
-rw-r--r--. 1 ansible_user users 0 Nov 13 09:30 and.yml

感谢

© www.soinside.com 2019 - 2024. All rights reserved.