使用import_playbook时未定义的变量

时间:2019-04-11 17:42:28

标签: ansible

这是我的基本测试手册

---
- 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 ^这里\ n“}”

凭据.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

由于有两部戏,我试图避免在每次播放时让用户两次提示输入用户名和密码...

我是新手,很可能会遇到这个错误,但是在这里希望您能提供一些指导。

谢谢!

1 个答案:

答案 0 :(得分:0)

在游戏的varsvars_promptvars_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上进行。在您的示例中,您在本地主机上的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