0% found this document useful (0 votes)
5 views

Ansible_Notes

The document provides a comprehensive guide on using Ansible for user and package management, including commands for creating, deleting, and checking users, as well as installing packages like httpd. It also covers file management tasks such as creating files and directories, and demonstrates privilege escalation methods. Additionally, it includes instructions for creating and executing a playbook to automate user creation.

Uploaded by

sethmonil30
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Ansible_Notes

The document provides a comprehensive guide on using Ansible for user and package management, including commands for creating, deleting, and checking users, as well as installing packages like httpd. It also covers file management tasks such as creating files and directories, and demonstrates privilege escalation methods. Additionally, it includes instructions for creating and executing a playbook to automate user creation.

Uploaded by

sethmonil30
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Ansible Notes

## Ad-Hoc Commands

### User Management

#### Create a User:


ansible all -m user -a "name=daksh uid=1007 groups=mishu shell=/sbin/no

- name=daksh: Username
- uid=1007: User ID
- groups=mishu: Primary group
- shell=/sbin/nologin/: No login shell
- state=present: Ensures the user is present

#### Adding Privileges in ansible.cfg:


[privilege_escalation]
become=true
become_user=root
become_method=sudo
become_ask_pass=false

#### Creating and Deleting Users:


ansible all -m user -a "name=mishu state=present" # Add user
ansible all -m user -a "name=mishu state=absent" # Delete user

#### Checking User ID:


ansible all -m command -a "id mishu"

### Package Management

#### Install httpd:


ansible all -m yum -a "name=httpd state=present"

#### Check if httpd is installed:


ansible all -m command -a "rpm -q httpd"
### Connectivity Check
ansible server1.com -m ping

### File Management

#### Create a File:


ansible server1.com -m file -a "path=/root/file state=touch"

#### Create a Directory:


ansible server1.com -m file -a "path=/root/fileD state=directory"

#### Check File Listing:


ansible all -m command -a "ls -l /root"

#### Create Directory with Permissions:


ansible server1.com -m file -a "path=/root/file2 state=directory mode='777'

### Privilege Escalation

#### Without Privileges (Error Expected):


ansible all -m user -a "name=divy state=present"

#### Using -b or -become:


ansible all -m user -a "name=divy state=present" -b
ansible all -m user -a "name=priyansh state=present" -become

#### With Privileges Enabled in ansible.cfg:


ansible server1.com -m file -a "path=/root/file2 mode='777' owner=mishu st

### Copy Files


ansible all -m copy -a "src=/root/ansible/copyTrial dest=/root"

## Playbook Execution

### Creating a Playbook (user.yml):


---
- name: We are creating a user.
hosts: all
tasks:
- name: Create user tiger
user:
name: tiger
state: present

### Running Playbook:


ansible-playbook --syntax-check user.yml
ansible-playbook user.yml
ansible all -m command -a "id tiger"

You might also like