简体   繁体   English

Ansible:命令模块未插入shell变量

[英]Ansible: Command module not interpolating shell variable

The following command fails most likely because it fails to interpolate the shell variable packdir 以下命令很可能失败,因为它无法插补shell变量packdir

  - name: archive_artifacts.yml --> Clear git history from packs directories
    command: 'for packdir in {{ packs_dir }}/*; do rm -rf {{ packs_dir }}/"${packdir}"/.git; done'
    args:
      chdir: "{{ temp_build_directory }}"

packs_dir is a variable in the defaults/main.yml of the specific role: packs_dir是特定角色的defaults/main.yml中的变量:

packs_dir: "packs"

Is there a way of having the command module substituting both ansible and shell variables? 有没有一种方法可以让命令模块替换ansible shell变量?

Is there a way of having the command module substituting both ansible and shell variables? 有没有一种方法可以让命令模块替换ansible和shell变量?

Yes. 是。 Make sure the shell variable is present in the environment of the host, user and shell you run the command in. 确保在运行命令的主机,用户和外壳程序的环境中存在shell变量。

- hosts: localhost
  vars:
    env_variable: SHELL
  tasks:
    - command: "echo ${{ env_variable }}"
      register: result
    - debug: msg="{{ result.stdout }}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM