Ansible Tutorial
Lets see ansible playbook to run shell scripts and display output file content which is generated via shell scripts.
To invoke shell scripts file, lets try with command ansible module like below,
--- - name: Run shell scripts on local machine hosts: localhost tasks: - name: run get_host_name.sh command: ". /home/user2/ansible/test_shell/get_host_name.sh" - name: read file content command: "cat /home/user2/ansible/shell_file" register: out - name: Print the msg debug: msg: "{{ out.stdout }}"
Shell scripts file content '/home/user2/ansible/test_shell/get_host_name.sh',
#!/bin/bash echo $(hostname -f) > shell_file
$ ansible-playbook run-shell-scripts.yml PLAY [Run shell scripts on local machine] *********************************************************************************************************************************** TASK [Gathering Facts] ****************************************************************************************************************************************************** ok: [localhost] TASK [run get_host_name.sh] ************************************************************************************************************************************************* fatal: [localhost]: FAILED! => {"changed": false, "cmd": ". /home/user2/ansible/test_shell/get_host_name.sh", "msg": "[Errno 13] Permission denied: b'.'", "rc": 13} PLAY RECAP ****************************************************************************************************************************************************************** localhost : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
Raised permission denied error when using command ansible module to run shell scripts as mentioned in above ansible playbook.
Lets see detailed error using -vvv option when running ansible playbook,
TASK [run get_host_name.sh] ************************************************************************************************************************************************* task path: /home/user2/ansible/run-shell-scripts.yml:5 The full traceback is: File "/tmp/ansible_command_payload_1_vbk9ne/ansible_command_payload.zip/ansible/module_utils/basic.py", line 2687, in run_command cmd = subprocess.Popen(args, **kwargs) File "/usr/lib64/python3.6/subprocess.py", line 729, in __init__ restore_signals, start_new_session) File "/usr/lib64/python3.6/subprocess.py", line 1364, in _execute_child raise child_exception_type(errno_num, err_msg, err_filename) fatal: [localhost]: FAILED! => { "changed": false, "cmd": ". /home/user2/ansible/test_shell/get_host_name.sh", "invocation": { "module_args": { "_raw_params": ". /home/user2/ansible/test_shell/get_host_name.sh", "_uses_shell": false, "argv": null, "chdir": null, "creates": null, "executable": null, "removes": null, "stdin": null, "stdin_add_newline": true, "strip_empty_ends": true, "warn": true } }, "msg": "[Errno 13] Permission denied: b'.'", "rc": 13 }
We have other ansible module 'shell', lets try running shell scripts using shell ansible module instead of command module,
--- - name: Run shell scripts on local machine hosts: localhost tasks: - name: run get_host_name.sh shell: ". /home/user2/ansible/test_shell/get_host_name.sh" - name: read file content command: "cat /home/user2/ansible/shell_file" register: out - name: Print the msg debug: msg: "{{ out.stdout }}"
Lets run the ansible playbook,
Output:
$ ansible-playbook run-shell-scripts.yml PLAY [Run shell scripts on local machine] *********************************************************************************************************************************** TASK [Gathering Facts] ****************************************************************************************************************************************************** ok: [localhost] TASK [run get_host_name.sh] ************************************************************************************************************************************************* changed: [localhost] TASK [read file content] **************************************************************************************************************************************************** changed: [localhost] TASK [Print the msg] ******************************************************************************************************************************************************** ok: [localhost] => { "msg": "mymachine-1.local" } PLAY RECAP ****************************************************************************************************************************************************************** localhost : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Now permission denied error is resolved and shell scripts executed as expected and hostname is displayed in ansible playbook tasks.
Ansible Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page