Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.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 playbook输出生成CSV文件报告(干运行)_Python_Shell_Ansible - Fatal编程技术网

Python 需要通过在检查模式下执行的ansible playbook输出生成CSV文件报告(干运行)

Python 需要通过在检查模式下执行的ansible playbook输出生成CSV文件报告(干运行),python,shell,ansible,Python,Shell,Ansible,我正在运行一个角色,使用ansible进行系统强化,这看起来不错,希望在执行剧本之前生成一个包含以下字段的“CSV”文件—{IP_地址、任务名称、状态(确定或更改)。 想法是在执行前获得一份报告 下面是剧本的示例输出: TASK [system_hardening : Include OS Specific Variables] ************************ ok: [192.168.10.10] TASK [system_hardening : Configure NTP

我正在运行一个角色,使用ansible进行系统强化,这看起来不错,希望在执行剧本之前生成一个包含以下字段的“CSV”文件—{IP_地址、任务名称、状态(确定或更改)。 想法是在执行前获得一份报告

下面是剧本的示例输出:

TASK [system_hardening : Include OS Specific Variables] ************************
ok: [192.168.10.10]

TASK [system_hardening : Configure NTP - Install Package] **********************
ok: [192.168.10.10]

TASK [system_hardening : Disable chronyd services] *****************************
ok: [192.168.10.10]

TASK [system_hardening : Set some kernel parameters] ***************************
changed: [192.168.10.10] => (item={u'regexp': u'server 0.rhel.pool.ntp.org iburst', u'line': u'server google.com iburst'})
changed: [192.168.10.10] => (item={u'regexp': u'server 1.rhel.pool.ntp.org iburst', u'line': u'server  google.com iburst'})
changed: [192.168.10.10] => (item={u'regexp': u'server 2.rhel.pool.ntp.org iburst', u'line': u'server  google.com iburst'})
changed: [192.168.10.10] => (item={u'regexp': u'server 3.rhel.pool.ntp.org iburst', u'line': u'server  google.com iburst'})

TASK [system_hardening : restart ntp services] *********************************

changed: [192.168.10.10]

TASK [system_hardening : Set Password Requirement Parameters Using pam_cracklib (Install)] ***
ok: [192.168.10.10]

TASK [system_hardening : Ensure password reuse is limited (/etc/pam.d/system-auth)] ***
changed: [192.168.10.10]

TASK [system_hardening : Ensure password reuse is limited (/etc/pam.d/password-auth)] ***
changed: [192.168.10.10]
所需CSV为

IP_Address,Task_Name, Status
192.168.10.10 ,SSH_Checks ,ok
192.168.10.11, SSH_Checks,changed
有什么意见吗

ansible-doc -t callback -l
列出可用的插件。如果没有合适的插件,则有一个选项

列出可用的插件。如果没有合适的插件,则有一个选项。

我看到有一个。它设置Ansible以显示通常以JSON格式在屏幕上显示的输出。您可能会接收JSON数据并轻松将其转换为CSV。下面是一篇文章

以下是帖子中的脚本:

# Python program to convert 
# JSON file to CSV 


import json 
import csv 


# Opening JSON file and loading the data 
# into the variable data 
with open('data.json') as json_file: 
    data = json.load(json_file) 

employee_data = data['emp_details'] 

# now we will open a file for writing 
data_file = open('data_file.csv', 'w') 

# create the csv writer object 
csv_writer = csv.writer(data_file) 

# Counter variable used for writing 
# headers to the CSV file 
count = 0

for emp in employee_data: 
    if count == 0: 

        # Writing headers of CSV file 
        header = emp.keys() 
        csv_writer.writerow(header) 
        count += 1

    # Writing data of CSV file 
    csv_writer.writerow(emp.values()) 

data_file.close() 
我看到有一个。它将Ansible设置为以JSON格式显示通常在屏幕上显示的输出。您可能会接收JSON数据并轻松将其转换为CSV。下面是一篇文章

以下是帖子中的脚本:

# Python program to convert 
# JSON file to CSV 


import json 
import csv 


# Opening JSON file and loading the data 
# into the variable data 
with open('data.json') as json_file: 
    data = json.load(json_file) 

employee_data = data['emp_details'] 

# now we will open a file for writing 
data_file = open('data_file.csv', 'w') 

# create the csv writer object 
csv_writer = csv.writer(data_file) 

# Counter variable used for writing 
# headers to the CSV file 
count = 0

for emp in employee_data: 
    if count == 0: 

        # Writing headers of CSV file 
        header = emp.keys() 
        csv_writer.writerow(header) 
        count += 1

    # Writing data of CSV file 
    csv_writer.writerow(emp.values()) 

data_file.close() 

我现在不知道有什么具体的解决方案,但是看看ansible回调插件。这些插件可以用来以不同的方式格式化ansible playbook命令的输出。或者在提供的平台上使用一个(如果您想使用它,可以使用json输出插件),或编写自己的:我现在不知道任何具体的解决方案,但请看一看ansible回调插件。这些插件可用于以不同方式格式化ansible playbook命令的输出。可以在提供的平台上使用一个(如果您想使用它,可以使用json输出插件),或者自己写:感谢回复,从现在开始,如果我们可以通过shell脚本实现,那就太好了。我们也将尝试插件。感谢回复,从现在开始,如果我们可以通过shell脚本实现,那就太好了。我们也将尝试插件。