我的剧本 yml 脚本如下:
---
- name: Download files from FTP
hosts: localhost
gather_facts: false
vars_prompt:
- name: target_host
prompt: "Target Host IP: "
- name: auth_method
prompt: "Authentication method (password/key): "
tasks:
- name: Gather target host credentials
when: auth_method == 'password'
vars_prompt:
- name: ssh_user
prompt: "SSH Username: "
- name: ssh_pass
prompt: "SSH Password: "
private: true
- name: Gather key path for target host
when: auth_method == 'key'
vars_prompt:
- name: ssh_user
prompt: "SSH Username: "
- name: key_path
prompt: "Path to SSH Key: "
- name: Gather FTP credentials
vars_prompt:
- name: ftp_username
prompt: "FTP Username: "
- name: ftp_password
prompt: "FTP Password: "
private: true
- name: Download files from FTP on target host
hosts: "{{ target_host }}"
gather_facts: false
remote_user: "{{ ssh_user }}"
become: true
become_user: root
become_method: sudo
tasks:
- name: Create upgrade directory if not exist
file:
path: /root/upgrade
state: directory
- name: Download file from FTP
get_url:
url: "ftp://something.com/{{ file_path }}"
dest: "/root/upgrade/{{ file_path }}"
force: yes
timeout: 30
remote_src: yes
validate_certs: false
url_username: "{{ ftp_username }}"
url_password: "{{ ftp_password }}"
register: download_result
ignore_errors: true
- name: Check if file downloaded successfully
debug:
msg: "File '{{ file_path }}' downloaded successfully."
when: download_result is success
- name: Display error message if file download failed
debug:
msg: "Failed to download file '{{ file_path }}'. Please check the file path or FTP credentials."
when: download_result is failed and file_path != 'q'
- name: Exit playbook if 'q' is pressed
meta: end_play
when: file_path == 'q'
我检查了缩进,一切似乎都正确。但当我尝试运行它时,它显示以下错误:
root@DESKTOP-VLCQSJO:/home/ridhoswasta/tassta-automation# ansible-playbook download-files.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match
'all'
ERROR! unexpected parameter type in action: <class 'ansible.parsing.yaml.objects.AnsibleSequence'>
The error appears to be in '/home/ridhoswasta/tassta-automation/download-files.yml': line 13, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
- name: Gather target host credentials
^ here
root@DESKTOP-VL
有什么建议吗?谢谢!
答案1
vars_prompt只能在剧中使用。没有任务、块或角色。参见剧本关键词. 在任务中,您可以使用暂停相反。例如,
shell> cat pb.yml
- hosts: localhost
vars_prompt:
- name: target_host
prompt: Target Host IP
private: false
- name: auth_method
prompt: Authentication method (password/key)
private: false
vars:
ssh_user: "{{ ssh_user_out.user_input }}"
ssh_pass: "{{ ssh_pass_out.user_input }}"
tasks:
- block:
- pause:
prompt: SSH Username
register: ssh_user_out
- pause:
prompt: SSH Password
echo: false
register: ssh_pass_out
- debug:
msg: |
target_host: {{ target_host }}
auth_method: {{ auth_method }}
ssh_user: {{ ssh_user }}
ssh_pass: {{ ssh_pass }}
when: auth_method == 'password'
给出
shell> ansible-playbook pb.yml
Target Host IP: 10.1.0.10
Authentication method (password/key): password
PLAY [localhost] ******************************************************************************
TASK [pause] **********************************************************************************
[pause]
SSH Username:
admin^Mok: [localhost]
TASK [pause] **********************************************************************************
[pause]
SSH Password (output is hidden):
ok: [localhost]
TASK [debug] **********************************************************************************
ok: [localhost] =>
msg: |-
target_host: 10.1.0.10
auth_method: password
ssh_user: admin
ssh_pass: 1234
PLAY RECAP ************************************************************************************
localhost: ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0