2

I have an Ansible playbook that should execute a script. This Script should add lines of code to a file. Note that the playbook is bigger then this one role though all other steps of the ansible-playbook work. The script also works when executed directly on the puppet server. I suspect that it is a permissions issue, though i am not sure how to solve the problem.

Note: The ansible playbook is getting executed as ansible user. The script and the file with the added lines are owned by usr ansible and grp ansible. Rights are: 0755. The file to be changed is also at the same location as the script.

script name:

createlines.sh 

file with the added lines:

testfile.py 

hosts:

puppet 

ansible command:

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

playbook:

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

roles/run-script:

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

Error message(is extremly big and constantly repeats itself):

Start of error message:

 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",........ 

End of error message:

........"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": []} 

Does someone see what could be wrong?


Edit: 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)" 
1
  • please show extracts of .sh and .py file to help understand Commented Dec 10, 2018 at 15:03

2 Answers 2

3

Please make sure that you are using the full path to the testfile.py script in your shell script. When Ansible runs shell commands, it does this from a different location, e.g. the /tmp directory. Using the full, absolute path to your Python script will ensure that it can be found in this case.

You can run Ansible with the -vvv parameter to see the location of the script when it gets executed by Ansible.

Sign up to request clarification or add additional context in comments.

Comments

1

the createlines.sh seems to be present on targeted host but not the py file.

you should ensure that the py file is either copied and present on the target host.

4 Comments

its there /var/opt/alu/deploy/hieradata/testfile.py. I have it right on my screen. The file is 100% there
Can you try to use the full path of the testfile.py file in your script? Just to make sure that it can be found...
@nwinkler the script works and does everything as it should. It adds the lines correctly and also finds the file. It only gets into trouble when i play the ansible-playbook.The script is also in the same dir as the testfile.py
Ansible sometime runs scripts from a different directory - you should see that if you run Ansible with the -vvv flag. Trying the absolute path was not meant as a permanent fix, just for verifying whether it can find the script. I'd give it a try...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.