ansible - 如何在 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}
任何人都可以检查一次上述代码并提出更改建议吗?
解决方案
推荐阅读
- prolog - 使用 Prolog 库 Simplex 求解线性方程组
- javascript - 无法从响应标头访问访问令牌和 uid
- matlab - 使用 plot 命令绘制任何原始数据矩阵的任何段,x 轴为时间,y 轴为电压(实际数据)
- r - 使用 plotly 跨动画帧的恒定热图
- audio - ffmpeg showspectrumpic fscale=log 不渲染右声道
- javascript - OnCreate 事件的确认对话框
- javascript - Expo 目前未启用对实验性语法“jsx”的支持
- sql - SQL:从另一个表中减去一个表中的值
- assembly - 如何在不同的操作系统和架构上运行相同的汇编程序?
- google-apps-script - 在 Google 表格的给定范围内,找到第一个空行