Ansible archive remove option is not woking - ansible

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.

Related

ansible link from directory to directory

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.

Creating a directory with Ansible results in file ownership change in /usr/lib/python3.7

Using Ubuntu 19.04 and Python 3.7
When I create a directory with Ansible, file ownership is changed in /usr/lib/python3.7
Obviously this should not happen and it breaks Python for the whole of Ubuntu, until the permissions are corrected.
The problem only happens when recurse=yes is used.
Here is a video showing the problem:
https://youtu.be/d3hj255pW9w
Here is the code/Ansible playbook:
ubuntu#ubuntu-GB-BXi3-5010:~/ansible$ cat ~/ansible/renderworker/ffmpegconverter/ansible/test.yml
---
- hosts: renderworkerhosts
become: yes
tasks:
# for some reason, recurse=yes results in owner & permissions changing in /usr/lib/python3.7
# which breaks Python on Ubuntu
- name: Create directory
file:
path=/opt/ffmpegconverter
state=directory
owner=root
group=ffmpeguser
mode=u=rwx,g=rwx,o=
recurse=yes
ubuntu#ubuntu-GB-BXi3-5010:~/ansible$
You should use a colon instead of the equal sign for delimiter between keys and values:
path: /etc/foo.conf

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.

How to delete local directory with ansible

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

How to get facts at each command in Ansible

Ansible get facts only at start. But i need check facts at each commands.
For example:
I need create a directory, after that i need put file to this directory. But ansible get fact 'dir doesn't exist' at start, create dir and at next step fact still FALSE and ansible skip this step =( And do this step only after second run.
I'll try setup after all steps to gathering facts again but it doesn't work.
I do it like this:
- stat: path=/etc/zabbix/scripts/rabbitmq
register: rmqscriptdir
- name: Create scripts dir if not exist
when: rmqscriptdir.stat.exists == False
shell: mkdir /etc/zabbix/scripts/rabbitmq
- name: Gathering facts again
setup:
- name: Set owner and permissions to rabbitmq directory
when: rmqscriptdir.stat.exists == True
file: path=/etc/zabbix/scripts/rabbitmq owner=zabbix group=root mode=0750
- stat: path=/etc/zabbix/scripts/rabbitmq/api.py
register: rmqscript_api
- name: Create api.py if not exist
when: rmqscript_api.stat.exists == False and rmqscriptdir.stat.exists == True
shell: cd /etc/zabbix/scripts/rabbitmq; wget https://raw.githubusercontent.com/jasonmcintosh/rabbitmq-zabbix/master/scripts/rabbitmq/api.py
- name: Gathering facts again
setup:
- name: Set owner and permissions to api.py
when: rmqscript_api.stat.exists == True
file: path=/etc/zabbix/scripts/rabbitmq/api.py owner=zabbix group=root mode=0755
I think you misunderstand what the setup module does. By registering a value it does not become a fact that will be reloaded by the setup module when run again. Your registered value stays the same. If you want to check again if a path exists you do not need to re-run the setup module, but the stats module and again register its output.
But anyway, the idea of Ansible is actually to not manually check if every task should be executed or not. That is something Ansible takes care for you, Ansible in general is indepotent, meaning it will have the same result no matter how many times you run the play.
Here is a cleaned up version, which creates a folder and downloads the file. If the folder already exists, the 1st task will do nothing. If the file api.py already exists, the 2nd task will do nothing.
- name: Create scripts dir if not exist
file:
path: /etc/zabbix/scripts/rabbitmq
state: directory
owner: zabbix
group: root
mode: 0750
- name: Create api.py if not exist
get_url:
url: https://raw.githubusercontent.com/jasonmcintosh/rabbitmq-zabbix/master/scripts/rabbitmq/api.py
dest: /etc/zabbix/scripts/rabbitmq/api.py
owner: zabbix
group: root
mode: 0755
PS: If you want to see which values are reloaded by the setup module, you can register its output and show it in a debug task, like so:
- setup:
register: all_server_facts
- debug:
var: all_server_facts
This only contains server facts, info about cpu, hard drives, network etc. Also see this answer for an example output.

Resources