0

I copied a task below that creates an object group on a Cisco router. Actually, I have lots of playbooks for different vendors, and I want to write all of the commands that I send with those playbooks to a MySQL table prepended with a client name and a function.

  tasks:

- name: Build prefix list
  asa_config:
    lines:
      - "object network {{ client_name }}"
      - "subnet {{ app_net }} {{ app_net_mask }}" 

- debug:
    msg: 
      - "object network {{ client_name }}"
      - "subnet {{ app_net }} {{ app_net_mask }}"
      
- name: Add data to mysql
  community.mysql.mysql_query:
    login_db: cmdb
    login_user: xxxx
    login_password: xxxx
    query:
    - INSERT INTO custcfg (cust,cfunc,comd) values ("{{ client_name }}", 'ASA', "{{ msg[0] }}")
    - INSERT INTO custcfg (cust,cfunc,comd) values ("{{ client_name }}", 'ASA', "{{ msg[1] }}")
    single_transaction: yes

The code above throws an error (not unsurprisingly):

FAILED! => {msg: "the task includes an option with an undefined variable. The error was: 'msg' is undefined."

I also tried using "lines[0]" and "lines[1]" and got the same error message.

I can't figure out how to take the commands that I send to the ASA with "lines" and turn those into variables. When I "Google" how to do this, I get flooded with answers about how to see the output from commands, NOT how to turn the commands themselves into writable values.

Well, I can think of one way that actually works, but it doesn't seem very efficient, and some of my playbooks have 100+ commands. This works:

   vars:
     c1: "object network {{ client_name }}"
     c2: "subnet {{ app_net }} {{ app_net_mask }}"

   tasks:
 
    - name: Build prefix list
      asa_config:
        lines:
          - "object network {{ client_name }}"
          - "subnet {{ app_net }} {{ app_net_mask }}" 

    - name: Add data to mysql
      community.mysql.mysql_query:
        login_db: cmdb
        login_user: xxxx
        login_password: xxxx
        query:
        - INSERT INTO custcfg (cust,cfunc,comd) values ("{{ client_name }}", 'ASA', "{{ c1 }}")
        - INSERT INTO custcfg (cust,cfunc,comd) values ("{{ client_name }}", 'ASA', "{{ c2 }}")
        single_transaction: yes
Dharman
  • 30,962
  • 25
  • 85
  • 135
SteveJ
  • 1
  • 3
  • 2
    You need to escape those values or you've got injection bugs. – tadman Apr 01 '21 at 21:32
  • 3
    This is probably better done by writing an extension to Ansible that can add this capability for logging on a lower level. – tadman Apr 01 '21 at 21:33
  • You can have a look at [this answer](https://stackoverflow.com/a/46903864/9401096) giving the bases to create an [action plugin](https://docs.ansible.com/ansible/latest/plugins/action.html) you could connect to [custom module](https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general.html) which could help you run the two modules in a single call with a single set of parameters. – Zeitounator Apr 02 '21 at 08:59
  • The [ARA](https://github.com/ansible-community/ara#how-it-works) project may interest you – mdaniel Apr 02 '21 at 14:49

0 Answers0