我想使用 ansible 在我的标准输出中打印特定的行/值

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

在我的剧本中,我运行 gitlab-rake backup 命令按预期工作。现在我想打印这个示例的输出“1706574679_2024_01_29_16.4.5_gitlab_backup.tar”,但我正在努力寻找如何打印它的方法。

这是我的剧本:

tasks:
     - name: GitLab rake backup
       become: yes
       shell: "gitlab-backup create SKIP=repositories,db,uploads"
       register: gitlab_backup

     - debug: "msg={{gitlab_backup.stdout | regex_search ('Creating backup archive:'.'*') }}"

这是备份完成时的输出:

TASK [GitLab rake backup] ******************************************************************************************************************************************************************
changed: [cdopla-538.bell.corp.bce.ca] => {"changed": true, "cmd": "gitlab-backup create SKIP=repositories,db,uploads", "delta": "0:03:30.216704", "end": "2024-01-29 19:34:13.442518", "msg": "", "rc": 0, "start": "2024-01-29 19:30:43.225814", "stderr": "", "stderr_lines": [], "stdout": "2024-01-29 19:31:19 -0500 -- Dumping database ... [SKIPPED]\n2024-01-29 19:31:19 -0500 -- Dumping repositories ... [SKIPPED]\n2024-01-29 19:31:19 -0500 -- Dumping uploads ... [SKIPPED]\n2024-01-29 19:31:19 -0500 -- Dumping builds ... \n2024-01-29 19:31:22 -0500 -- Dumping builds ... done\n2024-01-29 19:31:22 -0500 -- Dumping artifacts ... \n2024-01-29 19:33:34 -0500 -- Dumping artifacts ... done\n2024-01-29 19:33:34 -0500 -- Dumping pages ... \n2024-01-29 19:33:34 -0500 -- Dumping pages ... done\n2024-01-29 19:33:34 -0500 -- Dumping lfs objects ... \n2024-01-29 19:34:04 -0500 -- Dumping lfs objects ... done\n2024-01-29 19:34:04 -0500 -- Dumping terraform states ... \n2024-01-29 19:34:04 -0500 -- Dumping terraform states ... done\n2024-01-29 19:34:04 -0500 -- Dumping container registry images ... [DISABLED]\n2024-01-29 19:34:04 -0500 -- Dumping packages ... \n2024-01-29 19:34:05 -0500 -- Dumping packages ... done\n2024-01-29 19:34:05 -0500 -- Dumping ci secure files ... \n2024-01-29 19:34:05 -0500 -- Dumping ci secure files ... done\n2024-01-29 19:34:05 -0500 -- Creating backup archive: 1706574679_2024_01_29_16.4.5_gitlab_backup.tar ... \n2024-01-29 19:34:13 -0500 -- Creating backup archive: 1706574679_2024_01_29_16.4.5_gitlab_backup.tar ... done\n2024-01-29 19:34:13 -0500 -- Uploading backup archive to remote storage  ... [SKIPPED]\n2024-01-29 19:34:13 -0500 -- Deleting old backups ... [SKIPPED]\n2024-01-29 19:34:13 -0500 -- Deleting tar staging files ... \n2024-01-29 19:34:13 -0500 -- Cleaning up /app/gitlab/backups/backup_information.yml\n2024-01-29 19:34:13 -0500 -- Cleaning up /app/gitlab/backups/builds.tar.gz\n2024-01-29 19:34:13 -0500 -- Cleaning up /app/gitlab/backups/artifacts.tar.gz\n2024-01-29 19:34:13 -0500 -- Cleaning up /app/gitlab/backups/pages.tar.gz\n2024-01-29 19:34:13 -0500 -- Cleaning up /app/gitlab/backups/lfs.tar.gz\n2024-01-29 19:34:13 -0500 -- Cleaning up /app/gitlab/backups/terraform_state.tar.gz\n2024-01-29 19:34:13 -0500 -- Cleaning up /app/gitlab/backups/packages.tar.gz\n2024-01-29 19:34:13 -0500 -- Cleaning up /app/gitlab/backups/ci_secure_files.tar.gz\n2024-01-29 19:34:13 -0500 -- Deleting tar staging files ... done\n2024-01-29 19:34:13 -0500 -- Deleting backups/tmp ... \n2024-01-29 19:34:13 -0500 -- Deleting backups/tmp ... done\n2024-01-29 19:34:13 -0500 -- Warning: Your gitlab.rb and gitlab-secrets.json files contain sensitive data \nand are not included in this backup. You will need these files to restore a backup.\nPlease back them up manually.\n2024-01-29 19:34:13 -0500 -- Backup 1706574679_2024_01_29_16.4.5 is done.\n2024-01-29 19:34:13 -0500 -- Deleting backup and restore PID file ... done", "stdout_lines": ["2024-01-29 19:31:19 -0500 -- Dumping database ... [SKIPPED]", "2024-01-29 19:31:19 -0500 -- Dumping repositories ... [SKIPPED]", "2024-01-29 19:31:19 -0500 -- Dumping uploads ... [SKIPPED]", "2024-01-29 19:31:19 -0500 -- Dumping builds ... ", "2024-01-29 19:31:22 -0500 -- Dumping builds ... done", "2024-01-29 19:31:22 -0500 -- Dumping artifacts ... ", "2024-01-29 19:33:34 -0500 -- Dumping artifacts ... done", "2024-01-29 19:33:34 -0500 -- Dumping pages ... ", "2024-01-29 19:33:34 -0500 -- Dumping pages ... done", "2024-01-29 19:33:34 -0500 -- Dumping lfs objects ... ", "2024-01-29 19:34:04 -0500 -- Dumping lfs objects ... done", "2024-01-29 19:34:04 -0500 -- Dumping terraform states ... ", "2024-01-29 19:34:04 -0500 -- Dumping terraform states ... done", "2024-01-29 19:34:04 -0500 -- Dumping container registry images ... [DISABLED]", "2024-01-29 19:34:04 -0500 -- Dumping packages ... ", "2024-01-29 19:34:05 -0500 -- Dumping packages ... done", "2024-01-29 19:34:05 -0500 -- Dumping ci secure files ... ", "2024-01-29 19:34:05 -0500 -- Dumping ci secure files ... done", "2024-01-29 19:34:05 -0500 -- Creating backup archive: 1706574679_2024_01_29_16.4.5_gitlab_backup.tar ... ", "2024-01-29 19:34:13 -0500 -- Creating backup archive: 1706574679_2024_01_29_16.4.5_gitlab_backup.tar ... done", "2024-01-29 19:34:13 -0500 -- Uploading backup archive to remote storage  ... [SKIPPED]", "2024-01-29 19:34:13 -0500 -- Deleting old backups ... [SKIPPED]", "2024-01-29 19:34:13 -0500 -- Deleting tar staging files ... ", "2024-01-29 19:34:13 -0500 -- Cleaning up /app/gitlab/backups/backup_information.yml", "2024-01-29 19:34:13 -0500 -- Cleaning up /app/gitlab/backups/builds.tar.gz", "2024-01-29 19:34:13 -0500 -- Cleaning up /app/gitlab/backups/artifacts.tar.gz", "2024-01-29 19:34:13 -0500 -- Cleaning up /app/gitlab/backups/pages.tar.gz", "2024-01-29 19:34:13 -0500 -- Cleaning up /app/gitlab/backups/lfs.tar.gz", "2024-01-29 19:34:13 -0500 -- Cleaning up /app/gitlab/backups/terraform_state.tar.gz", "2024-01-29 19:34:13 -0500 -- Cleaning up /app/gitlab/backups/packages.tar.gz", "2024-01-29 19:34:13 -0500 -- Cleaning up /app/gitlab/backups/ci_secure_files.tar.gz", "2024-01-29 19:34:13 -0500 -- Deleting tar staging files ... done", "2024-01-29 19:34:13 -0500 -- Deleting backups/tmp ... ", "2024-01-29 19:34:13 -0500 -- Deleting backups/tmp ... done", "2024-01-29 19:34:13 -0500 -- Warning: Your gitlab.rb and gitlab-secrets.json files contain sensitive data ", "and are not included in this backup. You will need these files to restore a backup.", "Please back them up manually.", "2024-01-29 19:34:13 -0500 -- Backup 1706574679_2024_01_29_16.4.5 is done.", "2024-01-29 19:34:13 -0500 -- Deleting backup and restore PID file ... done"]}

我只需要打印这个文件名:1706574679_2024_01_29_16.4.5
任何人都可以帮忙

我只需要在我的标准输出中打印这个文件名:1706574679_2024_01_29_16.4.5
任何人都可以帮忙

ansible ansible-inventory ansible-facts
1个回答
0
投票

解决方案1:

  - debug:
      var: backup_name
    vars:
      backup_name: "{{ gitlab_backup.stdout | regex_search('Creating backup archive:\\s(.*?)\\s','\\1') | join('') }}"

这里我们使用您从备份任务中注册的

gitlab_backup
,并使用正则表达式我们搜索模式并仅匹配备份文件名的部分。最后我们
join
价值观。这里 join 用于将 regex_search 的输出转换为字符串,因为输出类型是列表。

解决方案2: 如果你不擅长正则表达式(解决方案 1 中的正则表达式非常简单),你可以使用这个。感觉更像是在 shell 中使用 tr/grep/cut 进行过滤:

  - debug:
      var: bak_name
    vars:
      bak_name: "{{ gitlab_backup.stdout_lines | select('match', '.*Creating backup archive.*done') | join('') | split(' ') | select('match','^\\d+_.*') }}"

  • 选择与我们的模式匹配的行(
    grep
    )
  • 从列表转换为字符串
  • 分裂(
    tr ' ' '\n'
    )
  • 匹配以数字开头且数字后有下划线的行(
    grep '^[0-9]*_'
    )
© www.soinside.com 2019 - 2024. All rights reserved.