Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible and Wget

Tags:

ansible

I am trying to wget a file from a web server from within an Ansible playbook.

Here is the Ansible snippet:

--- - 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:

ansible-playbook my_3rparties.yml -l vsrv644 --extra-vars "repo_host=vsrv656" -K -f 10  

It fails with the following:

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:

-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:

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

like image 507
isaac.hazan Avatar asked Apr 08 '14 14:04

isaac.hazan


People also ask

How do I untar in Ansible?

Ansible unarchive module is used to unpack or uncompressed the files from an archive file such as zip, tar, tar. gz. It can optionally copy the files to the remote server before uncompressing them. The unarchive module uses the basic unzip and tar -xvf command-line tools to operate.

How do I run an RPM in Ansible?

The path to the local rpm file on the server can be passed to the name parameter. From the Ansible yum module documentation: You can also pass a url or a local path to a rpm file. To operate on several packages this can accept a comma separated list of packages or (as of 2.0) a list of packages.

Does Ansible work over SSH?

By default, Ansible assumes you are using SSH keys to connect to remote machines. SSH keys are encouraged, but you can use password authentication if needed with the --ask-pass option.


1 Answers

Don't use shell-module when there is specialized modules available. In your case:

Create directories with file-module:

- name: create project directory {{ common.project_dir }}   file: state=directory path={{ common.project_dir }} 

Download files with get_url-module:

- 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).

like image 136
user272735 Avatar answered Sep 27 '22 18:09

user272735