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