我有这个剧本:
- name: push ssh key for all lxc
hosts: proxmox_host
vars:
lxc_cts:
- 100
- 102
- 106
- 107
- 108
- 109
- 111
- 112
- 113
- 114
tasks:
- name: check ssh
command: pct exec {{ item }} -- rpm -q openssh-server
register: ssh_check
ignore_errors: true
loop: '{{ lxc_cts }}'
- name: SSH status
debug:
var: ssh_check
- name: install openssh if not installed
command: pct exec {{ item }} -- dnf install -y openssh-server
when: ssh_check.results[ansible_loop.index0].rc != 0
loop: '{{ lxc_cts }}'
- name: enable sshd
command: pct exec {{ item }} -- dnf enable --now sshd
loop: '{{ lxc_cts }}'
- name: copy key
command: pct push {{ item }} /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
delegate_to: proxmox_host
loop: '{{ lxc_cts }}'
- name: perms for dir
command: pct exec {{ item }} -- chmod 700 /root/.ssh
loop: '{{ lxc_cts }}'
- name: perms for file
command: pct exec {{ item }} -- chmod 600 /root/.ssh/authorized_keys
loop: '{{ lxc_cts}}'
- name: (debug) SSH is already installed!
debug:
msg: 'SSH is already installed on CT {{ item }}!'
when: ssh_check.results|length > item|int and ssh_check.results[item|int].rc != 0
with_items: '{{ lxc_cts }}'
我尝试了 AI 建议的每一个方法,阅读了一些文档,但仍然无法理解日志中的错误
2025-01-09 21:09:12,656 p=3537536 u=root n=ansible | 致命:[proxmox_host]:失败! => {"msg": "条件检查 'ssh_check.results[ansible_loop.index0].rc != 0' 失败。错误为:评估条件时出错(ssh_check.results[ansible_loop.index0].rc != 0):列表对象没有元素 AnsibleUndefined(hint=None,obj=missing,name='ansible_loop')。列表对象没有元素 AnsibleUndefined(hint=None,obj=missing,name='ansible_loop')\n\n错误似乎在 '/root/ansible/keypush_with_check.yml':第 26 行,第 7 列,但可能\n在文件的其他位置,具体取决于确切的语法问题。\n\n有问题的行似乎是:\n\n\n - 名称:如果未安装,请安装 openssh\n ^ 此处\n"}
我认为这是逻辑错误,但人工智能无法解释和修复它,所以也许有人可以帮助我?
ansible_loop.index0
您需要使用loop_control
和 ,而不是index_var
: