Lambo-Fan
Lambo-Fan

Reputation: 33

ansible - tranferring files and changing ownership

I'm new to Ansible. The following is my requirement,

  1. Transfer files(.tar.gz) from one host to many machines (38+ Nodes) under /tmp as user1
  2. Log in to each machines as user2 and switch to root user using sudo su - (With Password)
  3. extract it to another directory (/opt/monitor)
  4. Change a configuration in the file (/opt/monitor/etc/config -> host= )
  5. Start the process under /opt/monitor/init.d

For this, should I use playbooks or ad-hoc commands ? I'll happy to use ad-hoc mode in ansible as I'm afraid of Playbooks.

Thanks in advance

Upvotes: 0

Views: 2584

Answers (2)

Bruce P
Bruce P

Reputation: 20759

You'll want a playbook to do this. At the simplest level, since you mention unpacking, it might look something like this:

- name: copy & unpack the file
  unarchive: src=/path/to/file/on/local/host
             dest=/path/to/target
             copy=yes

- name: copy custom config
  copy: src=/path/to/src/file
        dest=/path/to/target

- name: Enable service
  service: name=foo enabled=yes state=started

Upvotes: 1

Micah Elliott
Micah Elliott

Reputation: 10274

You’d have to write several ad hoc commands to accomplish this. I don’t see any good reason to not use a playbook here. You will want to learn about playbooks, but it’s not much more to learn than the ad hoc commands. The sudo parts are taken care of for you by using the -b option to “become” the using sudo. Ansible takes care of the logging in for you via ssh.

The actions you’ll want to make use of are common for this type of setup where you’re installing something from source, commands like yum, get_url, unarchive, service. As an example, here’s a pretty similar process to what you need, demonstrating installing redis from source on a RedHat-family system:

- name: install yum dependencies for redis
  yum: name=jemalloc-devel ... state=present

- name: get redis from file server
  get_url: url={{s3uri}}/common/{{redis}}.tar.gz dest={{tmp}}

- name: extract redis
  unarchive: copy=no src={{tmp}}/{{redis}}.tar.gz dest={{tmp}} creates={{tmp}}/{{redis}}

- name: build redis
  command: chdir={{tmp}}/{{redis}} creates=/usr/local/bin/redis-server make install

- name: copy custom systemd redis.service
  copy: src=myredis.service dest=/usr/lib/systemd/system/
  # and logrotate, redis.conf, etc

- name: enable myredis service
  service: name=myredis state=started enabled=yes

You could define custom variables like tmp and redis in a global_vars/all.yaml file. You’ll also want a site.yaml file to define your hosts and a role(s).

You’d invoke the playbook with something like:

ansible-playbook site.yaml -b --ask-become-pass -v

This can operate on your 38+ nodes as easily as on one.

Upvotes: 1

Related Questions