简体   繁体   中英

Running bash commands in Ansible task

I am porting a Makefile into a series of Ansible tasks. The make file has these two lines of code which are troubling me when it comes to ansible:

SERVERS := shell mysql "select hostname from servers"
EASY_INSTALL := wget https://bootstrap.pypa.io/ez_setup.py -O - | python

Firstly, what's the appropriate Ansible way to use dynamic vars for the value of SERVERS? The value of servers will depends upon the environment, ie stage, production, etc.

Secondly, I don't want to install the python-setuptools package from aptitude because I have had nothing but problems with it in the past. So how do I run a wget shell command with Ansible?

It looks like you are wanting to download a script and then run that as a command against a number of servers.

The concept of 'hosts' in an Ansible playbook covers your 'servers' concept - you would need to populate an inventory that Ansible can read for the hosts, or even possibly pass in the hosts as a variable when running the playbook (lets call that variable 'servers').

wget and running various commands are all standard Ansible modules (get_url is the generalised name for wget).

So, an example playbook:

- hosts: "{{ servers }}"
  tasks:
    - name: get a file
      get_url:
        url: "https://bootstrap.pypa.io/ez_setup.py"
        dest: "/usr/ez_setup.py"

    - name: run a command
      command: "python /usr/ez_setup.py"

You would then call the above playbook (lets call it shipit.yaml ) like this:

ansible-playbook /usr/shipit.yaml --extra-vars "servers=10.0.0.1"

You could also use the add_hosts module and a custom group name to dynamically read the servers from your database and then populate the custom group, then run your next set of commands against that custom group as the hosts item.

You milage may vary depending on if you need sudo enabled ( sudo: yes ) or not, and there may be a better way of running your python script than using command (command will run every time with no regard to if it has already been run - not idempotent in other words).

Hopefully the above gives you a good starting point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM