Skip to content

Commit

Permalink
substitute vars in chdir
Browse files Browse the repository at this point in the history
  • Loading branch information
flawiddsouza committed Nov 13, 2024
1 parent a16c74c commit 79c36d1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,18 @@ fn process_tasks(

println!("{}", format!("Executing task: {}", task.name).cyan());

let task_chdir = task.chdir.as_deref().or(dep_chdir); // Use task-level chdir if present, otherwise use top-level chdir

if let Some(vars) = &task.vars {
for (key, value) in vars {
let evaluated_value = utils::replace_placeholders_vars(&value, vars_map);
vars_map.insert(key.clone(), evaluated_value);
}
}

// Use task-level chdir if present, otherwise use top-level chdir
let task_chdir = task.chdir.as_deref().or(dep_chdir).map(|s| {
utils::replace_placeholders(s, vars_map)
});

// Debug print to verify vars_map
// println!("Vars map: {:?}", vars_map);

Expand All @@ -82,7 +85,7 @@ fn process_tasks(
is_localhost,
session,
true,
task_chdir,
task_chdir.as_deref(),
task.register.as_ref(),
vars_map,
)?;
Expand All @@ -95,7 +98,7 @@ fn process_tasks(
is_localhost,
session,
false,
task_chdir,
task_chdir.as_deref(),
task.register.as_ref(),
vars_map,
)?;
Expand All @@ -113,7 +116,7 @@ fn process_tasks(
&included_tasks,
is_localhost,
session,
task_chdir,
task_chdir.as_deref(),
vars_map,
deploy_file_dir,
)?;
Expand Down
14 changes: 14 additions & 0 deletions test-ymls/use-vars-in-chdir.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
- name: Test
hosts: test
vars:
global_working_directory: tests/mocks/ls-dir
chdir: "{{ global_working_directory }}"
tasks:
- name: Global working directory
shell: cat file1.txt

- name: Task-level working directory
vars:
task_working_directory: tests/mocks/ls-dir2
chdir: "{{ task_working_directory }}"
shell: cat file1.txt
10 changes: 10 additions & 0 deletions test-ymls/use-vars-in-chdir.yml.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Starting deployment: Test

Executing task: Global working directory
> cat file1.txt
File 1 of ls-dir

Executing task: Task-level working directory
> cat file1.txt
File 1 of ls-dir2

6 changes: 6 additions & 0 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,9 @@ fn run_level_vars() {
setup();
run_tests_for_both_inventories("test-ymls/run-level-vars.yml", false, "");
}

#[test]
fn use_vars_in_chdir() {
setup();
run_tests_for_both_inventories("test-ymls/use-vars-in-chdir.yml", false, "");
}

0 comments on commit 79c36d1

Please sign in to comment.