當我使用此代碼時,它的打印主機名和ip地址分兩行顯示。Asible將stdout_line拆分爲數組元素
- name: Store known hosts of 'all' the hosts in the inventory file
hosts: localhost
connection: local
vars:
ssh_known_hosts_command: "ssh-keyscan -T 10"
ssh_known_hosts_file: "{{ lookup('env','HOME') + '/.ssh/known_hosts' }}"
ssh_known_hosts: "{{ groups['all'] }}"
tasks:
- name: For each host, find the ip
shell: 'echo -e "{{ item }}\n`dig +short {{ item }}`"'
with_items: "{{ ssh_known_hosts }}"
register: ssh_known_host_results
ignore_errors: yes
- name: print message
debug:
msg: "{{ item + ' test' }}"
with_items: "{{ ssh_known_host_results.results | map(attribute='stdout_lines') | list }}"
如果我添加邏輯忽略本地主機
時:不項== 'localhost' 的
,其不劈裂主機名和IP地址作爲陣列線,它主機名爲& ip爲一行。
- name: Store known hosts of 'all' the hosts in the inventory file
hosts: localhost
connection: local
vars:
ssh_known_hosts_command: "ssh-keyscan -T 10"
ssh_known_hosts_file: "{{ lookup('env','HOME') + '/.ssh/known_hosts' }}"
ssh_known_hosts: "{{ groups['all'] }}"
tasks:
- name: For each host, find the ip
shell: 'echo -e "{{ item }}\n`dig +short {{ item }}`"'
with_items: "{{ ssh_known_hosts }}"
when: not item == 'localhost'
register: ssh_known_host_results
ignore_errors: yes
- name: print message
debug:
msg: "{{ item + ' test' }}"
with_items: "{{ ssh_known_host_results.results | map(attribute='stdout_lines') | list }}"
如何分割數組元素與條件?
感謝 SR
我敢打賭,這是[XY問題](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)。例如,有[dig](http://docs.ansible.com/ansible/playbooks_lookups.html#the-dns-lookup-dig)查找。 –
@KonstantinSuvorov,我的想法是獲取主機名和IP地址,並從know_hosts文件中刪除舊密鑰條目,並將新密鑰添加到已知主機文件。挖看不起這種情況。 – sfgroups
以下是可能的解決方案:爲每個主機運行'ssh-keyscan'命令(獲取當前指紋),循環槽註冊結果並使用'lineinfile'模塊更新known_hosts(如果需要)。 –