What is Ansible Playbooks.
yum
shell
command
template
user:
stat - check if a file exists
apt
copy
service
file
lineinfile
archive/unarchive
git
package
ec2
---
- hosts: jenkins ===> Remote name/group name
gather_facts: True ===> default Variable True /False
become: yes ===> root user
# remote_user:
ansadm ===> remote user defined
vars: ===> Variable defined
my_content:
"this file created using var concept" ===> variable name "my_content"
tasks: ===> First Play 1
- name: create a file
var_file.txt.. ===> First Task and Task Name
copy: ===> Module name
dest:
/tmp/var_file.txt
content:
"{{ my_content }}"
- name: install
nginx service ===> Second Task
yum:
name: httpd
state:
present
- name: httpd
service start ===> third Task
service:
name: httpd
state: started
- name: remove file ===> fourth Task
file:
path:
/tmp/Shashi.txt
state:
absent
2. Logs redirect to output.log file.
# ansible-playbook httpd.yml | tee -a output.log
yum state :-
- latest.
- present.
- installed.
- absent.
- removed.
Services : state :-
- reloaded
- restarted
- running
- started
- stopped
---
- name: Restart network service for interface eth0
service:
name: network
state: restarted
args: eth0
2. Copy Module
---
- name: Copy file with owner and permissions
copy:
src: /etc/files/jk.conf
dest: /srv/jk.conf
owner: shashi
group: shashi
mode: '0644'
---
- name: Change file ownership, group, and permissions
file:
path: /etc/jk.conf
owner: root
group: root
mode: '0644'
---
- name: Remove file (delete file)
file
path: /etc/shashi.conf
state: absent
---
- name: create a directory if it doesn’t exist
file:
path: /etc/mydirectory
State: directory
mode: '0777'
---
- name: Recursively deleting a directory
file:
path: /etc/shashi.conf
state: absent
---
- name: Ensure SELinux is set to enforcing mode
lineinfile:
path: /etc/selinux/config
regexp: '^SELINUX='
line: SELINUX=disabled
SELINUX=disabled
---
- name: Add a line to a file if the file does not exist, without passing regexp
lineinfile:
path: /etc/hosts
line: 10.200.50.51 shashi.com
create: yes
- name: Compress directory /path/to/shashi_dir/ into /path/to/shashi.tgz
archive:
path: /path/to/shashi_dir
dest: /path/to/shashi.tgz
- name: Compress regular file /path/to/shashi into /path/to/foo.gz and remove it
archive:
path: /path/to/shashi
dest: /path/to/shashi.tgz
remove: yes
- name: Create a bz2 archive of /path/to/shashi
archive:
path: /path/to/shashi
format: bz2
---
- hosts: localhost
gather_facts: no
vars:
username: decodingdevops
token: d6sdfshhghyjggj448t8tnt9h
repo_name: devops
- name: Checkout The Code From Github Using Ansible.
git:
repo: 'https://{{ token }}@github.com/{{ username }}/{{ repo_name }}.git'
dest: /root/mycode
i) cat command.
- name: Executing a command using the command module
command: cat helloworld.txt
---
- name: Check the remote host uptime
hosts: servers
tasks:
- name: Execute the Uptime command over Command module
register: uptimeoutput
command: "uptime"
var: uptimeoutput.stdout_lines
i) Check Disk Space
---
- hosts: all
become: yes
- name: Execute /boot usage on Hosts
command: 'df -Th /'
register: df
- debug: var=df.stdout
No comments:
Post a Comment