Can';无法访问我的库存项目的ansible_主机属性
我有这个库存文件:Can';无法访问我的库存项目的ansible_主机属性,ansible,Ansible,我有这个库存文件: [masters] master ansible_host=1.2.3.4.5 ansible_user=ubuntu [workers] worker1 ansible_host=1.2.3.4.5 ansible_user=ubuntu [nodes] master ansible_host=1.2.3.4.5 ansible_user=ubuntu worker1 ansible_host=1.2.3.4.5 ansible_user=ubuntu [filese
[masters]
master ansible_host=1.2.3.4.5 ansible_user=ubuntu
[workers]
worker1 ansible_host=1.2.3.4.5 ansible_user=ubuntu
[nodes]
master ansible_host=1.2.3.4.5 ansible_user=ubuntu
worker1 ansible_host=1.2.3.4.5 ansible_user=ubuntu
[fileserver]
fs-01 ansible_host=1.2.3.4.5 ansible_user=ubuntu
我正在尝试将SSH密钥从本地计算机安装到所有库存计算机
- name: install the SSH key on all machines.
hosts: 127.0.0.1
connection: local
tasks:
- name: install SSH-key
shell: sshpass -p {{ ssh_key_password }} ssh-copy-id -i /id_rsa.pub -o StrictHostKeyChecking ubuntu@{{ item }}
with_items:
- "{{ hostvars['all'].ansible_host }}"
但是,它以以下错误结束:
fatal: [127.0.0.1]: FAILED! => {"msg": "\"hostvars['all']\" is undefined"}
我正在尝试获取库存中每个唯一库存名称的ansible_主机
1.2.3.4.5
。然而,我运气不太好。我做错了什么?hostvars是一个字典,它将ansible主机存储为键,将其变量存储为值。因此,对于您的清单,hostvars
dict如下所示:
master: {...}
worker1: {...}
fs-01: {...}
all
返回未定义的原因是您没有名为all
的主机
因为您只需要主机名,而不是hostvars['all'].ansible\u主机
,所以只需使用ansible\u主机
,它将解析主机名供您通过ssh访问
像这样:
with_items:
- "{{ ansible_host }}"
你在倒退。Ansible会自动循环播放主机,因此请使用您的主机,而不是
localhost
,然后将任务委托给localhost
- name: install the SSH key on all machines.
hosts: all
gather_facts: no
tasks:
- name: install SSH-key
shell: sshpass -p {{ ssh_key_password }} ssh-copy-id -i /id_rsa.pub -o StrictHostKeyChecking {{ ansible_user }}@{{ ansible_host }}
delegate_to: localhost
我认为你正在寻找的参考资料如下
---
- hosts: localhost
connection: local
gather_facts: no
tasks:
- name: Print all hosts
debug:
msg: "{{hostvars[item]['ansible_host']}}"
with_items:
- "{{groups['all']}}"
这给出了输出
PLAY [localhost] ***************************************************************************************************************************************************************************************************************************************************************
TASK [Print all hosts] *******************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => (item=master) => {
"msg": "1.2.3.4.5"
}
ok: [localhost] => (item=worker1) => {
"msg": "1.2.3.4.5"
}
ok: [localhost] => (item=fs-01) => {
"msg": "1.2.3.4.5"
}
PLAY RECAP *********************************************************************************************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0