Difference between revisions of "Ansible"

From Indie IT Wiki
imported>Plittlefield
 
Line 69: Line 69:
  
 
=== Examples ===
 
=== Examples ===
 +
 +
'''Update Package List and Upgrade System'''
 +
 +
Create hosts file...
 +
 +
<code>./inventory/hosts</code>
 +
 +
[servers]
 +
servername ansible_connection=ssh ansible_user=username
 +
 +
Create the playbook file...
 +
 +
<code>./playbooks/apt.yml</code>
 +
 +
- hosts: "*"
 +
  become: yes
 +
  tasks:
 +
    - name: apt
 +
      apt:
 +
        update_cache: yes
 +
        upgrade: 'yes'
 +
 +
Run the playbook command...
 +
 +
ansible-playbook ./playbooks/apt.yml -i ./inventory/hosts
  
 
'''Create single directory using the 'file' module.'''
 
'''Create single directory using the 'file' module.'''

Revision as of 11:21, 21 July 2021

Introduction

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code.

It is used to deploy and maintain many servers at once, from the command line.

  • Control Node = Admin Computer
  • Host Node = Remote Server

How To Install and Configure Ansible on Ubuntu 20.04

Installation

sudo apt-get -y install ansible

Configuration

Add your hosts...

sudo nano /etc/ansible/hosts

[servers]
myserver ansible_host=123.456.789.0

[all:vars]
ansible_python_interpreter=/usr/bin/python3

Check the config...

ansible-inventory --list -y

all:
  children:
    servers:
      hosts:
        myserver:
          ansible_host: 123.456.789.0
          ansible_python_interpreter: /usr/bin/python3
    ungrouped: {}

Testing

AWS SSH Key

ansible all -m ping -u ubuntu --private-key=~/.ssh/myserver.pem

myserver | SUCCESS => {
   "changed": false,
   "ping": "pong"
}

Commands

You can perform one off commands on a single host or multiple hosts.

Hostname

ansible all -u ubuntu --private-key=~/.ssh/myserver.pem -a "hostname -f"

Update Package Cache

ansible all -u ubuntu --private-key=~/.ssh/myserver.pem -a "sudo apt-get update"

Playbooks

Playbooks are YAML text files which contain commands and options in a text file, just like a docker compose file.

The file contains 'modules' which perform different tasks.

Examples

Update Package List and Upgrade System

Create hosts file...

./inventory/hosts

[servers]
servername ansible_connection=ssh ansible_user=username

Create the playbook file...

./playbooks/apt.yml

- hosts: "*"
  become: yes
  tasks:
    - name: apt
      apt:
        update_cache: yes
        upgrade: 'yes'

Run the playbook command...

ansible-playbook ./playbooks/apt.yml -i ./inventory/hosts

Create single directory using the 'file' module.

Create the playbook...

nano mkdir.yml

- hosts: all
  tasks:
  - name: Ansible file module create directory
    file:
      path: ~/backups
      state: directory

Run the playbook (dry run)...

ansible-playbook -C mkdir.yml

Run the playbook...

ansible-playbook mkdir.yml

Create multiple directories using the 'file' module.

Create the playbook...

nano mkdirs.yml

- hosts: all
  tasks:
  - name: Ansible create multiple directories with_items    
    file:
      path: ~/backups/{{item}}
      state: directory
      with_items:
      - 'mysql'      
      - 'repository'
      - 'config'

Run the playbook...

ansible-playbook mkdirs.yml

https://linuxhint.com/create-directory-ansible/

Install Apache web server software and start it on a Red Hat based system...

- name: Playbook
  hosts: webservers
  become: yes
  become_user: root
  tasks:
    - name: ensure apache is at the latest version
      yum:
        name: httpd
        state: latest
    - name: ensure apache is running
      service:
        name: httpd
        state: started