Ansible主机名模块,根据IP地址设置主机名

问题描述 投票:0回答:1

我正在尝试使用 ansible 主机名模块 更改多个 Raspberry Pi 计算模块 4 的主机名。因为它们都基于同一个 Raspberry Pi 操作系统映像,所以它们的主机名都是

raspberrypi
,我的意思是想要改变。

我的库存大致是这样的:

rpi-cm4-1:
  node_number: 1
  name: rpi-cm4-1
  ansible_host: 192.168.1.101

rpi-cm4-2:
  node_number: 2
  name: rpi-cm4-2
  ansible_host: 192.168.1.102

...

我的剧本是这样的:

- name: Change hostname of managed nodes
  hosts: rpi_cm4-2
  tasks:
    - name: Change hostname of managed nodes
      ansible.builtin.hostname:
        name: "{{ hostvars[inventory_hostname].name }}"

当我运行剧本时,我面临的错误是这样的:

TASK [Gathering Facts] *****************************************************************************************************
ok: [rpi-cm4-2]

TASK [Change hostname of managed nodes] *****************************************************************************************************
fatal: [rpi-cm4-2]: FAILED! => changed=false
  msg: |-
    Command failed rc=1, out=, err=Could not set pretty hostname: Connection timed out

猜测,Ansible 可能没有使用清单的 IP 地址(但它用它来收集事实)。 我该怎么做才能让他使用库存的IP?

我无法让 Ansible 使用 IP 地址。

-vvv
实际上显示了这一点:

TASK [Change hostname of managed nodes] ****************************************************************************************************************************
task path: /home/ncasaux/home-lab/ansible/tpi2_rpi_cm4.tpi2_rpios.yml:75
<192.168.1.102> ESTABLISH SSH CONNECTION FOR USER: pi
<192.168.1.102> SSH: EXEC sshpass -d12 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'User="pi"' -o ConnectTimeout=10 -o 'ControlPath="/home/ncasaux/.ansible/cp/ed650a3305"' 192.168.1.102 '/bin/sh -c '"'"'echo ~pi && sleep 0'"'"''
<192.168.1.102> (0, b'/home/pi\n', b'')
<192.168.1.102> ESTABLISH SSH CONNECTION FOR USER: pi
<192.168.1.102> SSH: EXEC sshpass -d12 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'User="pi"' -o ConnectTimeout=10 -o 'ControlPath="/home/ncasaux/.ansible/cp/ed650a3305"' 192.168.1.102 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /home/pi/.ansible/tmp `"&& mkdir "` echo /home/pi/.ansible/tmp/ansible-tmp-1703118028.4996538-25006-160853801334657 `" && echo ansible-tmp-1703118028.4996538-25006-160853801334657="` echo /home/pi/.ansible/tmp/ansible-tmp-1703118028.4996538-25006-160853801334657 `" ) && sleep 0'"'"''
<192.168.1.102> (0, b'ansible-tmp-1703118028.4996538-25006-160853801334657=/home/pi/.ansible/tmp/ansible-tmp-1703118028.4996538-25006-160853801334657\n', b'')
Using module file /usr/lib/python3/dist-packages/ansible/modules/hostname.py
<192.168.1.102> PUT /home/ncasaux/.ansible/tmp/ansible-local-24823y1az1b7h/tmp3nxd67rj TO /home/pi/.ansible/tmp/ansible-tmp-1703118028.4996538-25006-160853801334657/AnsiballZ_hostname.py
<192.168.1.102> SSH: EXEC sshpass -d12 sftp -o BatchMode=no -b - -C -o ControlMaster=auto -o ControlPersist=60s -o 'User="pi"' -o ConnectTimeout=10 -o 'ControlPath="/home/ncasaux/.ansible/cp/ed650a3305"' '[192.168.1.102]'
<192.168.1.102> (0, b'sftp> put /home/ncasaux/.ansible/tmp/ansible-local-24823y1az1b7h/tmp3nxd67rj /home/pi/.ansible/tmp/ansible-tmp-1703118028.4996538-25006-160853801334657/AnsiballZ_hostname.py\n', b'')
<192.168.1.102> ESTABLISH SSH CONNECTION FOR USER: pi
<192.168.1.102> SSH: EXEC sshpass -d12 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'User="pi"' -o ConnectTimeout=10 -o 'ControlPath="/home/ncasaux/.ansible/cp/ed650a3305"' 192.168.1.102 '/bin/sh -c '"'"'chmod u+x /home/pi/.ansible/tmp/ansible-tmp-1703118028.4996538-25006-160853801334657/ /home/pi/.ansible/tmp/ansible-tmp-1703118028.4996538-25006-160853801334657/AnsiballZ_hostname.py && sleep 0'"'"''
<192.168.1.102> (0, b'', b'')
<192.168.1.102> ESTABLISH SSH CONNECTION FOR USER: pi
<192.168.1.102> SSH: EXEC sshpass -d12 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'User="pi"' -o ConnectTimeout=10 -o 'ControlPath="/home/ncasaux/.ansible/cp/ed650a3305"' -tt 192.168.1.102 '/bin/sh -c '"'"'/usr/bin/python3 /home/pi/.ansible/tmp/ansible-tmp-1703118028.4996538-25006-160853801334657/AnsiballZ_hostname.py && sleep 0'"'"''
<192.168.1.102> (1, b'\x1b[1;31m==== AUTHENTICATING FOR org.freedesktop.hostname1.set-static-hostname ====\r\n\x1b[0mAuthentication is required to set the statically configured local hostname, as well as the pretty hostname.\r\nAuthenticating as: ,,, (pi)\r\nPassword: polkit-agent-helper-1: pam_authenticate failed: Authentication failure\r\n\r\n{"failed": true, "msg": "Command failed rc=1, out=, err=Could not set pretty hostname: Connection timed out\\n", "invocation": {"module_args": {"name": "192.168.1.102", "use": null}}}\r\n', b'Shared connection to 192.168.1.102 closed.\r\n')
<192.168.1.102> Failed to connect to the host via ssh: Shared connection to 192.168.1.102 closed.
<192.168.1.102> ESTABLISH SSH CONNECTION FOR USER: pi
<192.168.1.102> SSH: EXEC sshpass -d12 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'User="pi"' -o ConnectTimeout=10 -o 'ControlPath="/home/ncasaux/.ansible/cp/ed650a3305"' 192.168.1.102 '/bin/sh -c '"'"'rm -f -r /home/pi/.ansible/tmp/ansible-tmp-1703118028.4996538-25006-160853801334657/ > /dev/null 2>&1 && sleep 0'"'"''
<192.168.1.102> (0, b'', b'')
fatal: [rpi-cm4-2]: FAILED! => changed=false
  invocation:
    module_args:
      name: 192.168.1.102
      use: null
  msg: |-
    Command failed rc=1, out=, err=Could not set pretty hostname: Connection timed out

所以IP好像被使用了...

ansible raspberry-pi hostname
1个回答
0
投票

很明显使用的是IP地址,所以这不是问题。看起来 ansible is 连接成功,但在尝试设置系统主机名时收到错误。

从错误信息来看,似乎是权限问题:

==== AUTHENTICATING FOR org.freedesktop.hostname1.set-static-hostname ====
Authentication is required to set the statically configured local hostname, as well as the pretty hostname.
Authenticating as: ,,, (pi)
Password: polkit-agent-helper-1: pam_authenticate failed: Authentication failure

您可能需要确保在远程主机上以

root
的方式运行任务:

- name: Change hostname of managed nodes
  hosts: rpi_cm4-2
  become: true
  tasks:
    - name: Change hostname of managed nodes
      ansible.builtin.hostname:
        name: "{{ hostvars[inventory_hostname].name }}"

注意添加了

become: true
,这将导致此剧中的所有任务都以
root
的方式运行。

© www.soinside.com 2019 - 2024. All rights reserved.