Ansible Модуль подтверждения - PullRequest
0 голосов
/ 01 мая 2020

Я пытаюсь использовать модуль assert в Ansible. Получается следующая ошибка:

Неустранимый: [S1]: FAILED! => {"msg": "Сбой условной проверки 'ip routing' в print_output.stdout_lines '. Ошибка: ошибка шаблона при шаблонной строке: ожидаемый токен' конец блока операторов ', получена' маршрутизация '. Строка: {% if ip routing 'в print_output.stdout_lines%} True {% else%} False {% endif%} "}

Я в основном пытаюсь проверить, был ли добавлен мой маршрут stati c в конфиг. Спасибо заранее за любые предложения. Вот моя пьеса:

---

### 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!"

Ответы [ 2 ]

0 голосов
/ 02 мая 2020

Итак, теперь у меня есть рабочий скрипт:

---

### 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!"
}

Просто хочу сделать скрипт короче, чтобы не было смысла сохранять его в файл, а затем читать из него. Скорее прочитал бы с него сразу, не сохраняя

0 голосов
/ 02 мая 2020

Как и указано очень четко в сообщении об ошибке, вы пропускаете открывающую кавычку:

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

YAML не применяет соответствующие кавычки, поэтому выводятся символы "a", "b", "c", "'" :

- debug:
   msg: abc'
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...