可靠断言模块



我正在尝试在Ansible中使用assert模块。得到以下错误:

致命:[S1]:失败!=>{"msg":"中的条件检查'ip-routing'print_output.stdout_lines'失败。错误为:模板错误,而模板字符串:应为标记"语句块末尾",得到"路由"。字符串:{%if ip routing‘in print_output.stdout_lines%}True{%else%}False{%endif%}"}

我基本上是在检查我的静态路由是否已添加到配置中。提前感谢您的任何建议。这是我的战术手册:

---
### Here I am trying to use assert on the file which was saved in a file
# Here we basically moving username and password to yaml_demo
- name: Take global command
hosts: S1
connection: local
gather_facts: no
vars_files:
- /home/patryk/Ansible-GNS3/default_route/yaml_demo.yaml
#./yaml_demo.yaml - if it's in the same location
vars_prompt:
# - name: USERNAME
#   prompt: "Please put your username"
#   private: no
# - name: DEVICE_PASSWORD
#   prompt: "Please put your password"
#   private: yes

- name: NEXT_HOP
prompt: "Please enter the IP of the Next HOP"
private: no

tasks:
- name: Add default route
eos_config:
provider:
host: "{{ inventory_hostname }}"
username: "{{ USERNAME }}"
password: "{{ DEVICE_PASSWORD }}"
use_ssl: no
authorize: yes
transport: cli
lines:
- "ip route 0.0.0.0/0 {{ NEXT_HOP }}"
- name: INPUT SHOW COMMAND TO DEVICES
eos_command:
commands: "show run"
provider:
host: "{{ inventory_hostname }}"
username: "{{ USERNAME }}"
password: "{{ DEVICE_PASSWORD }}"
use_ssl: no
authorize: yes
transport: cli
register: print_output
- name: OUTPUT SHOW COMMAND to SCREEN
debug:
msg: "{{ print_output.stdout_lines }}"
- name: OUTPUT SHOW COMMAND to FILE
copy: content="{{ print_output.stdout[0] }}" dest="./output/{{ inventory_hostname }}.txt"
- name: Get the whole config FILE
command: cat ./output/{{ inventory_hostname }}.txt
- name: Assert that the config was pushed succesfully
assert:
that:
- "'ip route 0.0.0.0/0 {{ NEXT_HOP }},' in {{ inventory_hostname }}.txt.stdout_lines"
success_msg: "Yes it has been pushed"
fail_msg: "It's not there!"

正如错误消息中非常清楚地所述,您缺少一个开场白:

{% if ip routing' in print_output.stdout_lines %} True {% else %} False {% endif %}

YAML不强制使用匹配的引号,因此它输出字符"a", "b", "c", "'":

- debug:
msg: abc'

所以我现在有了一个工作脚本:

---
### Here I am trying to use assert on the file which was saved in a file
# Here we basically moving username and password to yaml_demo
- name: Take global command
hosts: S1
connection: local
gather_facts: no
vars_files:
- /home/patryk/Ansible-GNS3/default_route/yaml_demo.yaml
#./yaml_demo.yaml - if it's in the same location
vars_prompt:
# - name: USERNAME
#   prompt: "Please put your username"
#   private: no
# - name: DEVICE_PASSWORD
#   prompt: "Please put your password"
#   private: yes

- name: NEXT_HOP
prompt: "Please enter the IP of the Next HOP"
private: no

tasks:
- name: Add default route
eos_config:
provider:
host: "{{ inventory_hostname }}"
username: "{{ USERNAME }}"
password: "{{ DEVICE_PASSWORD }}"
use_ssl: no
authorize: yes
transport: cli
lines:
- "ip route 0.0.0.0/0 {{ NEXT_HOP }}"
- name: INPUT SHOW COMMAND TO DEVICES
eos_command:
commands: "show run"
provider:
host: "{{ inventory_hostname }}"
username: "{{ USERNAME }}"
password: "{{ DEVICE_PASSWORD }}"
use_ssl: no
authorize: yes
transport: cli
register: print_output
- name: OUTPUT SHOW COMMAND to SCREEN
debug:
msg: "{{ print_output.stdout_lines }}"
- name: OUTPUT SHOW COMMAND to FILE
copy: content="{{ print_output.stdout[0] }}" dest="./output/{{ inventory_hostname }}.txt"
- name: Get the whole config FILE
command: cat ./output/{{ inventory_hostname }}.txt
register: postcheck
- name: Assert that the config was pushed succesfully
assert:
that:
- "'ip route 0.0.0.0/0 {{ NEXT_HOP }}' in postcheck.stdout_lines"
success_msg: "Yes it has been pushed"
fail_msg: "It's not there!"

但不确定为什么几乎完全相同的脚本不起作用:

---
### Here I am trying to use assert directly after printing output without saving to a file
# Here we basically moving username and password to yaml_demo
- name: Take global command
hosts: S1
connection: local
gather_facts: no
vars_files:
- /home/patryk/Ansible-GNS3/default_route/yaml_demo.yaml
#./yaml_demo.yaml - if it's in the same location
#  vars_prompt:
# - name: USERNAME
#   prompt: "Please put your username"
#   private: no
# - name: DEVICE_PASSWORD
#   prompt: "Please put your password"
#   private: yes

#- name: NEXT_HOP
#  prompt: "Please enter the IP of the Next HOP"
#    private: no

tasks:
- name: Add default route
eos_config:
provider:
host: "{{ inventory_hostname }}"
username: "{{ USERNAME }}"
password: "{{ DEVICE_PASSWORD }}"
use_ssl: no
authorize: yes
transport: cli
#    lines:
#      - "ip route 0.0.0.0/0 {{ NEXT_HOP }}"
- name: INPUT SHOW COMMAND TO DEVICES
eos_command:
commands: "show vlan"
provider:
host: "{{ inventory_hostname }}"
username: "{{ USERNAME }}"
password: "{{ DEVICE_PASSWORD }}"
use_ssl: no
authorize: yes
transport: cli
register: print_output
- name: OUTPUT SHOW COMMAND to SCREEN
debug:
msg: "{{ print_output.stdout_lines }}"
- name: Assert that ip route 0.0.0.0/0 55.5.5.5 is there
assert:
that:
- "'ip route 0.0.0.0/0 5.5.5.5' in print_output.stdout_lines"
success_msg: "Yes it has been pushed"
fail_msg: "It's not there!"

# - name: OUTPUT SHOW COMMAND to SCREEN
#   debug:
#       msg: "{{ print_output.stdout_lines }}"
# - name: Get the whole SW1.txt file
#   command: cat /home/patryk/Ansible-GNS3/default_route/output/S1.txt
#   register: SW1_config

得到以下错误:

fatal: [S1]: FAILED! => {
"assertion": "'ip route 0.0.0.0/0 5.5.5.5' in print_output.stdout_lines", 
"changed": false, 
"evaluated_to": false, 
"msg": "It's not there!"
}

只是想让脚本更短,这样就没有必要保存到文件中,然后从中读取。宁愿直接从中读取,而不保存

最新更新