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"