首页 > 解决方案 > 如何在 ansible playbook 中的用户给定主机上运行用户给定的命令?

问题描述

---
- name: Adding the host server
  hosts: localhost

  vars_prompt:
    - name: "Server"
      prompt: "Server IP"
      private: no

    - name: "User"
      prompt: "Username"
      default: "Ubuntu"
      private: no

    - name: "Passwd"
      prompt: "Password"
      private: yes
      encrypt: "sha512_crypt"

    - name: "IdFile"
      prompt: "Identity file path(~/.ssh/id_rsa.pub)"
      private: no
      when: Passwd is undefined

    - name: "cmd"
      prompt: "Which command you want to run 1.ls -l 2.Top 3.uptime."
      private: |-
        no


  tasks:
    - name: Add host server
      add_host:
        name: "{{ Server }}"
        ansible_ssh_user: "{{ User }}"
        ansible_ssh_private_key_file: "{{ IdFile }}"
      when: IdFile is defined

    - name: Add host server
      add_host:
        name: "{{ Server }}"
        ansible_ssh_user: "{{ User }}"
         ansible_ssh_pass: "{{ Passwd }}"
      when: Passwd is defined

    - name: check OS name
      shell: uname -a
      delegate_to: "{{ Server }}"

- hosts: "{{ Server }}"
#below are for testing only.
  tasks:
    - name: Execute a command using the shell module
      become: true
      #become_user: root
      shell: uname -a

    - name: check OS name
      shell: uname -a
      delegate_to: "{{ Server }}"

这给出了以下错误:致命:[localhost]:无法访问!=> {"changed": false, "msg": "Authentication failure.", "unreachable": true}

任何人都可以检查一次上述代码并提出更改建议吗?

标签: ansibleuser-inputssh-keysansible-inventory

解决方案


推荐阅读