Unable to execute Python script using Ansible play

2019-03-03 15:26发布

问题:

I have a python script that runs "vgs" command to check the free space in the volume groups available and then creates lvm , using lvcreate command.

When I run this python script locally, I can run it without issues and the lvm gets created as well. But if I run it using Ansible playbook, I've tried with both 'command' and 'shell' modules, it is unable to locate the path for vgs command and fails. It seems it is only looking for /usr/bin:/bin paths. Please help.

which: no vgs in (/usr/bin:/bin)", "No Volume Groups Found", "Space Found in Volume Group:"], "warnings": []}

"stderr": "sh: -c: line 0: syntax error near unexpected token `('\nsh: -c: line 0: `{ which: no vgs in (/usr/bin:/bin) -o VG_NAME --noheadings --units m ; } 2>&1'

回答1:

It seems it is only looking for '/usr/bin:/bin' paths.

Because interactive and non-interactive shell sessions call different set of rc-files. If you set PATH in rc-files that are sourced only by interactive shell, those settings won't be reflected in non-interactive shells.

Add PATH as an environment variable to the task:

- command: <script_on_target_node>
  environment:
    PATH: <path_string>


标签: linux ansible