要修改Raspberry Pi上的cmdline.txt文件,可以使用Ansible进行自动化配置。以下是一个使用Ansible的示例代码:
modify_cmdline.yml
的playbook文件:---
- name: Modify cmdline.txt on Raspberry Pi
hosts: raspberrypi # 根据实际情况设置主机名或主机组
gather_facts: false
tasks:
- name: Read the current contents of cmdline.txt
shell: cat /boot/cmdline.txt
register: current_cmdline
changed_when: false
- name: Modify cmdline.txt
lineinfile:
path: /boot/cmdline.txt
line: "your modification here"
register: cmdline_modified
changed_when: cmdline_modified.changed
- name: Show modified cmdline.txt
shell: cat /boot/cmdline.txt
when: cmdline_modified.changed
- name: Reboot if cmdline.txt was modified
reboot:
connect_timeout: 10
reboot_timeout: 300
reboot_timeout_warning: 240
when: cmdline_modified.changed
hosts
的文件,其中包含Raspberry Pi的IP地址或主机名:[raspberrypi]
192.168.1.100 # 替换为实际的Raspberry Pi的IP地址或主机名
ansible-playbook -i hosts modify_cmdline.yml
这个playbook会先读取当前cmdline.txt文件的内容,然后使用lineinfile模块修改cmdline.txt文件,并显示修改后的内容。如果cmdline.txt文件被修改,它将重新启动Raspberry Pi。请根据需要修改line: "your modification here"
部分,将"your modification here"替换为你要在cmdline.txt中进行的实际修改。