# I want this playbook to skip systems that erro out due to missing tables

**URL:** <https://forum.ansible.com/t/i-want-this-playbook-to-skip-systems-that-erro-out-due-to-missing-tables/5308>\
**Category:** Get Help\
**Tags:** mysql, playbook\
**Created:** [May 6, 2024, 4:43pm UTC](https://forum.ansible.com/t/i-want-this-playbook-to-skip-systems-that-erro-out-due-to-missing-tables/5308 "2024-05-06T16:43:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RyanAccent](https://avatars.discourse-cdn.com/v4/letter/r/b77776/32.png) [@RyanAccent](https://forum.ansible.com/u/RyanAccent)\
**Post date:** [May 6, 2024, 4:43pm UTC](https://forum.ansible.com/t/i-want-this-playbook-to-skip-systems-that-erro-out-due-to-missing-tables/5308/1 "2024-05-06T16:43:58Z")

</div>

I have aplaybook that is designed to keep 12 month’s worth of SQL Table data and dump the rest. When I run the playbook against multiple systems any system that errors out is not included in subsequent tasks. Everything else seems to be working but I am looking to check if the tables exists and if they don’t just skip the subsequent tasks related to that table

* * *

- name: Move Asterisk CDR Data  
hosts: myhosts  
tasks:
  - name: Stop FWConsole  
shell: fwconsole stop

  - name: Enter MySQL  
community.mysql.mysql\_db:  
login\_host: localhost  
name: temporary  
state: present

  - name: Get total number of records in cdr table  
community.mysql.mysql\_query:  
login\_host: localhost  
query: “SELECT COUNT(\*) AS cdr\_count FROM asteriskcdrdb.cdr”  
register: cdr\_count

  - name: Get total number of records in cdr table for the last 12 months  
community.mysql.mysql\_query:  
login\_host: localhost  
query: “SELECT COUNT(\*) AS cdr\_12month\_count FROM asteriskcdrdb.cdr WHERE calldate \>= DATE\_SUB(NOW(), INTERVAL 12 MONTH)”  
register: cdr\_12month\_count

  - name: Create temporary CDR table  
community.mysql.mysql\_query:  
login\_host: localhost  
query: “CREATE TABLE IF NOT EXISTS temporary.cdr LIKE asteriskcdrdb.cdr”

  - name: Copy CDR data to temporary table  
community.mysql.mysql\_query:  
login\_host: localhost  
query: “INSERT INTO temporary.cdr SELECT \* FROM asteriskcdrdb.cdr WHERE calldate \>= DATE\_SUB(NOW(), INTERVAL 12 MONTH)”

  - name: Get total number of records in cel table  
community.mysql.mysql\_query:  
login\_host: localhost  
query: “SELECT COUNT(\*) AS cel\_count FROM asteriskcdrdb.cel”  
register: cel\_count

  - name: Get total number of records in cel table for the last 12 months  
community.mysql.mysql\_query:  
login\_host: localhost  
query: “SELECT COUNT(\*) AS cel\_12month\_count FROM asteriskcdrdb.cel WHERE eventtime \>= DATE\_SUB(NOW(), INTERVAL 12 MONTH)”  
register: cel\_12month\_count

  - name: Create temporary CEL table  
community.mysql.mysql\_query:  
login\_host: localhost  
query: “CREATE TABLE IF NOT EXISTS temporary.cel LIKE asteriskcdrdb.cel”  
ignore\_errors: yes

  - name: Copy CEL data to temporary table  
community.mysql.mysql\_query:  
login\_host: localhost  
query: “INSERT INTO temporary.cel SELECT \* FROM asteriskcdrdb.cel WHERE eventtime \>= DATE\_SUB(NOW(), INTERVAL 12 MONTH)”  
ignore\_errors: yes

  - name: Get total number of records in transient\_cdr table  
community.mysql.mysql\_query:  
login\_host: localhost  
query: “SELECT COUNT(\*) AS transient\_cdr\_count FROM asteriskcdrdb.transient\_cdr”  
register: transient\_cdr\_count

  - name: Get total number of records in transient\_cdr table for the last 12 months  
community.mysql.mysql\_query:  
login\_host: localhost  
query: “SELECT COUNT(\*) AS transient\_cdr\_12month\_count FROM asteriskcdrdb.transient\_cdr WHERE calldate \>= DATE\_SUB(NOW(), INTERVAL 12 MONTH)”  
register: transient\_cdr\_12month\_count

  - name: Create temporary transient\_cdr table  
community.mysql.mysql\_query:  
login\_host: localhost  
query: “CREATE TABLE IF NOT EXISTS temporary.transient\_cdr LIKE asteriskcdrdb.transient\_cdr”  
ignore\_errors: yes

  - name: Copy transient\_cdr data to temporary table  
community.mysql.mysql\_query:  
login\_host: localhost  
query: “INSERT INTO temporary.transient\_cdr SELECT \* FROM asteriskcdrdb.transient\_cdr WHERE calldate \>= DATE\_SUB(NOW(), INTERVAL 12 MONTH)”  
ignore\_errors: yes

  - name: Drop and recreate asteriskcdrdb database  
community.mysql.mysql\_db:  
login\_host: localhost  
name: asteriskcdrdb  
state: absent

  - name: Recreate asteriskcdrdb database  
community.mysql.mysql\_db:  
login\_host: localhost  
name: asteriskcdrdb  
state: present

  - name: Recreate asteriskcdrdb.cdr Table  
community.mysql.mysql\_query:  
login\_host: localhost  
query: “CREATE TABLE IF NOT EXISTS asteriskcdrdb.cdr LIKE temporary.cdr”

  - name: Recreate asteriskcdrdb.cel Table  
community.mysql.mysql\_query:  
login\_host: localhost  
query: “CREATE TABLE IF NOT EXISTS asteriskcdrdb.cel LIKE temporary.cel”  
ignore\_errors: yes

  - name: Recreate asteriskcdrdb.transient\_cdr Table  
community.mysql.mysql\_query:  
login\_host: localhost  
query: “CREATE TABLE IF NOT EXISTS asteriskcdrdb.transient\_cdr LIKE temporary.transient\_cdr”  
ignore\_errors: yes

  - name: Copy data from temporary tables to asteriskcdrdb  
community.mysql.mysql\_query:  
login\_host: localhost  
query: “INSERT INTO asteriskcdrdb.cdr SELECT \* FROM temporary.cdr”

  - name: Copy data from temporary tables to asteriskcdrdb  
community.mysql.mysql\_query:  
login\_host: localhost  
query: “INSERT INTO asteriskcdrdb.cel SELECT \* FROM temporary.cel”  
ignore\_errors: yes

  - name: Copy data from temporary tables to asteriskcdrdb  
community.mysql.mysql\_query:  
login\_host: localhost  
query: "INSERT INTO asteriskcdrdb.transient\_cdr SELECT \* FROM temporary.transient\_cdr "  
ignore\_errors: yes

  - name: Drop temporary database  
community.mysql.mysql\_db:  
login\_host: localhost  
name: temporary  
state: absent

  - debug:  
msg: “CDR Count: {{ cdr\_count.query\_result[0][0].cdr\_count }}, CDR 12Month Count: {{ cdr\_12month\_count.query\_result[0][0].cdr\_12month\_count }}”

  - name: Calculate difference between cdr\_count and cdr\_12month\_count  
set\_fact:  
cdr\_difference: “{{ cdr\_count.query\_result[0][0].cdr\_count - cdr\_12month\_count.query\_result[0][0].cdr\_12month\_count }}”

  - debug:  
msg: “CDR Difference: {{ cdr\_difference }}”

  - debug:  
msg: “cel Count: {{ cel\_count.query\_result[0][0].cel\_count }}, cel 12Month Count: {{ cel\_12month\_count.query\_result[0][0].cel\_12month\_count }}”

  - name: Calculate difference between cel\_count and cel\_12month\_count  
set\_fact:  
cel\_difference: “{{ cel\_count.query\_result[0][0].cel\_count - cel\_12month\_count.query\_result[0][0].cel\_12month\_count }}”

  - debug:  
msg: “cel Difference: {{ cel\_difference }}”

  - debug:  
msg: “transient\_cdr Count: {{ transient\_cdr\_count.query\_result[0][0].transient\_cdr\_count }}, transient\_cdr 12Month Count: {{ transient\_cdr\_12month\_count.query\_result[0][0].transient\_cdr\_12month\_count }}”

  - name: Calculate difference between transient\_cdr\_count and transient\_cdr\_12month\_count  
set\_fact:  
transient\_cdr\_difference: “{{ transient\_cdr\_count.query\_result[0][0].transient\_cdr\_count - transient\_cdr\_12month\_count.query\_result[0][0].transient\_cdr\_12month\_count }}”

  - debug:  
msg: “transient\_cdr Difference: {{ transient\_cdr\_difference }}”

  - name: Start FWConsole  
shell: fwconsole start  
register: fwconsole\_output

  - debug:  
var: fwconsole\_output.stdout\_lines

  - name: Check Asterisk  
shell: ps -ef | grep -v grep | grep ‘/usr/sbin/asterisk’  
register: asterisk\_status

  - debug:  
var: asterisk\_status.stdout\_lines

---

<div class="post-metadata">

**Author:** ![mariolenz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mariolenz/32/108_2.png) [@mariolenz](https://forum.ansible.com/u/mariolenz)\
**Post date:** [May 6, 2024, 6:03pm UTC](https://forum.ansible.com/t/i-want-this-playbook-to-skip-systems-that-erro-out-due-to-missing-tables/5308/2 "2024-05-06T18:03:56Z")

</div>

I’m not 100% sure if I understand what you’re trying to do. But maybe moving some of your tasks to a [block](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_blocks.html) and define `ignore_errors: true` might help.

You could also try to use a block and define `always` for stuff you want to be done no matter if the table exists or not.

As I’ve said, I’m not really sure what you’re trying to do. But maybe this helps.

---

<div class="post-metadata">

**Author:** ![RyanAccent](https://avatars.discourse-cdn.com/v4/letter/r/b77776/32.png) [@RyanAccent](https://forum.ansible.com/u/RyanAccent)\
**Post date:** [May 6, 2024, 6:21pm UTC](https://forum.ansible.com/t/i-want-this-playbook-to-skip-systems-that-erro-out-due-to-missing-tables/5308/3 "2024-05-06T18:21:21Z")

</div>

Some testing between the post and you’re reply appears you are dead on despite claiming to not understand. I wa absolutely missing some ignore\_errors commands on other tasks. Thank you!
