skip to Main Content

On Ubuntu 18 server in directory /home/adminuser/keys are 5 files that contain key parts:

/home/adminuser/key/
|-  unseal_key_0
|-  unseal_key_1
|-  unseal_key_2
|-  unseal_key_3
|-  unseal_key_4

File contents:

1bbeaafab5037a287bde3e5203c8b2cd205f4cc55b4fcffe7931658dc20d8cdcdf
bdf7a6ee4c493aca5b9cc2105077ec67738a0e8bf21936abfc5d1ff8080b628fcb
545c087d3d59d02556bdbf8690c8cc9faafec0e9766bb42de3a7884159356e91b8
053207b0683a8a2886129f7a1988601629a9e7e0d8ddbca02333ce08f1cc7b3887
2320f6275804341ebe5d39a623dd309f233e454b4453c692233ca86212a3d40b5f

Part of Ansible playbook (task):

- name: Reading file contents
      command: cat {{item}}
      register: unseal_keys
      with_fileglob: "/home/adminuser/keys/*"

The error that I get:

"[WARNING]: Unable to find ‘/home/adminuser/keys’ in expected paths (use -vvvvv to see paths)"

I have tried to:

  • change user that creates directory and files
  • change path to /home/adminuser/keys/ and /home/adminuser/keys

I expect all of the file contents (that is parts of a single key) to be merged into one string:

1bbeaafab5037a287bde3e5203c8b2cd205f4cc55b4fcffe7931658dc20d8cdcdfbdf7a6ee4c493aca5b9cc2105077ec67738a0e8bf21936abfc5d1ff8080b628fcb545c087d3d59d02556bdbf8690c8cc9faafec0e9766bb42de3a7884159356e91b8 053207b0683a8a2886129f7a1988601629a9e7e0d8ddbca02333ce08f1cc7b38872320f6275804341ebe5d39a623dd309f233e454b4453c692233ca86212a3d40b5f

2

Answers


  1. Chosen as BEST ANSWER

    Thanks ! Problem was in paths and hosts where task had to be executed. Problem is solved by locating and reading files localy and executing this task:

    - name: Reading file contents
      command: cat "{{item}}"
      register: keys                       ----> all file contents to variable "keys"
      with_fileglob: "~/keys/*"            ----> this is path to directory all files are storedon my local machine
       delegate_to: localhost              ----> here I specify that this task will be executed on local machine
      become: false                        ----> remove sudo so that password is not requested
    

  2. Given the files below for testing

    shell> tree /tmp/admin/
    /tmp/admin/
    └── key
        ├── key_0
        ├── key_1
        └── key_2
    
    1 directory, 3 files
    
    shell> cat /tmp/admin/key/key_0
    abc
    
    shell> cat /tmp/admin/key/key_1
    def
    
    shell> cat /tmp/admin/key/key_2
    ghi
    


    Use the module assemble to: "assemble a configuration file from fragments."

    Declare the path

      key_all_path: /tmp/admin/key_all
    

    and assemble the fragments

        - assemble:
            src: /tmp/admin/key
            dest: "{{ key_all_path }}"
    

    This will create the file /tmp/admin/key_all

    shell> cat /tmp/admin/key_all 
    abc
    def
    ghi
    

    Read the file and join the lines. Declare the variable

      key_all: "{{ lookup('file', key_all_path).splitlines()|join('') }}"
    

    gives

      key_all: abcdefghi
    

    Example of a complete playbook for testing

    - hosts: localhost
    
      vars:
    
        key_all_path: /tmp/admin/key_all
        key_all: "{{ lookup('file', key_all_path).splitlines()|join('') }}"
    
      tasks:
    
        - assemble:
            src: /tmp/admin/key
            dest: "{{ key_all_path }}"
    
        - debug:
            var: key_all
    

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search