无法对ansib创建的数字海洋水滴运行任何命令

2024-09-30 23:31:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我想自动化这个过程

  • 生成SSH密钥
  • 把它加起来做
  • 创建液滴
  • 对它运行命令//这就是我失败的地方

到目前为止,我有这个:

在主.yml在

- name: DO
  hosts: localhost
  connection: local
  vars:
    PROJECT_NAME: "project_name"
    DO_TOKEN: "digital_ocean_api_token"
  tasks:
    - name: Generate SSH key
      shell: ssh-keygen -b 2048 -t rsa -f ~/.ssh/{{PROJECT_NAME}} -q -N ""
      args:
        creates: ~/.ssh/{{PROJECT_NAME}}

    - name: Create DigitalOcean SSH Key
      digital_ocean:
        state: present
        command: ssh
        name: "{{PROJECT_NAME}}_deploy"
        ssh_pub_key: "{{lookup('file', '~/.ssh/{{PROJECT_NAME}}.pub')}}"
        api_token: "{{DO_TOKEN}}"
      register: digital_ocean_key

    - name: Create DigitalOcean Droplet
      digital_ocean:
        state: present
        command: droplet
        name: "{{PROJECT_NAME}}"
        api_token: "{{DO_TOKEN}}"
        size_id: s-1vcpu-1gb
        region_id: fra1
        image_id: ubuntu-18-04-x64
        ssh_key_ids: "{{digital_ocean_key.ssh_key.id}}"
        wait_timeout: 500
        unique_name: yes
      when: digital_ocean_key.ssh_key is defined
      register: digital_ocean_droplet

    - debug: msg="ssh root@{{digital_ocean_droplet.droplet.ip_address}} -i ~/.ssh/{{PROJECT_NAME}}"

    - name: Add new host to inventory
      add_host:
        name: "{{digital_ocean_droplet.droplet.ip_address}}"
        groups: do
        ansible_ssh_private_key_file: "~/.ssh/{{PROJECT_NAME}}"
        ansible_python_interpreter: "/usr/local/bin/python"
      when: digital_ocean_droplet.droplet is defined

    - name: Wait for port 22 to become available.
      local_action: "wait_for port=22 host={{digital_ocean_droplet.droplet.ip_address}}"

- name: SERVER
  hosts: do
  remote_user: root
  gather_facts: false
  pre_tasks:
    - raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal)
    - setup:
  tasks:
    - name: Update APT package cache
      shell: apt -y update
      register: out

    - debug: var=out.stdout_lines

/etc/ansible/主机

^{pr2}$

获取错误(格式化了一点)

fatal: [xxx.xxx.xxx.xxx]: FAILED! =>
{  
  "changed":false,
  "module_stderr":"Shared connection to xxx.xxx.xxx.xxx closed.\r\n",
  "module_stdout":"/bin/sh: 1: /usr/local/bin/python: not found\r\n",
  "msg":"The module failed to execute correctly, you probably need to set the interpreter.\nSee stdout/stderr for the exact error",
  "rc":127
}

这个

  pre_tasks:
    - raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal)
    - setup:

是我在几小时的谷歌搜索后试图自己解决这个问题。我试过不同版本的ubuntu。另外,请不要太在意apt -y update命令,任何命令都将因相同的错误而无法执行。在

我可以使用这里打印的命令ssh到droplet

- debug: msg="ssh root@{{digital_ocean_droplet.droplet.ip_address}} -i ~/.ssh/{{PROJECT_NAME}}"

所以连接起作用了。在

错误的详细版本

...
        "(Reading database ... 80%",
        "(Reading database ... 85%",
        "(Reading database ... 90%",
        "(Reading database ... 95%",
        "(Reading database ... 100%",
        "(Reading database ... 60825 files and directories currently installed.)",
        "Preparing to unpack .../python_2.7.15~rc1-1_amd64.deb ...",
        "Unpacking python (2.7.15~rc1-1) ...",
        "Processing triggers for mime-support (3.60ubuntu1) ...",
        "Processing triggers for man-db (2.8.3-2ubuntu0.1) ...",
        "Setting up libpython2.7-stdlib:amd64 (2.7.15~rc1-1ubuntu0.1) ...",
        "Setting up python2.7 (2.7.15~rc1-1ubuntu0.1) ...",
        "Setting up libpython-stdlib:amd64 (2.7.15~rc1-1) ...",
        "Setting up python (2.7.15~rc1-1) ..."
    ]
}

TASK [setup] *******************************************************************
task path: /Users/kolpav/Projects/Authentication/ansible/so.yaml:56
<xxx.xxx.xxx.xxx> ESTABLISH SSH CONNECTION FOR USER: root
<xxx.xxx.xxx.xxx> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o 'IdentityFile="/Users/kolpav/.ssh/project_name"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=root -o ConnectTimeout=10 -o ControlPath=/Users/kolpav/.ansible/cp/69afd34995 xxx.xxx.xxx.xxx '/bin/sh -c '"'"'echo ~root && sleep 0'"'"''
<xxx.xxx.xxx.xxx> (0, b'/root\n', b'')
<xxx.xxx.xxx.xxx> ESTABLISH SSH CONNECTION FOR USER: root
<xxx.xxx.xxx.xxx> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o 'IdentityFile="/Users/kolpav/.ssh/project_name"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=root -o ConnectTimeout=10 -o ControlPath=/Users/kolpav/.ansible/cp/69afd34995 xxx.xxx.xxx.xxx '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1557727106.860597-245092007050680 `" && echo ansible-tmp-1557727106.860597-245092007050680="` echo /root/.ansible/tmp/ansible-tmp-1557727106.860597-245092007050680 `" ) && sleep 0'"'"''
<xxx.xxx.xxx.xxx> (0, b'ansible-tmp-1557727106.860597-245092007050680=/root/.ansible/tmp/ansible-tmp-1557727106.860597-245092007050680\n', b'')
Using module file /usr/local/Cellar/ansible/2.7.10/libexec/lib/python3.7/site-packages/ansible/modules/system/setup.py
<xxx.xxx.xxx.xxx> PUT /Users/kolpav/.ansible/tmp/ansible-local-4206fpg3cnwi/tmpzlis42y0 TO /root/.ansible/tmp/ansible-tmp-1557727106.860597-245092007050680/AnsiballZ_setup.py
<xxx.xxx.xxx.xxx> SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o 'IdentityFile="/Users/kolpav/.ssh/project_name"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=root -o ConnectTimeout=10 -o ControlPath=/Users/kolpav/.ansible/cp/69afd34995 '[xxx.xxx.xxx.xxx]'
<xxx.xxx.xxx.xxx> (0, b'sftp> put /Users/kolpav/.ansible/tmp/ansible-local-4206fpg3cnwi/tmpzlis42y0 /root/.ansible/tmp/ansible-tmp-1557727106.860597-245092007050680/AnsiballZ_setup.py\n', b'')
<xxx.xxx.xxx.xxx> ESTABLISH SSH CONNECTION FOR USER: root
<xxx.xxx.xxx.xxx> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o 'IdentityFile="/Users/kolpav/.ssh/project_name"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=root -o ConnectTimeout=10 -o ControlPath=/Users/kolpav/.ansible/cp/69afd34995 xxx.xxx.xxx.xxx '/bin/sh -c '"'"'chmod u+x /root/.ansible/tmp/ansible-tmp-1557727106.860597-245092007050680/ /root/.ansible/tmp/ansible-tmp-1557727106.860597-245092007050680/AnsiballZ_setup.py && sleep 0'"'"''
<xxx.xxx.xxx.xxx> (0, b'', b'')
<xxx.xxx.xxx.xxx> ESTABLISH SSH CONNECTION FOR USER: root
<xxx.xxx.xxx.xxx> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o 'IdentityFile="/Users/kolpav/.ssh/project_name"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=root -o ConnectTimeout=10 -o ControlPath=/Users/kolpav/.ansible/cp/69afd34995 -tt xxx.xxx.xxx.xxx '/bin/sh -c '"'"'/usr/local/bin/python /root/.ansible/tmp/ansible-tmp-1557727106.860597-245092007050680/AnsiballZ_setup.py && sleep 0'"'"''
<xxx.xxx.xxx.xxx> (127, b'/bin/sh: 1: /usr/local/bin/python: not found\r\n', b'Shared connection to xxx.xxx.xxx.xxx closed.\r\n')
<xxx.xxx.xxx.xxx> Failed to connect to the host via ssh: Shared connection to xxx.xxx.xxx.xxx closed.
<xxx.xxx.xxx.xxx> ESTABLISH SSH CONNECTION FOR USER: root
<xxx.xxx.xxx.xxx> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o 'IdentityFile="/Users/kolpav/.ssh/project_name"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=root -o ConnectTimeout=10 -o ControlPath=/Users/kolpav/.ansible/cp/69afd34995 xxx.xxx.xxx.xxx '/bin/sh -c '"'"'rm -f -r /root/.ansible/tmp/ansible-tmp-1557727106.860597-245092007050680/ > /dev/null 2>&1 && sleep 0'"'"''
<xxx.xxx.xxx.xxx> (0, b'', b'')
fatal: [xxx.xxx.xxx.xxx]: FAILED! => {
    "changed": false,
    "module_stderr": "Shared connection to xxx.xxx.xxx.xxx closed.\r\n",
    "module_stdout": "/bin/sh: 1: /usr/local/bin/python: not found\r\n",
    "msg": "The module failed to execute correctly, you probably need to set the interpreter.\nSee stdout/stderr for the exact error",
    "rc": 127
}

Tags: tononamebinrootansibleusersssh