获取多个文件内容到一个Ansible变量中

huangapple go评论70阅读模式
英文:

Get multiple file contents to one Ansible variable

问题

在Ubuntu 18服务器上的目录*/home/adminuser/keys*中有5个包含密钥部分的文件:

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

文件内容:

1bbeaafab5037a287bde3e5203c8b2cd205f4cc55b4fcffe7931658dc20d8cdcdf
bdf7a6ee4c493aca5b9cc2105077ec67738a0e8bf21936abfc5d1ff8080b628fcb
545c087d3d59d02556bdbf8690c8cc9faafec0e9766bb42de3a7884159356e91b8
053207b0683a8a2886129f7a1988601629a9e7e0d8ddbca02333ce08f1cc7b3887
2320f6275804341ebe5d39a623dd309f233e454b4453c692233ca86212a3d40b5f

Ansible playbook(任务)的一部分:

- name: 读取文件内容
  command: cat {{item}}
  register: unseal_keys
  with_fileglob: "/home/adminuser/keys/*"

我遇到的错误:

"[WARNING]: 无法在预期的路径中找到'/home/adminuser/keys'(使用-vvvvv查看路径)"

我尝试过:

  • 更改创建目录和文件的用户
  • 更改路径为/home/adminuser/keys/和/home/adminuser/keys

我期望所有文件内容(即单个密钥的部分)被合并为一个字符串:

1bbeaafab5037a287bde3e5203c8b2cd205f4cc55b4fcffe7931658dc20d8cdcdfbdf7a6ee4c493aca5b9cc2105077ec67738a0e8bf21936abfc5d1ff8080b628fcb545c087d3d59d02556bdbf8690c8cc9faafec0e9766bb42de3a7884159356e91b8 053207b0683a8a2886129f7a1988601629a9e7e0d8ddbca02333ce08f1cc7b38872320f6275804341ebe5d39a623dd309f233e454b4453c692233ca86212a3d40b5f
英文:

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

答案1

得分: 1

使用模块assemble 来:"从片段组装配置文件。

声明路径

  key_all_path: /tmp/admin/key_all

组装片段

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

这将创建文件*/tmp/admin/key_all*

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

读取文件并连接行。声明变量

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

得到

  key_all: abcdefghi
英文:

<sup>

Given the files below for testing

shell&gt; tree /tmp/admin/
/tmp/admin/
└── key
    ├── key_0
    ├── key_1
    └── key_2

1 directory, 3 files
shell&gt; cat /tmp/admin/key/key_0
abc
shell&gt; cat /tmp/admin/key/key_1
def
shell&gt; cat /tmp/admin/key/key_2
ghi

</sup>

<hr>

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: &quot;{{ key_all_path }}&quot;

This will create the file /tmp/admin/key_all

shell&gt; cat /tmp/admin/key_all 
abc
def
ghi

Read the file and join the lines. Declare the variable

  key_all: &quot;{{ lookup(&#39;file&#39;, key_all_path).splitlines()|join(&#39;&#39;) }}&quot;

gives

  key_all: abcdefghi

<hr>

Example of a complete playbook for testing

<sup>

- hosts: localhost

  vars:

    key_all_path: /tmp/admin/key_all
    key_all: &quot;{{ lookup(&#39;file&#39;, key_all_path).splitlines()|join(&#39;&#39;) }}&quot;

  tasks:

    - assemble:
        src: /tmp/admin/key
        dest: &quot;{{ key_all_path }}&quot;

    - debug:
        var: key_all

</sup>

答案2

得分: 0

问题在于任务需要执行的路径和主机。
通过定位和本地阅读文件以及执行此任务解决了问题:

- name: 读取文件内容
  command: cat "{{item}}"
  register: keys                       ----> 将所有文件内容存储在变量 "keys" 中
  with_fileglob: "~/keys/*"            ----> 这是存储在我的本地机器上的所有文件的目录路径
  delegate_to: localhost              ----> 在这里,我指定该任务将在本地机器上执行
  become: false                        ----> 移除 sudo,以便不需要密码。
英文:

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 &quot;{{item}}&quot;
  register: keys                       ----&gt; all file contents to variable &quot;keys&quot;
  with_fileglob: &quot;~/keys/*&quot;            ----&gt; this is path to directory all files are storedon my local machine
   delegate_to: localhost              ----&gt; here I specify that this task will be executed on local machine
  become: false                        ----&gt; remove sudo so that password is not requested

huangapple
  • 本文由 发表于 2023年2月14日 21:32:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定