write_file

Write contents to a file (locally)

Example

[1]:
from nornir import InitNornir
from nornir_utils.plugins.tasks.files import write_file
from nornir_utils.plugins.functions import print_result

nr = InitNornir(
    inventory={
        "plugin": "SimpleInventory",
        "options": {"host_file": "data/hosts.yaml", "group_file": "data/groups.yaml"},
    }
)

def generate_configurations(task):
    task.run(
        task=write_file,
        filename=f"out_files/{task.host}-config.txt",
        content="\n\n---example configuration to write out---\n\n\n"
    )

r = nr.run(
    task=generate_configurations
)

print_result(r)
generate_configurations*********************************************************
* dev1.group_1 ** changed : True ***********************************************
vvvv generate_configurations ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
---- write_file ** changed : True ---------------------------------------------- INFO
--- out_files/dev1.group_1-config.txt

+++ new

@@ -0,0 +1,5 @@

+
+
+---example configuration to write out---
+
+
^^^^ END generate_configurations ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev2.group_1 ** changed : True ***********************************************
vvvv generate_configurations ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
---- write_file ** changed : True ---------------------------------------------- INFO
--- out_files/dev2.group_1-config.txt

+++ new

@@ -0,0 +1,5 @@

+
+
+---example configuration to write out---
+
+
^^^^ END generate_configurations ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev3.group_2 ** changed : True ***********************************************
vvvv generate_configurations ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
---- write_file ** changed : True ---------------------------------------------- INFO
--- out_files/dev3.group_2-config.txt

+++ new

@@ -0,0 +1,5 @@

+
+
+---example configuration to write out---
+
+
^^^^ END generate_configurations ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev4.group_2 ** changed : True ***********************************************
vvvv generate_configurations ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
---- write_file ** changed : True ---------------------------------------------- INFO
--- out_files/dev4.group_2-config.txt

+++ new

@@ -0,0 +1,5 @@

+
+
+---example configuration to write out---
+
+
^^^^ END generate_configurations ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* dev5.no_group ** changed : True **********************************************
vvvv generate_configurations ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
---- write_file ** changed : True ---------------------------------------------- INFO
--- out_files/dev5.no_group-config.txt

+++ new

@@ -0,0 +1,5 @@

+
+
+---example configuration to write out---
+
+
^^^^ END generate_configurations ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^