我写了一部剧本来备份网络设备的配置。我的剧本有效。
- name: run backup configuration
hosts: HOST_NAME
gather_facts: no
connection: local
vars:
date: "{{ lookup('pipe', 'date +%Y%m%d') }}"
backup_dir: "/backup/backup-{{ date }}"
cli:
host: "{{ ansible_host }}"
username: <username>
password: <password>
tasks:
- sros_command:
commands: <command backup>
provider: "{{ cli }}"
register: config
- copy:
content: "{{ config.stdout[0] }}"
dest: "{{ backup_dir }}/backup_conf-{{ inventory_hostname }}.txt"
但是如果主机不可达或无法运行备份命令或任务失败。如果发生这种情况,我们可以编写剧本来创建XML或exec shell脚本吗?因为我必须向监视工具发送警报。
请帮助提供建议。
谢谢
答案 0 :(得分:0)
您可以在播放,阻止或任务级别使用ignore_errors
和ignore_unreachable
关键字。这将使播放继续进行,您可以将注册的结果用作将来任务的条件。
例如(在播放级别上):
- name: run backup configuration
hosts: HOST_NAME
gather_facts: no
connection: local
ignore_errors: yes
ignore_unreachable: yes
vars:
date: "{{ lookup('pipe', 'date +%Y%m%d') }}"
backup_dir: "/backup/backup-{{ date }}"
cli:
host: "{{ ansible_host }}"
username: <username>
password: <password>
tasks:
- sros_command:
commands: <command backup>
provider: "{{ cli }}"
register: config
- <task to send alarm notification>
when: config.unreachable
- copy:
content: "{{ config.stdout[0] }}"
dest: "{{ backup_dir }}/backup_conf-{{ inventory_hostname }}.txt"
when: config.unreachable is not defined
register: copy_result
- <task to send alarm notification>
when: copy_result.failed
请注意,您需要使用Ansible 2.7或更高版本才能使用ignore_unreachable