大网数据广告活动
服务器运维问答

copy contents of registered variable to other hosts

· 52CE 我爱测

I'm running the following tasks (I'm going to paste only the relevant asks in the whole role): - name: create certificate private key community.crypto.openssl_privatekey: path: "/root/client/{{ item }}.key" type: Ed25519 backup: …

问题原文

原帖链接 · Lethargos · CC BY-SA 4.0

I'm running the following tasks (I'm going to paste only the relevant asks in the whole role):

- name: create certificate private key
  community.crypto.openssl_privatekey:
    path: "/root/client/{{ item }}.key"
    type: Ed25519
    backup: yes
    return_content: yes
  register: privatekey
  loop: "{{ ansible_play_hosts_all }}"
  when: "'prometheus1' in inventory_hostname"

What I'm trying to do it copy the contents of the variable to another host.

- name: copy private key to host
  copy:
    content: "{{ privatekey }}"
    dest: /root/private_key_from_var.key

(I'm not at this point worried about filtering or distributing the key to the corresponding hosts - just want to understand why the content itself isn't being copied.

So weirdly enough, the only host it works on is 'prometheus1', the one where I'm actually running the task that generates the keys. There I get the a generated file private_key_from_var.key with all the contents I'm expecting.

The exact same file on the other two hosts is just an error report coming from ansible itself:

{"results": [{"changed": false, "skipped": true, "skip_reason": "Conditional result was False", "item": "prometheus1", "ansible_loop_var": "item"}, {"changed": false, "skipped": true, "skip_reason": "Conditional result was False", "item": "breitband", "ansible_loop_var": "item"}, {"changed": false, "skipped": true, "skip_reason": "Conditional result was False", "item": "status1", "ansible_loop_var": "item"}], "msg": "All items completed", "changed": false}

I'm not sure why I am unable to copy this content on any host I wish to, but it's rather strange. I also tried the same thing using a template and invoking the variable directly in the jinja file, but to no avail.

Any ideas?:)

已采纳解答

原帖链接 · Ackack · CC BY-SA 4.0

when: "'prometheus1' in inventory_hostname"

This is only running the task when the hostname matches prometheus1, and explains why you're getting it only on the prometheus1 host.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-03-26)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。