【问题标题】:Ansible known_hosts module with a loop带有循环的 Ansible known_hosts 模块
【发布时间】:2020-05-06 02:38:24
【问题描述】:

我正在尝试运行一个任务来执行 known_hosts 模块,其中包含一个关键条目列表。问题是我不断收到以下错误,即使变量在使用debug 时有数据。

The task includes an option with an undefined variable. The error was: 'item' is undefined

我有以下任务来注册 ssh-keyscan 输出的变量

- name: keyscan platform hosts
  shell: "ssh-keyscan ssh.{{ item }}"
  register: "platform_ssh_host_keys"
  loop:
    - "one.example.com"
    - "two.example.com"

当我运行以下调试时,我得到包含密钥的标准输出

- name: debug
  debug:
    var: item.stdout
  with_items: "{{platform_ssh_host_keys.results}}"

但当我使用 known_hosts 运行它时,它会说 item 未定义。

- name: configure known_hosts
  known_hosts:
    path: "~/.ssh/known_hosts"
    name: "ssh.{{ item.item }}"
    key: "{{ item.stdout }}"
    state: present
    loop: "{{ platform_ssh_host_keys.results }}"

我不明白如何在 debug 中定义 item 而不是 known_hosts 任务。

【问题讨论】:

    标签: ansible


    【解决方案1】:

    ?‍♂️ 好吧,在我发布此内容后,我立即意识到问题在于我将loop 作为known_hosts 的属性而不是任务。所以问题是缩进。

    这一切都解决了。

    - name: configure known_hosts
      known_hosts:
        path: "~/.ssh/known_hosts"
        name: "ssh.{{ item.item }}"
        key: "{{ item.stdout }}"
        state: present
      loop: "{{platform_ssh_host_keys.results}}"
    

    【讨论】:

      猜你喜欢
      • 2020-01-01
      • 2017-09-15
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      • 1970-01-01
      • 2022-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多