Skip to content

Commit

Permalink
add loop like ansible
Browse files Browse the repository at this point in the history
  • Loading branch information
flawiddsouza committed Nov 12, 2024
1 parent 25d9196 commit 58829ff
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 32 deletions.
75 changes: 43 additions & 32 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct Task {
vars: Option<IndexMap<String, String>>,
chdir: Option<String>,
when: Option<String>,
r#loop: Option<Vec<Value>>,
}

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -572,41 +573,51 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Debug print to verify vars_map
// println!("Vars map: {:?}", vars_map);

if let Some(debug) = &task.debug {
println!("{}", "Debug:".blue());
for (key, msg) in debug.0.iter() {
println!("{}", format!("{}:", key).blue());
let debug_msg = replace_placeholders(msg, &register_map, &vars_map);
println!("{}", format!("{}", debug_msg).blue());
let loop_items = task.r#loop.clone().unwrap_or_else(|| vec![Value::Null]);

for item in loop_items {
let mut local_vars_map = vars_map.clone();

if !item.is_null() {
local_vars_map.insert("item".to_string(), item.clone());
}
}

if let Some(shell_command) = &task.shell {
let commands = split_commands(shell_command);
process_commands(
commands,
is_localhost,
session.as_ref(),
true,
task_chdir,
task.register.as_ref(),
&mut register_map,
&vars_map,
)?;
}
if let Some(debug) = &task.debug {
println!("{}", "Debug:".blue());
for (key, msg) in debug.0.iter() {
println!("{}", format!("{}:", key).blue());
let debug_msg = replace_placeholders(msg, &register_map, &local_vars_map);
println!("{}", format!("{}", debug_msg).blue());
}
}

if let Some(shell_command) = &task.shell {
let commands = split_commands(shell_command);
process_commands(
commands,
is_localhost,
session.as_ref(),
true,
task_chdir,
task.register.as_ref(),
&mut register_map,
&local_vars_map,
)?;
}

if let Some(command) = &task.command {
let commands = split_commands(command);
process_commands(
commands,
is_localhost,
session.as_ref(),
false,
task_chdir,
task.register.as_ref(),
&mut register_map,
&vars_map,
)?;
if let Some(command) = &task.command {
let commands = split_commands(command);
process_commands(
commands,
is_localhost,
session.as_ref(),
false,
task_chdir,
task.register.as_ref(),
&mut register_map,
&local_vars_map,
)?;
}
}

println!();
Expand Down
34 changes: 34 additions & 0 deletions test-ymls/loop-item.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
- name: Test
hosts: test
tasks:
- name: loop
command: echo {{ item }}
loop:
- 1
- 2
- 3

- name: loop inner
command: echo {{ item.name }} ({{ item.count }})
loop:
- { name: 'Test', count: 30 }
- { name: 'Test 2', count: 25 }
- { name: 'Test 3', count: 10 }
- { name: 'Test 4', count: 40 }

- name: loop (debug)
debug:
item: "{{ item }}"
loop:
- 1
- 2
- 3

- name: loop inner (debug)
debug:
item: "{{ item.name }} ({{ item.count }})"
loop:
- { name: 'Test', count: 30 }
- { name: 'Test 2', count: 25 }
- { name: 'Test 3', count: 10 }
- { name: 'Test 4', count: 40 }
45 changes: 45 additions & 0 deletions test-ymls/loop-item.yml.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Starting deployment: Test

Executing task: loop
> echo 1
1
> echo 2
2
> echo 3
3

Executing task: loop inner
> echo Test (30)
Test (30)
> echo Test 2 (25)
Test 2 (25)
> echo Test 3 (10)
Test 3 (10)
> echo Test 4 (40)
Test 4 (40)

Executing task: loop (debug)
Debug:
item:
1
Debug:
item:
2
Debug:
item:
3

Executing task: loop inner (debug)
Debug:
item:
Test (30)
Debug:
item:
Test 2 (25)
Debug:
item:
Test 3 (10)
Debug:
item:
Test 4 (40)

6 changes: 6 additions & 0 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,9 @@ fn when_condition() {
setup();
run_tests_for_both_inventories("test-ymls/when-condition.yml", false, "condition=true");
}

#[test]
fn loop_item() {
setup();
run_tests_for_both_inventories("test-ymls/loop-item.yml", false, "");
}

0 comments on commit 58829ff

Please sign in to comment.