ansible link from directory to directory - ansible

I have a playbook that creates a directory, creates content on index.html, and a link from /web_hosting to /var/www/html.
The directory is called /web_hosting
the content is /web_hosting/index.html
I do not want to change the httpd.conf default web directory to /web_hosting I just want to use a link.
After running the play when I curl the server I'm not seeing the content from the index.html file.
Can someone help me with my play?
name: setup webserver and link to folder
hosts: prod
tasks:
name: create dir
file:
path: /web_hosting
state: directory
setype: httpd_sys_content_t
mode: 0775
name: install
yum:
name: httpd
state: present
name: configure service
service:
name: httpd
state: started
enabled: true
name: create content on index.html
copy:
dest: /web_hosting/index.html
content: "hello from {{ansible_hostname}}"
name: create link
file:
src: /web_hosting
dest: /var/www/html
state: link

This doesn't sound like an Ansible problem if it is creating the files and not erroring out.
If you manually create a file in /var/www/html/ called "index2.html", can you use curl to see it? If not, then it's definitely NOT an Ansible problem.
If that test works, then look for differences in ownership, SELinux permissions, etc. Then use Ansible to set those properly on your "index.html".
I suspect you might need to enable a "follow links" setting in your webserver configuration. But again, that's not an Ansible issue either - though Ansible could update the configuration file once you figure out what setting(s) to apply.

Related

How do I configure remote host journal.conf file with ansible?

I am trying to configure raspberry pi journalctl using ansible.
I tried using some ansible-galaxy roles which seem too complicated and did not deliver in the end.
I am just trying to configure the /etc/systemd/journald.conf file.
Can I do it with ansible.builtin.systemd or any other suggestions?
You only need a playbook and a template file.
myproject/changejournald.yml # your playbook
myproject/journald.conf.j2 # a jinja2 template, the 'journald.conf as you want it'
in changejournald.yml
---
- name: upload new template
template:
src: 'journald.conf.j2'
dest: '/etc/systemd/journald.conf'
become: true #<-- unless you are connecting as root
- name: reload systemd-journald
systemd:
name: systemd-journald
state: restart
become: true
Something like that should work?
There are also other modules like lineinfile or blockinfile that might be more useful depending on how you intend to configure it.
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/template_module.html
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html
https://unix.stackexchange.com/questions/253203/how-to-tell-journald-to-re-read-its-configuration

Ansible - Edit a systemd service file

The systemd module: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/systemd_module.html
I'm looking for a way to add a Condition to the service file.
For instance:
ConditionPathIsMountPoint=/mnt/myreplication/path/
This would be useful for docker installations, ensuring docker doesn't start containers before a mount they need is actually available.
Sadly, it looks like Ansible doesn't support adding this right now. Am I correct there? Will I need to manually add it, or with lineinfile? Or is there an other way?
EDIT: This question appears to be getting views, so I'll add this:
https://askubuntu.com/questions/659267/how-do-i-override-or-configure-systemd-services
And this answer to another question of mine: https://askubuntu.com/a/1348117/1612
To quote it:
Don't edit files in /lib/systemd/ or /usr/share/systemd as they will get overwritten on updates.
Let me post a solution using ini_file that worked for me:
- name: Create a foo.service override directory
file:
owner: root
group: root
mode: 0755
path: /etc/systemd/system/foo.service.d
state: directory
- name: Set up foo.service override
ini_file:
dest: /etc/systemd/system/foo.service.d/bar_override.conf
owner: root
group: root
mode: 0644
section: Unit
option: ConditionPathIsMountPoint
value: /mnt/myreplication/path/
This avoids rewriting the original service file, but rather adds a dedicated override into a .d subdirectory.
Note that ini_file adds whitespace around = as in
[Unit]
ConditionPathIsMountPoint = /mnt/myreplication/path/
but this is fine, see systemd.syntax(7):
Each file is a plain text file divided into sections, with configuration entries in the style key=value. Whitespace immediately before or after the "=" is ignored.
Am I correct there?
Right, the systemd_module is not for manipulating service files.
Since I've had some similar questions in the past I like to share my approach.
You could either maintain your own service file template and deploy it
- name: "Make sure the systemd service file is correct"
template:
src: "{{ MYSERVICE }}.service.j2"
dest: "/etc/systemd/system/{{ MYSERVICE }}.service"
mode: 0755
tags: install,systemd
or add the necessary line via lineinfile_module
- name: "Make sure the entry in '{{ MYSERVICE }}.service' exists"
lineinfile:
path: "/etc/systemd/system/{{ MYSERVICE }}.service"
line: "ConditionPathIsMountPoint=/mnt/myreplication/path/"
state: present
tags: install,systemd
and reload and restart the service
- name: "Make sure the service is started and enabled via systemd"
systemd:
name: "{{ MYSERVICE }}"
state: started
enabled: yes
daemon_reload: yes
tags: install,systemd
whereby it might be good to use insertbefore or insertafter also.
EDIT: This question appears to be getting views, so I'll add this:
https://askubuntu.com/questions/659267/how-do-i-override-or-configure-systemd-services
And this answer to another question of mine: https://askubuntu.com/a/1348117/1612
To quote it:
Don't edit files in /lib/systemd/ or /usr/share/systemd as they will get overwritten on updates.

Copy to remote shared path using ansible

I am trying to copy some files to a remote shared path.
---
- hosts: localhost
tasks:
- name: Test
copy:
src: /tmp/log/test.csv
dest: \\xyz_prod.com\public\app\
The playbook ran fine and it displayed changed=1 for the first run. When I ran it again, still it is successful and changed=0. But if I navigate to the shared location manually under the folder the test.csv file is not present. Can anyone please suggest what is wrong here?
dest must not include URL. Quoting from copy
Remote absolute path where the file should be copied to.
Try the play below
- hosts: xyz_prod.com
tasks:
- name: Test
copy:
src: /tmp/log/test.csv
dest: /public/app
For Windows remote hosts use win_copy which "Copies files to remote locations on windows hosts".
To copy from a remote server
use fetch – Fetch files from remote nodes.
See: Ansible - fetch files from one remote node to another.

Ansible archive remove option is not woking

I have to archive a directory on remote host and remove the directory once .tgz file is successfully created. I have the following in my playbook:
—-
- hosts: remote_hostName
archive:
path: /test/folderA
dest: /test/tmp/folderA.tgz
remove: True
register: result
I can see my /test/tmp/folderA.tgz being created. However, /test/folderA is still there even I set remove option as True. I am using Ansible 2.3.
give become: True a try. So you have no problems with rights.

How do i run a task in ansible only if contents of a directory has been modified

I am trying to write a task for my playbook to run a subsequent code only if changes are made to my wordpress directory. I came up with the script below however the code to redeploy the wordpress application isn't running when a change is made to the wordpress directory.
- name: Run if content is added or deleted in application directory
shell: find /var/www/html/wordpress -type f -exec md5sum {} \; | sort -k 2 | md5sum
register: change
- name: Copy code to application directory
unarchive:
src: /root/wordpress.zip
dest: /var/www/html
owner: apache
group: apache
mode: 0644
when:
- change.stdout|success
notify:
- Reload Apache server
I think that the problem is that you are using the shell module.
The shell module is considered a low level module in Ansible, meaning that it doesn't have "Idempotence" baked in.
So that shell command is going to run every time you run it, by default.
The way I've got it handled is that I use an git module for my app, and that git module then tells me if the state of my git repo is changed in relation to the state of the app that's on my production.
Example:
- name: Ensure demo application is at correct release.
git:
repo: https://github.com/foobar
version: "{{ app_version }}"
dest: "{{ app_directory }}"
force: true
accept_hostkey: true
register: app_updated
notify: restart nginx
In this example a change is only going to happen (the action that you want), when the state of the git repo is changed ( in relation to the repo thats on the server) and all the relevant actions in my playbook are going to be based on that fact.
So, you're on the right track, but I would advise you to use the copy module, that will copy files or folders only if there is a change between your source directory and the remote directory.
And then you can catch that change with a variable (like you did), and use it later in your playbook.

Resources