我有这个剧本脚本来在交换机和防火墙上执行一些基本任务。
家长:
- hosts: localhost
become: no
gather_facts: no
vars_prompt:
- name: ansible_user
prompt: "Enter Username"
private: no
- name: ansible_password
prompt: "Enter Password"
private: yes
- name: Execute Script on Switch
import_playbook: test_dummy_switch.yml
- name: Execute Script on Firewall
import_playbook: test_dummy_fw.yml
子 1 / 开关:test_dummy_coreit.yml
- hosts: Switch
become: no
gather_facts: no
vars_prompt:
- name: ansible_user
prompt: "Enter Username"
private: no
- name: ansible_password
prompt: "Enter Password"
private: yes
tasks:
- name: Interface Config
ios_config:
lines:
- description Testing Switch
parents: "interface Gi0/0"
register: print_output
- debug: var=print_output
子 2 / 防火墙:test_dummy_fw.yml
- hosts: Firewall
become: no
gather_facts: no
vars_prompt:
- name: ansible_user
prompt: "Enter Username"
private: no
- name: ansible_password
prompt: "Enter Password"
private: yes
tasks:
- name: Interface Config
ios_config:
lines:
- description Testing Firewall
parents: "interface Ethernet1"
register: print_output
- debug: var=print_output
我想创建一个脚本来在父剧本中设置变量时跳过提示。我也在 playbook 上尝试过此操作,但 Ansible 仍然提示输入子 playbook 中的变量。
家长:
- hosts: localhost
become: no
gather_facts: no
vars_prompt:
- name: ansible_user
prompt: "Enter Username"
private: no
- name: ansible_password
prompt: "Enter Password"
private: yes
tasks:
- set_fact:
ansible_user: "{{ ansible_user }}"
ansible_password: "{{ ansible_password }}"
- name: Execute Script on Switch
import_playbook: test_dummy_switch.yml
vars:
ansible_user: "{{ ansible_user|d('default') }}"
ansible_password: "{{ ansible_password|d('default') }}"
- name: Execute Script on Firewall
import_playbook: test_dummy_fw.yml
vars:
ansible_user: "{{ ansible_user|d('default') }}"
ansible_password: "{{ ansible_password|d('default') }}"
有什么方法可以确保 Ansible 识别该变量已经存在并跳过提示而不使用额外的变量?因为这个脚本的要求只需要提示一次用户名和密码。
例如:
shell> cat pb1.yml
- hosts: localhost
vars_prompt:
- name: ansible_user
prompt: Enter Username
private: false
- name: ansible_password
prompt: Enter Password
private: true
tasks:
- set_fact:
ansible_user: "{{ ansible_user }}"
ansible_password: "{{ ansible_password }}"
- import_playbook: pb2.yml
- import_playbook: pb3.yml
shell> cat pb2.yml
- hosts: localhost
pre_tasks:
- when: ansible_user|length == 0
block:
- pause:
prompt: Enter Username
register: out
- set_fact:
ansible_user: "{{ out.user_input }}"
- when: ansible_password|length == 0
block:
- pause:
prompt: Enter Password
register: out
- set_fact:
ansible_password: "{{ out.user_input }}"
tasks:
- debug:
msg: |
ansible_user: {{ ansible_user }}
ansible_password: {{ ansible_password }}