Ansible:读取远程文件

问题描述 投票:26回答:5

我在远程主机上生成带有ansible的文件,在这一代之后,我想在另一个任务中读取这些文件。

我找不到任何模块用ansible读取远程文件(查找似乎只在本地主机上)。

你知道这样的模块吗?

谢谢

编辑:

这是我的用例:

我生成ssh密钥,然后将其添加到github。这些键是由var文件中的对象设置的,所以我像这样循环生成它:

    tasks:
  - name: Create ssh key
    user:
      name: "{{sshConfigFile.user}}"
      generate_ssh_key: yes
      ssh_key_file: ".ssh/{{item.value.file}}"
      state: present
    with_dict: "{{sshConfiguration}}"

它工作得很好但是如何读取这些键通过API将其发送到github?

ansible ansible-playbook
5个回答
13
投票

正如您所说,所有查找都在localhost上。但是所有这些都可以通过使用shellregister在远程完成。你能告诉你到底想要做什么吗?只是一个例子。

  - shell: cat "{{remote_file}}"
    register: data

  - shell: ......
    with_xxxx:

52
投票

使用--diff标志运行(在目标文件更改时输出diff)。

ansible-playbook --diff server.yaml

或者slurp它...

- name: Slurp hosts file
  slurp:
    src: /etc/hosts
  register: slurpfile

- debug: msg="{{ slurpfile['content'] | b64decode }}"

6
投票

您可以尝试使用'fetch'模块,该模块将密钥文件检索到localhost上的目标路径:

fetch: 
  src: ".ssh/{{item.value.file}}" 
  dest:"/tmp/ssh_keys/{{item.value.file}}"
  flat: yes
with_dict: "{{sshConfiguration}}" 

2
投票

您可以使用register命令在变量中注册文件的内容。这是我的建议,

- name: get contents of file
  command: cat /path/to/file
  register: filename
  become: true # use case specific option

- name: viewing the contents
  debug:
    msg: "{{filename.stdout}}"

这将显示文件的内容。


-2
投票

通过使用命令模块,您可以在远程节点中的另一个任务中读取或使用该文件。

喜欢

-command: cp /tmp/xxx/example.sh /usr/local/yyy
© www.soinside.com 2019 - 2024. All rights reserved.