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

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

I’m not 100% sure if I understand what you’re trying to do. But maybe moving some of your tasks to a block 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.

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!