我发现使用WSL查询树莓派的地址,使用PowerShell确实更简单。并且,我也可以在Ansible中使用它
使用WSL查找Raspberry Pi的IP地址。
在运行Windows 10的Windows Subsystem for Linux上访问树莓派时,我需要调用Windows的ping.exe程序来查找raspberrypi.local的地址,然后再进行SSH连接。这有些不方便。
$ ping.exe -4 raspberrypi.local
raspberrypi.local [192.168.0.7]に ping を送信しています 32 バイトのデータ:
192.168.0.7 からの応答: バイト数 =32 時間 =5ms TTL=64
(以下省略)
根据提及,据说可以用 Powershell 查找 树莓派 的地址,所以我试了试 Powershell ,结果很容易就找到了。
$ powershell.exe "(Resolve-DnsName -type A raspberrypi.local).IP4Address"
192.168.0.7
$
只需将SSH连接作为参数传递给它,就可以这样做。为了防止树莓派地址发生变化时造成困扰,我添加了HostKeyAlias。强烈推荐这种做法。
$ ssh -o HostKeyAlias=raspberrypi.local -l pi $(powershell.exe "(Resolve-DnsName -type A raspberrypi.local).IP4Address")
Password:
(以下省略)
可以将Ansible安装在电脑上。
我也可以使用Ansible和PowerShell来查找树莓派的地址,所以我会在下面提供参考。
hosts:在localhost上查找树莓派的地址,并使用add_host注册树莓派。
hosts:树莓派只写了更改初始密码、NTP和时区设置以及安装软件包的部分。
由于更改初始密码时尝试了一次连接,第二次会导致错误,所以我加入了ignore_unreachable: yes。
我使用的是Ansible 2.11.8版本。
树莓派的新操作系统已经扩展了各种设置选项,使得在创建SD卡镜像时可以进行多种配置。此外,默认用户也不再是pi。这篇文章并不是很有参考价值,请参考下面的链接:https://www.raspberrypi.com/documentation/computers/getting-started.html#advanced-options和https://www.raspberrypi.com/documentation/computers/configuration.html#setting-up-a-headless-raspberry-pi。
[ssh_connection]
ssh_args = -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null
- hosts: localhost
gather_facts: no
pre_tasks:
- name: Find IP Address of Raspberry Pi
shell: |
powershell.exe "(Resolve-DnsName -type A raspberrypi.local).IP4Address"
changed_when: False
register: resolve
- name: Show IP Address of Raspberry Pi
debug:
msg: '{{ resolve.stdout }}'
- name: Add Raspberry Pi to hosts
add_host:
hostname: raspberrypi
ansible_ssh_host: '{{ resolve.stdout }}'
ansible_user: pi
# New password
ansible_password: 123456
ansible_python_interpreter: python3
changed_when: False
- hosts: raspberrypi
gather_facts: no
become: true
handlers:
- name: Restart timesyncd
systemd:
name: systemd-timesyncd
state: restarted
daemon_reload: yes
tasks:
- name: Change password from default password
block:
- name: Set default password to sshpass
set_fact:
# Default password
ansible_ssh_pass: raspberry
- name: Try changing pi user password (Ignore if unreachable)
shell: |
echo '{{ ansible_user }}:{{ ansible_password }}' | chpasswd
ignore_unreachable: yes
- set_fact:
ansible_ssh_pass: null
- name: NTP settings for timesyncd
lineinfile:
dest: /etc/systemd/timesyncd.conf
regexp: '^#?NTP='
line: 'NTP=ntp.nict.jp'
notify:
- Restart timesyncd
- name: Set timezone to tokyo
timezone:
name: Asia/Tokyo
- name: Install packages
apt:
package:
- i2c-tools
- python3-pip
请不要参考将123456设置为新密码,因为它是最常用的密码。
$ ansible-playbook main.yaml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match
'all'
PLAY [localhost] *******************************************************************************************************
TASK [Find IP Address of Raspberry Pi] *********************************************************************************
ok: [localhost]
TASK [Show IP Address of Raspberry Pi] *********************************************************************************
ok: [localhost] => {
"msg": "192.168.0.7"
}
TASK [Add Raspberry Pi to hosts] ***************************************************************************************
ok: [localhost]
(以下省略)