Итак, теперь у меня есть рабочий скрипт:
---
### 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!"
}
Просто хочу сделать скрипт короче, чтобы не было смысла сохранять его в файл, а затем читать из него. Скорее прочитал бы с него сразу, не сохраняя