Create a sym link in ansible - ansible

I'm writing a playbook and i want to create a symlink.
While installing citrix on the linux system i need to create a sym link using this command:
ln -s /etc/ssl/serts cacerts
now in the playbook i use it as a :
- name: Link
command: ln -s /etc/ssl/serts cacerts
The thing is when I use the format above it works fine. But if I want to check if the file exists and if not creating and if yes then skip to the next task.
I could use ignore_errors: yes but I think there is a better way of doing it.
Thank you very much in advance.

You can use the "file" module:
- name: Link
file:
src: cacerts
dest: /etc/ssl/serts
state: link
It is generally better to use a proper module which will deal with failure conditions and check mode. In this case, it will not fail if the link already exists and it is correct.
You may want to give an absolute src depending on your application.
For more information: https://docs.ansible.com/ansible/latest/modules/file_module.html

Related

Ansible - win_file module - force deletion if file in use

We are using ansible win_file module to delete a particular folder in a Windows Server machine with the following code:
- name: Delete <folderName> directory
win_file: path=C:\<pathToFolder>\{{target_environment}}\<folderName> state=absent
tags: <folderName>
The problem: When a file from that directory is open in another program at the same time the ansible role runs, it fails saying:
"The process cannot access the file because it is being used by another process"
Now, i understand the error, but i am looking for suggestions to force this deletion, even if the file is in use, or if there is other module that i don't know that can't resolve this problem.
(currently using ansible 2.4.6)
So, after some searching and digging, i came out with a solution, i found a similar ansible module that can do the job, win_shell.
I resolved the problem with the following code:
name: Delete <folderName> directory
win_shell: Remove-Item –path <folderName> –recurse -force
args:
chdir: C:\<pathToFolder>\{{target_environment}}
removes: C:\<pathToFolder>\{{target_environment}}\<folderName>
tags: <folderName>
removes: checks if the folder exists else skips the task
force: does the trick of what i want, delete the folder and all his files even if some of the files are in use or open in any program.

Ansible idempotency issue with unarchive and then modify extracted file

In one of the ansible roles we extract some tar.gz file and then we replace one of the extracted files with another one to fix some issue.
The problem is when we run ansible again, ansible is extracting the archive back again since the directory content is changed and naturally marking the task changed and also replaces the file again as expected.
So we have two "changes" now everytime we run the playbook...
How should I handle this issue to keep the operation idempotent?
Use exclude option to ignore certain paths, see documentation.
i.e.
- unarchive:
src: https://example.com/example.zip
dest: /usr/local/bin
remote_src: True
exclude: bad.config
creates might also suit you, unarchive step will not be run if specified path already exists on remote machine

Is it possible to copy a local file to a remove server without using the Copy module?

I'm working on a playbook to upload a configuration file to remote servers, but the remote servers do not have python installed (which is a requirement for using modules). I have successfully written other playbooks using the raw feature to avoid having to install python on the servers, but I can't find any examples in the Ansible documentation to perform a file upload using bare-bones ssh. Is a non-module based upload possible?
No sure why do you use Ansible this way, but you can make a local task with scp:
- name: remote task
raw: echo remote
- name: local scp
local_action: command scp /path/to/localfile {{ inventory_hostname }}:/path/to/remotefile
- name: remote task
raw: cat /path/to/remotefile
I usually check and install python with raw module and continue with Ansible core modules.
This answer may not always be applicable, but as long as you are allowed to put the files on some kind of Web or so server, and as long as curl or wget or similar are installed on the remote system, you can use those tools to download your files within the raw block.

Ansible synchronize mode permissions

I'm using an Ansible playbook to copy files between my host and a server. The thing is, I have to run the script repeatedly in order to upload some updates. At the beginning I was using the "copy" module of Ansible, but to improve performance of the synchronizing of files and directories, I've now switched to use the "synchronize" module. That way I can ensure Ansible uses rsync instead of sftp or scp.
With the "copy" module, I was able to specify the file's mode in the destination host by adding the mode option (e.g. mode=644). I want to do that using synchronize, but it only has the perms option that accepts yes or no as values.
Is there a way to specify the file's mode using "synchronize", without having to inherit it?
Thx!
Finally I solved it using rsync_opts
- name: sync file
synchronize:
src: file.py
dest: /home/myuser/file.py
rsync_opts:
- "--chmod=F644"

(no such process) while deploying app via Ansible

I am trying to follow the Ansible Tutorial by Matt Wright. I have forked it and updated with latest Ansible modules here.
But I'm getting
msg: hello_flask: ERROR (no such process)
while running deploy.yml at -name: start app. I have a open issue here on github.
Why I am getting this error?
So you are seeing the error because supervisor is not finding the hello_flask application.
This is probably because you have a newer configuration for supervisor that doesn't include ini files.
If you look at one of the latest /etc/supervisor/supervisor.conf it actually includes *.conf files not *.ini files.
[include]
files = /etc/supervisor/conf.d/*.conf
Also, if you look at this Ansible task:
- name: create supervisor program config
action: template src=templates/supervisor.ini dest=/etc/supervisor/${app_name}.ini
notify:
- restart app
You can see that the configuration for hello_flash is being put under /etc/supervisor/hello_flash.ini
So make sure either that your supervisor.conf includes *.ini files. Or simply change this step to this:
- name: create supervisor program config
action: template src=templates/supervisor.ini dest=/etc/supervisor/conf.d/${app_name}.conf
notify:
- restart app
Hope it helps.

Resources