0% found this document useful (0 votes)
8 views19 pages

Automation

The document provides a comprehensive guide on installing and using Ansible for automation on various Linux distributions, including CentOS, Rocky Linux, and Ubuntu. It covers the installation process, ad-hoc commands, configuration files, and various Ansible modules for tasks such as managing users, services, and files. Additionally, it includes examples of playbook commands and module usage for common automation tasks.

Uploaded by

gototheschoolcar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views19 pages

Automation

The document provides a comprehensive guide on installing and using Ansible for automation on various Linux distributions, including CentOS, Rocky Linux, and Ubuntu. It covers the installation process, ad-hoc commands, configuration files, and various Ansible modules for tasks such as managing users, services, and files. Additionally, it includes examples of playbook commands and module usage for common automation tasks.

Uploaded by

gototheschoolcar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Linux Automation

node1 node2 node3

Network
control

node4 node5
Control Site

Installing ansible on Centos,Rockylinux

yum install epel-release -y

yum install ansible -y

Installing ansible on Ubuntu

apt-add-repository --yes --update ppa:ansible/ansible-2.9

apt install ansible -y

ansible –version ( check ansible version)
Ad-Hoc ansible commands

Default inventory files path (/etc/ansible/)

Config file path (/etc/ansible/ansible.cfg)

Create inventory files

ansible all -i myhosts --list-hosts (check all group)

ansible db -i myhosts --list-hosts (check db group)

ansible ungroup -i myhosts --list-hosts (non group host)
Ansible create own config file

[defaults]

inventory = myhosts

remote_user = bozin

host_key_checking = false

[privilege_escalation]

become = true

become_method = sudo

become_user = root

become_ack_pass = false
Ad-hoc command
Ad-hoc command in ansible

ansible host_pattern -m module_name -a
“module_option”

ansible myhosts -m command -a “uptime”
Ansible module

Command (not support pipe | and python
needed)

Shell (support pipe and python needed)

Raw ( No need python )
Ansible playbook
Playbook format
Ansible playbook command
ansible-playbook playbook_name.xml

Ansible modules

* copy Module *

- name: Ensure MOTD file is in place

copy:

src: files/motd

dest: /etc/motd

owner: root

group: root

mode: 0644
Ansible modules

* user modules *

- name: Ensure user ricardo exists

user:

name: ricardo

group: users

groups: wheel

uid: 2001

password: "{{ 'mypassword' | password_hash('sha512') }}"

state: present
Ansible Modules

* package modules *

- name: Ensure Apache package is installed

package:

name: httpd

state: present
Ansible modules

* service module *

- name: Ensure SSHD is started

service:

name: sshd

state: started
Ansible modules

* firewall module* for service

- name: Ensure port 80 (http) is open

firewalld:

service: http

state: enabled

permanent: yes

immediate: yes

For port

- name: Ensure port 3000/TCP is open

firewalld:

port: 3000/tcp

state: enabled

permanent: yes

immediate: yes
Ansible modules

*file module *

- name: Ensure directory /app exists

file:

path: /app

state: directory

owner: ricardo

group: users

mode: 0770

You can add “recurse : yes” or delete “state : absent”
Ansible Modules

* lineinfile module*

- name: Ensure host rh8-vm03 in hosts file

lineinfile:

path: /etc/hosts

line: 192.168.122.236 rh8-vm03

state: present

- name: Ensure root cannot login via ssh

lineinfile:

path: /etc/ssh/sshd_config

regexp: '^PermitRootLogin'

line: PermitRootLogin no

state: present
Ansible Modules

* unarchive module*

- name: Extract contents of app.tar.gz

unarchive:

src: /tmp/app.tar.gz

dest: /app

remote_src: yes
Ansible modules

* command module *

- name: Run the app installer

command: "/app/install.sh"

You might also like