ansible“ sed:无法读取文件:没有这样的文件或目录”

时间:2018-12-10 14:45:15

标签: ansible file-permissions

我有一本应执行脚本的Ansible剧本。该脚本应将代码行添加到文件中。请注意,尽管ansible-playbook的所有其他步骤都起作用,但剧本比该角色更大。直接在人偶服务器上执行时,该脚本也可以工作。我不确定这是一个权限问题,尽管我不确定如何解决该问题。

注意:ansible剧本正以ansible用户的身份执行。脚本和带有添加的行的文件由usr ansible和grp ansible拥有。权利是:0755。 要更改的文件也与脚本位于同一位置。

脚本名称:

createlines.sh

添加了几行的文件:

testfile.py

主机:

puppet

可用命令:

ansible-playbook deployment-test.yml --ask-become-pass

剧本:

---
- name: add lines in file
  hosts: Puppet
  become: yes
  become_method: sudo
  become_user: root
  vars_files:
    - vars/gpv-test.yml
  roles:
    - run-script

角色/运行脚本:

---
- name: Execute the createlines script
  command: sh /var/opt/alu/deploy/hieradata/createlines.sh

错误消息(非常大,并不断重复出现):

错误消息开始:

     fatal: [puppet]: FAILED! => {"changed": true, "cmd": ["sh", "/var/opt/alu/deploy/hieradata/createlines.sh"], "delta": "0:00:00.467213", "end": "2018-12-10 15:33:25.429726", "msg": "non-zero return code", "rc": 2, "start": "2018-12-10 15:33:24.962513", "stderr": "sed: can't read testfile.py: No such file or directory\nsed: can't read testfile.py: No such file or directory\nsed: can't read testfile.py: No such file or directory\nsed: can't read testfile.py........
.....can't read gen_prod_trm_test_yaml.py: No such file or directory", "stderr_lines": ["sed: can't read testfile.py: No such file or directory", "sed: can't read testfile.py: No such file or directory", "sed: can't read testfile.py: No such file or directory", "sed: can't read testfile.py: No such file or directory",........

错误信息结尾:

........"sed: can't read testfile.py: No such file or directory", "sed: can't read testfile.py: No such file or directory"], "stdout": "", "stdout_lines": []}

有人看到有什么问题吗?


编辑: createlines.sh:

#! /bin/bash
today=`date +%Y-%m-%d`
group_2=(/tmp/${today}/group-2/*)
group_3=(/tmp/${today}/group-3/*)
group_4=(/tmp/${today}/group-4/*)
group_5=(/tmp/${today}/group-5/*)
file="testfile.py"
#filessha1="../../../testfolder/testfolder/group-2/"
#sha1keyy=`sha1sum "${filesha1}"`
#echo "${sha1key}"
#echo "$today"
######GROUP-2
for i in ${group_2[@]}; do
   #echo $i
   #echo ${i##*/}
   sed -i "/trm_data_file/a  \   ensure: present" "$file"
   sed -i "/trm_data_file/a  \   dest_folder: group-2" "$file"
   sed -i "/trm_data_file/a  \     sha1: `(cd /tmp/$today/group-2/; sha1sum ${i##*/} | awk '{ print $1 }')`" "$file"
   sed -i "/trm_data_file/a  \     url: filestore://testfolder/testfolder/${today}/group-2/${i##*/}" "$file"
   sed -i "/trm_data_file/a -  file:" "$file"
done

######GROUP-3
for i in ${group_3[@]}; do
   #echo $i
   #echo ${i##*/}
   sed -i "/trm_data_file/a  \   ensure: present" "$file"
   sed -i "/trm_data_file/a  \   dest_folder: group-3" "$file"
   sed -i "/trm_data_file/a  \     sha1: `(cd /tmp/$today/group-3/; sha1sum ${i##*/} | awk '{ print $1 }')`" "$file"
   sed -i "/trm_data_file/a  \     url: filestore://testfolder/testfoldedf/${today}/group-3/${i##*/}" "$file"
   sed -i "/trm_data_file/a -  file:" "$file"
done

######GROUP-4
for i in ${group_4[@]}; do
   #echo $i
   #echo ${i##*/}
   sed -i "/trm_data_file/a  \   ensure: present" "$file"
   sed -i "/trm_data_file/a  \   dest_folder: group-4" "$file"
   sed -i "/trm_data_file/a  \     sha1: `(cd /tmp/$today/group-4/; sha1sum ${i##*/} | awk '{ print $1 }')`" "$file"
   sed -i "/trm_data_file/a  \     url: filestore://testfolder/testfolder/${today}/group-4/${i##*/}" "$file"
   sed -i "/trm_data_file/a -  file:" "$file"
done

######GROUP-5
for i in ${group_5[@]}; do
   #echo $i
   #echo ${i##*/}
   sed -i "/trm_data_file/a  \   ensure: present" "$file"
   sed -i "/trm_data_file/a  \   dest_folder: group-5" "$file"
   sed -i "/trm_data_file/a  \     sha1: `(cd /tmp/$today/group-5/; sha1sum ${i##*/} | awk '{ print $1 }')`" "$file"
   sed -i "/trm_data_file/a  \     url: filestore://testfolder/testfolder/${today}/group-5/${i##*/}" "$file"
   sed -i "/trm_data_file/a -  file:" "$file"
done

testfile.py:

"some Python code(not relevant)"
trm_data_files:
"Place where lines get inserted"
"some Python code(not relevant)"

2 个答案:

答案 0 :(得分:2)

请确保您在外壳程序脚本中使用testfile.py脚本的完整路径。当Ansible运行shell命令时,它将在其他位置执行此操作,例如/tmp目录。使用Python脚本的完整绝对路径将确保在这种情况下可以找到它。

您可以使用-vvv参数运行Ansible,以在Ansible执行脚本时查看脚本的位置。

答案 1 :(得分:0)

createlines.sh似乎存在于目标主机上,但没有py文件。

您应确保py文件已复制并存在于目标主机上。