Search code examples
ansibleansible-inventoryansible-facts

I want to print a specific line/value in my stdout using ansible


In my playbook, I run the gitlab-rake backup command works as expected. Now I want to print the output for this example its "1706574679_2024_01_29_16.4.5_gitlab_backup.tar" but I'm struggling to find a way on how to print this.

here's my playbook:

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:'.'*') }}"

this is the output when the backup completed:

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"]}

I only need to print this file name: 1706574679_2024_01_29_16.4.5
Anyone can help

I only need to print this file name in my stdout: 1706574679_2024_01_29_16.4.5
Anyone can help


Solution

  • solution 1:

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

    here we use gitlab_backup which you are registering from your backup task and using regex we search for pattern and match just the part which is backup file name. and in the end we join the values. here join is used to just convert output of regex_search to string since output type is list.

    solution 2: if You are not good with regex(which regex in solution 1 is really simple) you can use this one. it feels more like filtering with tr/grep/cut in shell:

      - debug:
          var: bak_name
        vars:
          bak_name: "{{ gitlab_backup.stdout_lines | select('match', '.*Creating backup archive.*done') | join('') | split(' ') | select('match','^\\d+_.*') }}"
    
    
    • select line which match our pattern (grep)
    • convert to string from list
    • split(tr ' ' '\n')
    • match the line which starts with number and has underscore after numbers(grep '^[0-9]*_')