这是我的一些基本测试的剧本
---
- name: get username and password
import_playbook: credentials.yaml
- name: Run some commands
hosts:
- qfx5100-48s
roles:
- Juniper.junos
vars:
ansible_python_interpreter: "/opt/ansible/ansible-venv/bin/python"
connection: local
gather_facts: no
tasks:
- name: get uptime
juniper_junos_command:
commands:
- show system uptime
provider:
host: "{{ ansible_host }}"
port: 22
user: "{{ username }}"
passwd: "{{ password }}"
register: uptime
- name: Run some other commands
hosts:
- ex3300-48t
roles:
- Juniper.junos
vars:
ansible_python_interpreter: "/opt/ansible/ansible-venv/bin/python"
connection: local
gather_facts: no
tasks:
- name: get uptime
juniper_junos_command:
commands:
- show system alarms
provider:
host: "{{ ansible_host }}"
port: 22
user: "{{ username }}"
passwd: "{{ password }}"
register: alarms
- name: display uptime
debug:
var: uptime.stdout_lines
- name: display alarms
debug:
var: alarms.stdout_lines
当我运行这个我得到这个错误:
“”任务包括一个带有未定义变量的选项。错误是:'密码'未定义\ n \ n错误似乎位于'/opt/ansible/commands2.yaml':第15行第7列,但可能在文件的其他位置,具体取决于确切的语法问题。\ n \ n违规行似乎是:\ n \ n任务:\ n - 名称:获取正常运行时间\ n ^此处\ n“}”
credentials.yaml文件是:
- name: Get username and password
hosts: localhost
vars:
ansible_python_interpreter: "/opt/ansible/ansible-venv/bin/python"
connection: local
gather_facts: no
vars_prompt:
- name: username
prompt: Junos Username
private: no
- name: password
prompt: Junos Password
private: yes
由于有两个游戏,我试图避免在每个游戏运行时用户提示他们的用户名和密码两次...
我很想回答我接近这个错误的机会,但是我会在这里得到一些指导。
谢谢!
在vars
或vars_prompt
或vars_files
部分中设置的变量将限定在该剧中:它们将不会在后续剧中出现。例如。
---
- hosts: localhost
gather_facts: false
vars:
example: foo
- hosts: localhost
gather_facts: false
tasks:
- debug:
var: example|default('<NOT SET>')
将输出:
TASK [debug] **********************************************************************************************************************************************************************************
ok: [localhost] => {
"example|default('<NOT SET>')": "<NOT SET>"
}
但是如果你使用该变量在主机上设置事实,它将在后续播放中作为hostvar使用。就是这样:
---
- hosts: localhost
gather_facts: false
vars:
example: foo
tasks:
- set_fact:
example: "{{ example }}"
- hosts: localhost
gather_facts: false
tasks:
- debug:
var: example|default('<NOT SET>')
将输出:
TASK [debug] **********************************************************************************************************************************************************************************
ok: [localhost] => {
"example|default('<NOT SET>')": "foo"
}
在这个例子中,我能够引用{{ example }}
,因为事实是在localhost
上设置的,第二个游戏也在localhost
上运行。在您的示例中,您在localhost上运行credentials.yml
中的游戏但尝试在另一台主机上运行的游戏中使用该值时,您需要将其称为:
{{ hostvars['localhost'].example }}
为了完整,你的credentials.yml
看起来像:
---
- hosts: localhost
gather_facts: false
vars_prompt:
- name: username
prompt: Junos Username
private: no
- name: password
prompt: Junos Password
private: yes
tasks:
- set_fact:
username: "{{ username }}"
password: "{{ password }}"
而你的playbook.yml
看起来像:
---
- name: get username and password
import_playbook: credentials.yml
- name: Run some commands
hosts:
- qfx5100-48s
roles:
- Juniper.junos
vars:
ansible_python_interpreter: "/opt/ansible/ansible-venv/bin/python"
connection: local
gather_facts: no
tasks:
- name: get uptime
juniper_junos_command:
commands:
- show system uptime
provider:
host: "{{ ansible_host }}"
port: 22
user: "{{ hostvars['localhost'].username }}"
passwd: "{{ hostvars['localhost'].password }}"
register: uptime