我有 ansible playbook 可以在 Linux 服务器上执行更新。这本剧本运行良好。我想获取此剧本的输出并将其插入到 MySql 数据库中。下面是我的 ansible 剧本
---
- name: Running Updates on Amazon Linux 2
hosts: all
gather_facts: yes
tasks:
- name: Run yum update with exceptions
become: yes
yum:
name: '*'
state: latest
exclude: "httpd*,nginx*,mod24_ssl,php*,mod_ssl,nodejs*,npm*,mod_ldap*,ansible*,jenkins*,puppet*,varnish*,log4j*,java*,mysql*,mod_security*,libssh2*"
register: yum_update_output
- name: Display updated packages
debug:
var: yum_update_output.changes
- name: Parse JSON and Extract Relevant Information
set_fact:
extracted_changes: "{{ yum_update_output.changes }}"
- name: Debug extracted_changes.updated
debug:
var: extracted_changes.updated
- name: Store extracted changes in MySQL
mysql_db:
login_user: "username"
login_password: "password"
login_host: "host"
login_port: 3306
name: yum_update_data
state: query
with_together:
- "{{ extracted_changes.updated | map(attribute=0) | list }}"
- "{{ extracted_changes.updated | map(attribute=1) | list }}"
loop_var: package_name, version
query: >
INSERT INTO yum_update_changes (package_name, version, update_time)
VALUES ('{{ package_name }}', '{{ version }}', NOW())
下面是
extracted_changes.updated
的输出
ok: [server1] => {
"extracted_changes.updated": [
[
"libicu",
"50.2-4.amzn2.0.1.x86_64 from amzn2-core"
],
[
"ecs-init",
"1.74.1-1.amzn2.x86_64 from amzn2extra-ecs"
],
[
"iputils",
"20180629-11.amzn2.1.20160308.x86_64 from amzn2-core"
],
[
"python-requests",
"2.6.0-10.amzn2.0.1.noarch from amzn2-core"
],
[
"openldap",
"2.4.44-25.amzn2.0.7.x86_64 from amzn2-core"
],
[
"python-ipaddress",
"1.0.16-2.amzn2.0.2.noarch from amzn2-core"
],
[
"python3-pip",
"20.2.2-1.amzn2.0.4.noarch from amzn2-core"
],
[
"libcap",
"2.54-1.amzn2.0.2.x86_64 from amzn2-core"
],
[
"glib2",
"2.56.1-9.amzn2.0.6.x86_64 from amzn2-core"
]
]
}
当尝试在 mysql 中插入数据时,出现以下错误
{"msg": "The task includes an option with an undefined variable. The error was: 'package_name' is undefined\n\nThe error appears to be in 'updated-playbook.yml': line 30, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Store extracted changes in MySQL\n ^ here\n"}
我也尝试了以下方法,但仍然遇到相同的错误
- name: Store extracted changes in MySQL
mysql_db:
login_user: "username"
login_password: "password"
login_host: "host"
login_port: 3306
name: yum_update_data
state: query
loop: "{{ extracted_changes.updated }}"
loop_var: package_info
query: >
INSERT INTO yum_update_changes (package_name, version, update_time)
VALUES ('{{ package_info.0 }}', '{{ package_info.1 }}', NOW())
和
- name: Store extracted changes in MySQL
mysql_db:
login_user: "username"
login_password: "password"
login_host: "host"
login_port: 3306
name: yum_update_data
state: query
query: >
INSERT INTO yum_update_changes (package_name, version, update_time)
VALUES ('{{ item.0 }}', '{{ item.1 }}', NOW())
loop: "{{ extracted_changes.updated }}"
有人可以帮我吗?
代码太长,所以发布在这里。尝试这个修正后的版本,看看它是否适合你
- name: Running Updates on Amazon Linux 2
hosts: all
gather_facts: yes
tasks:
- name: Run yum update with exceptions
become: yes
yum:
name: '*'
state: latest
exclude: "httpd*,nginx*,mod24_ssl,php*,mod_ssl,nodejs*,npm*,mod_ldap*,ansible*,jenkins*,puppet*,varnish*,log4j*,java*,mysql*,mod_security*,libssh2*"
register: yum_update_output
- name: Display updated packages
debug:
var: yum_update_output.changes
- name: Parse JSON and Extract Relevant Information
set_fact:
extracted_changes: "{{ yum_update_output.changes }}"
- name: Store extracted changes in MySQL
mysql_db:
login_user: "username"
login_password: "password"
login_host: "host"
login_port: 3306
name: yum_update_data
state: query
loop: "{{ extracted_changes.updated }}"
query: >
INSERT INTO yum_update_changes (package_name, version, update_time)
VALUES ('{{ item.0 }}', '{{ item.1 }}', NOW())