Delete user folder on windows using ansible - ansible

Unable to delete user directory using ansible win_file module, there is no error but it is not deleting
win_file:
path: C:\Users\myuser
state: absent
force: yes
Below one also I have tried where it tries to delete the current user folder and returns directory in use cos it is trying to delete the current user folder
- name: Remove directory structur
win_file:
path: C:\Users
name: myuser
state: absent
force: yes

It is best to delete the profile using a tool designed specifically for that purpose. This will help avoid cluttering the Windows registry and running into any number of other issues.
Check out this Ansible community module: https://docs.ansible.com/ansible/latest/collections/community/windows/win_user_profile_module.html
- name: Remove a profile for a still-existing account
community.windows.win_user_profile:
username: myuser
state: absent
If the account has already been deleted, you need to specify 'name' instead of 'username':
- name: Remove a profile for a deleted account
community.windows.win_user_profile:
name: myuser
state: absent

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.

Ansible: How to delete a folder and file inside a directory in a single task?

I'm using Ansible 2.3.2.0 and am trying to delete a file and folder inside a directory in one task.
Right now I have this
tasks:
- name: Removing existing war
file:
path: /usr/share/tomcat/webapps/app.war
state: absent
- name: Removing existing folder
file:
path: /usr/share/tomcat/webapps/app
state: absent
I cannot simply remove the webapps folder because I do not want to delete other files and folders in there. I want to reduce the number of tasks because I am using Duo push auth and this adds to the deploy time. I've tried looping over files and file globs but for some reason it never works.
http://docs.ansible.com/ansible/latest/playbooks_loops.html#looping-over-files
Simply iterate over the two values:
tasks:
- name: Removing
file:
path: "{{ item }}"
state: absent
with_items:
- /usr/share/tomcat/webapps/app.war
- /usr/share/tomcat/webapps/app
But it will still create 2 tasks executions: one for each item.
If you simply want to delete a directory and its contents, just use the file module and pass the path to the directory only:
tasks:
- name: Removing
file:
path: /usr/share/tomcat/webapps/app
state: absent
See this post: Ansible: How to delete files and folders inside a directory?
and from the ansible docs:
If absent, directories will be recursively deleted, and files or symlinks will be unlinked.
see: http://docs.ansible.com/ansible/latest/file_module.html

Ansible: Create new user and copy ssh-keys from local system

I'm new to Ansible and I'm struggeling with creating a new user on a remote machine and copying ssh-keys (for git) from the local machine to the remote machine's new user.
Basically, from localmachine/somepath/keys/ to remotemachine/newuser/home/.ssh/.
So far I tried:
- name: Create user
hosts: remote_host
remote_user: root
tasks:
- name: Create new user
user: name=newuser ssh_key_file=../keys/newuser
While this creates the newuser on the remote machine, it doesn't copy any keys (.ssh is still empty). I also tried authorized_key as a second task but only got an error message when trying to copy the private key.
Is it even possible that the keys are still added after I already ran it and newuseralready exists. Ie, can I just run it again or will I have to delete the newuser first?
The ssh_key_file is the path used by the option generate_ssh_key of user module. It's not the path of a local SSH key to upload to the remote user created.
If you want to upload the SSH key, you have to use the copy module
- name: Create user
hosts: remote_host
remote_user: root
tasks:
- name: Create new user
user:
name: newuser
- name: Create .ssh folder
file:
path: ~newuser/.ssh
state: directory
owner: newuser
group: newuser
mode: 0700
- name: Upload SSH key
copy:
src: ../keys/newuser
dest: ~newuser/.ssh/id_rsa
owner: newuser
group: newuser
mode: 0700
BTW, it's recommended to use the YAML syntax instead of the args syntax.

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.

ansible create user: no default .bashrc created?

I am creating users with these tasks:
- name: ensure home directory
sudo: yes
file: path={{item.home}} state=directory
with_items: users
- name: create user {{item.name}}
sudo: yes
user: name={{item.name}} home={{item.home}} shell=/bin/bash group={{item.group}} groups={{item.groups}} password={{item.password}} state=present
with_items: users
but it seems that a ~/.bashrc per user is not created in their home directory.
Is there a way to create a default basic .bashrc file?
Thanks
Starting from Ansible 2.0, you can pass along a skeleton option:
Optionally set a home skeleton directory. Requires createhome option!
This works for me:
- name: "Create deployment user"
user:
name: "foobar"
groups: "sudo"
append: yes
skeleton: "/etc/skel"
createhome: yes
I used a specific path for the home folders (different than the traditionnal /home/user1 folder): /specific/path/home/user1
Since I had an error that the path /specific/path/home/user1 could not be created when I was creating the user1, I then created the home folders before creating the users.
However if the home folder already exists when a user is created, the default .bashrc is not copied.
You have to add append=yes at the end of the user command
user:
- name: Add users
add_user:
name: '{{item.name}}'
home: '{{item.home}}'
shell: /bin/bash
group: '{{item.group}}'
groups: '{{item.groups}}'
password: '{{item.password}}'
state: present
append: yes
with_items: users

Resources