Say I have a very simple program magic_command.c that I want compiled to magic_command and installed in /usr/local/bin.
One method I can think of for doing this is roughly the following with Ansible:
Create a temporary directory on the remote host
Copy the magic_command.c and a Makefile to the temporary directory
Run make all to create magic_command
Run make install, a target that copies magic_command to /usr/local/bin
Is there a simpler or more concise way of doing this with Ansible?
I ended up using a method very similar to that described in my original question:
- name: Create temporary directory for compilation
command: mktemp -d /tmp/magic_command.XXXXXXXXX
register: magic_command_temp_dir
- name: Copy source and makefile
copy: src={{ item }} dest={{ magic_command_temp_dir.stdout }}
with_items:
- magic_command.c
- Makefile
- name: Compile executable
shell: make chdir={{ magic_command_temp_dir.stdout }}
- name: Install executable
copy: remote_src=True src={{ magic_command_temp_dir.stdout }}/magic_command dest=/usr/local/bin/magic_command mode=0755 owner=root group=root
- name: Remove temporary directory
file: name={{ magic_command_temp_dir.stdout }} state=absent
Related
I am trying to take a .zip file and copy it to a directory and extract the .zip file afterwards on a Windows target using an Ansible playbook. I was wondering if there's a module or task that can be written to do this nice and clean.
The .zip file itself is stored on another server so the playbook task would be pulling from this location to get the file to copy to the target.
I use these two modules to accomplish that task. win_copy: could be used if the file is on your ansible control host.
- name: download zlib source
win_get_url:
url: "{{ zlib_url }}"
dest: "{{ boost_workdir }}"
register: download_zlib
until: download_zlib is succeeded
retries: 10
delay: 2
- name: unzip zlib source
win_unzip:
src: "{{ download_zlib.dest }}"
dest: "{{ boost_prefix }}"
delete_archive: true
creates: "{{ boost_prefix }}/zlib-{{ zlib_version }}/contrib"
copy: false
I have the following statement, that I would like to translate into Ansible:
$ curl -L https://github.com/drone/drone-cli/releases/latest/download/drone_linux_amd64.tar.gz | tar zx
$ sudo install -t /usr/local/bin drone
How to build an Ansible task for it?
I've tried:
- name: Extract droner exec runner into /usr/local/bin
unarchive:
src: drone_linux_amd64.tar.gz
dest: /usr/local/bin/
mode: "0664"
But it does not work as expected.
install command manages the permissions of the file, making sure the execute permissions are added to the file during installation.
unarchive just extracts the contents of the archive file to the destination. Set the desired mode to the task, in this case to make it executable add the +x permission.
- name: Extract droner exec runner into /usr/local/bin
unarchive:
src: drone_linux_amd64.tar.gz
dest: /usr/local/bin/
mode: "a+x"
a+x, giving execute permission for all expecting the drone command will be used across users.
- hosts: localhost
become: yes
gather_facts: no
tasks:
- name: Get the file from github
get_url:
url: "https://github.com/drone/drone-cli/releases/latest/download/drone_linux_amd64.tar.gz"
dest: /tmp/drone_linux_amd64.tar.gz
- name: Unarchive the file
unarchive:
src: /tmp/drone_linux_amd64.tar.gz
dest: /tmp
mode: 0664
- name: Install
shell:
cmd: install -t /usr/local/bin drone
chdir: /tmp
Task 1: Fetching the file form the github with get_url. you can also you shell module. get_url module
Task 2: Unarchive the file using unarchive module. unarchive module
Task 3: Install with shell command shell module
I have unarchive it in the tmp folder. and then installed it.
I have an ansible playbook which will copy a file into a location on a remote server. It works fine. In this case, the file is an rpm. This is the way it works:
---
- hosts: my_host
tasks:
- name: mkdir /tmp/RPMS
file: path=/tmp/RPMS state=directory
- name: copy RPMs to /tmp/RPMS
copy:
src: "{{ item }}"
dest: /tmp/RPMS
mode: 0755
with_items:
[any_rpm-x86_64.rpm]
register: rpms_copied
Now, with the file successfully on the remote server, I need to start some new logic that will install the rpm that sits in /tmp/RPMS. I have run many different versions of the below (So this code is added onto the above block):
- name: install rpm from file
yum:
name: /tmp/RPMS/any_rpm-x86_64.rpm
state: present
become: true
I don't know if the formatting is incorrect, or if this is not the way. Can anyone advise as to how I can get the rpm in the directory /tmp/RPMS installed using a new few lines in the existing playbook?
Thanks.
I did not find this anywhere else, and it genuinely took me all of my working day to get to this point. For anyone else struggling:
- name: Install my package from a file on server
shell: rpm -ivh /tmp/RPMS/*.rpm
async: 1800
poll: 0
become_method: sudo
become: yes
become_user: root
I have to make delete some directory and create them on local before copy to the remote. Is there anyway to delete and create locally?
Currently I'm using 'command'
command: rm -r directory
But warning shows as
Consider using file module with state=absent rather than running rm
Is there any options we can use for local folder changes?
You can use diffrent delegation methods or use the local_action:
- local_action: file path=directory state=absent
If you're running this in a playbook, you can use a section of the playbook that uses a local connection to make changes on the command machine, then copies files to the remote:
---
- hosts: 127.0.0.1
connection: local
tasks:
- name: Delete local directory
file: path=/directory state=absent
- hosts: myhosts
tasks:
copy: src=/directory dest=/foo/directory
Update:
Current Ansible (2.10) does not like - local_action: , instead use delegate_to:
- name: Remove directory 'dir1'
file:
path: "path/to/dir1"
state: absent
delegate_to: localhost
my requirement is moving files to remote host using ansible playbook.
my ansible script
---
- hosts: webservers
remote_user: root
tasks:
- copy: src=/home/bu/Bilal/site dest=/tmp owner=root group=root mode=777
when run playbook it has moved the file to remote.
when I have ran playbook again it will overwrite the whole folder again. I am looking, what are the files I have modified that files only get overwrite because my folder size is too large its taking so much time even single file change.
Take a look at the Synchronize module:
Uses rsync to make synchronizing file paths in your playbooks quick and easy.
Example:
- name: Sync files
synchronize:
src: "{{ conf.dev_path }}/"
dest: "{{ conf.host_path }}"
delete: yes
rsync_opts:
- "--exclude=.*"