Module 可转换断言模块
我试图在Ansible中使用assert模块。 获取以下错误:Module 可转换断言模块,module,ansible,network-programming,Module,Ansible,Network Programming,我试图在Ansible中使用assert模块。 获取以下错误: 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!" } 致命:[S1]:失败!=>{“msg”:“中的条件检查
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!"
}
致命:[S1]:失败!=>{“msg”:“中的条件检查“ip路由”
打印输出。标准输出行失败。错误为:模板错误
模板字符串:应为标记“语句块结尾”,已获取
“路由”。字符串:{%if ip routing'在print_output.stdout_line%}
真{%else%}假{%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”和“'”
:
-调试:
味精: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!"
}
只是想缩短脚本,这样就没有必要保存到文件中,然后从中读取。我宁愿直接阅读而不保存它嗨,欢迎来到SO。错误中提到的失败条件检查不在示例剧本中。你确定你粘贴了好的剧本和/或错误吗?除此之外,请花一些时间阅读并尝试将您的示例限制为最小的数据集和失败的任务,任何人都可以轻松地在自己的机器上玩,以帮助您解决问题。谢谢