Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用Ansible格式化SQL查询_Python_Mysql_Ansible - Fatal编程技术网

Python 如何使用Ansible格式化SQL查询

Python 如何使用Ansible格式化SQL查询,python,mysql,ansible,Python,Mysql,Ansible,我正在尝试使用Ansible格式化SQL查询,以便以通用方式写入数据库中的表。我现在的代码是: 1 --- 2 - hosts: all 3 vars: 4 inv_list: [] 5 compliance_info: 6 host: "{{ ansible_host }}" 7 passwd_change: "{{ root_pass_chg }}" 8 timestamp: "{{ ansible_da

我正在尝试使用Ansible格式化SQL查询,以便以通用方式写入数据库中的表。我现在的代码是:

  1 ---
  2 - hosts: all
  3   vars:
  4     inv_list: []
  5     compliance_info:
  6       host: "{{ ansible_host }}"
  7       passwd_change: "{{ root_pass_chg }}"
  8       timestamp: "{{ ansible_date_time.date }}"
  9   tasks:
 10     - name: get password change date
 11       command: sudo chage -l root
 12       register: chage_output
 13
 14     - name: pattern
 15       set_fact:
 16         regx: '\w\w\w\s\d\d\,\s\d\d\d\d'
 17
 18     - name : regex
 19       set_fact:
 20         root_pass_chg: "{{ chage_output.stdout | regex_search(regx, multiline=true, ignorecase=true) }}"
 21
 22     - name: build dict
 23       set_fact:
 24         client_compliance: "{{ client_compliance | default({}) | combine({ inventory_hostname : compliance_info }, recursive=True ) }}"
 25
 26     - name: build inventory list
 27       set_fact:
 28         inv_list: "{{ inv_list + ansible_play_batch }}"
 29 #this relies on whether or not client01 has build inv_list. if connection fails then this task fails
 30 - hosts: localhost
 31   vars:
 32     headers: [ host, passwd_change, timestamp ]
 33     outputs: "{{ headers | map('extract', compliance_dict['client01']) | list }}"
 34     results: "{{ outputs | wrap | join(', ') }}"
 35   tasks:
 36     - name: build dict
 37       set_fact:
 38         compliance_dict: "{{ compliance_dict | default({}) | combine(hostvars[item].client_compliance) }}"
 39       with_items:
 40         - "{{ hostvars['client01'].inv_list }}"
 41
 42     - debug:
 43         var: outputs
 44
 45     - debug:
 46         var: results
 47
 48     - name: write stuff to mysql
 49       command: sudo mysql --user=user --password=password compliance --host=localhost --execute="insert into root_pass({{ headers|join(', ') }}) values( {{ results }} ) on duplicate key update {{ headers|join(', ') }} = {{ results }}    "
 50       with_items:
 51         - "{{ hostvars['client01'].inv_list }}"`
结果的输出是

ok: [localhost] => {
"\"testserver.net\", \"some date, 2019\", \"2019-xx-xx\"": "('testserver.net', 'some date, 2019', '2019-xx-xx')"}
我在输出上使用的wrap过滤器来自我使用的自定义插件,定义为

  1 def wrap(list):
  2         return [ '"' + x + '"' for x in list ]
  3
  4 class FilterModule(object):
  5         def filters(self):
  6                 return {
  7                         'wrap': wrap
  8                 }

在要插入到表中的值周围放置单引号时需要使用。我是python新手,因此我不确定如何编写自定义插件,使其不会返回key:value对,而是一个单引号值字符串。注意到您使用的是命令而不是mysql\u db模块。您是否仅尝试实现一个查询?如果是-这将提供更好的内置模块。如果您想添加自定义输入,您可以使用jinja2 templateHi创建sql,谢谢您的回复。我可能在文档中遗漏了一些东西,但mysql_db似乎不允许您实现查询。它似乎用于添加/删除数据库或从转储文件读取/写入。我只是不确定我将如何使用该模块来编写我的表。正是这样,在sql查询本身中,您可以编写查询并执行:)希望这能使解决方案变得清晰我很抱歉,如果我觉得有点密集,但Ansible和sql对我来说都是新的。我不明白mysql_db如何允许您编写查询。我想我错过了这里的核心概念。