Question:
I am trying to wget a file from a web server from within an Ansible playbook.
Here is the Ansible snippet:
1 2 3 4 5 6 7 8 9 10 11 |
--- - hosts: all sudo: true tasks: - name: Prepare Install folder sudo: true action: shell sudo mkdir -p /tmp/my_install/mysql/ && cd /tmp/my_install/mysql/ - name: Download MySql sudo: true action: shell sudo wget http://{{ repo_host }}/MySQL-5.6.15-1.el6.x86_64.rpm-bundle.tar |
Invoking it via:
1 2 |
ansible-playbook my_3rparties.yml -l vsrv644 --extra-vars "repo_host=vsrv656" -K -f 10 |
It fails with the following:
1 2 3 4 5 6 7 8 |
Cannot write to `MySQL-5.6.15-1.el6.x86_64.rpm-bundle.tar' (Permission denied). FATAL: all hosts have already failed -- aborting PLAY RECAP ******************************************************************** to retry, use: --limit @/usr2/ihazan/vufroria_3rparties.retry vsrv644 : ok=2 changed=1 unreachable=0 failed=1 |
When trying to do the command that fail via regular remote ssh to mimic what ansible would do, it doesn’t work as follows:
1 2 3 4 |
-bash-4.1$ ssh ihazan@vsrv644 'cd /tmp/my_install/mysql && sudo wget http://vsrv656/MySQL-5.6.15-1.el6.x86_64.rpm-bundle.tar' Enter passphrase for key '/usr2/ihazan/.ssh/id_rsa': sudo: sorry, you must have a tty to run sudo |
But I can solve it using -t as follows:
1 2 |
-bash-4.1$ ssh -t ihazan@vsrv644 'cd /tmp/my_install/mysql && sudo wget http://vsrv656/MySQL-5.6.15-1.el6.x86_64.rpm-bundle.tar' |
Then it works.
Is there a way to set the -t (pseudo tty option) on ansible?
P.S: I could solve it by editing the sudoers file as others propose but that is a manual step I am trying to avoid.
Answer:
Don’t use shell-module when there is specialized modules available. In your case:
Create directories with file-module:
1 2 3 |
- name: create project directory {{ common.project_dir }} file: state=directory path={{ common.project_dir }} |
Download files with get_url-module:
1 2 3 |
- name: download sources get_url: url={{ opencv.url }} dest={{ common.project_dir }}/{{ opencv.file }} |
Note the new module call syntax in the examples above.
If you have to use sudo
with password remember to give --ask-sudo-pass
when needed (see e.g. Remote Connection Information).