代码之家  ›  专栏  ›  技术社区  ›  Nicolas CASAUX

可靠的主机名模块,根据IP地址设置主机名

  •  0
  • Nicolas CASAUX  · 技术社区  · 1 年前

    我正在尝试使用 ansible hostname module 以更改多个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似乎被使用了。。。

    1 回复  |  直到 1 年前
        1
  •  0
  •   larsks    1 年前

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

    从错误消息来看,这看起来像是权限问题:

    ==== 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 ,这将导致此播放中的所有任务以 .