Basic Knowledge of Ansible for Automated Operations and Maintenance

Ansible Host Directory

Default configuration file: /etc/ansible/hosts

# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses
#   - A hostname/ip can be a member of multiple groups

# Ex 1: Ungrouped hosts, specify before any group headers:

## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10

# Ex 2: A collection of hosts belonging to the 'webservers' group:

## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110

# If you have multiple hosts following a pattern, you can specify
# them like this:

## www[001:006].example.com

# You can also use ranges for multiple hosts: 

## db-[99:101]-node.example.com

# Ex 3: A collection of database servers in the 'dbservers' group:

## [dbservers]
##
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57

# Ex4: Multiple hosts arranged into groups such as 'Debian' and 'openSUSE':

## [Debian]
## alpha.example.org
## beta.example.org

## [openSUSE]
## green.example.com
## blue.example.com

Ansible Command Format

ansible <host-pattern> [options]

# Check Ansible installation environment Check if all remote hosts have an environment created by the "root" user that the ansible management host can access
ansible all -m ping -u root

# Execute command Execute "echo hello" on remote hosts
ansible all -a "/bin/echo hello"

# Copy file Copy file /etc/hosts to remote host group "web", location is /tmp/hosts
ansible web -m copy -a "src=/etc/hosts dest=/tmp/hosts"

# Install package Install yum package on remote host "web"
ansible web -m yum -a "name=acme state=present"

# Add user
ansible all -m user -a "name=foo password=<crypted password here>"

# Download git repository
ansible web -m git -a "repo=git://foo.example.org/repo.git dest=/srv/myapp version=HEAD"

# Start service
ansible web -m service -a "name=httpd state=started"

# Execute in parallel Start 10 parallel executions to reboot
ansible lb -a "/sbin/reboot" -f 10

# View all system information of remote hosts
ansible all -m setup

Ansible Scripts

Ansible manages hosts using scripts called Playbooks, which use YAML format, and files have a .yml or .yaml suffix

# Method to execute Playbook script
ansible-playbook deploy.yml
# Keywords:
hosts: hosts
remote_user: execute as a certain user
vars: variables
tasks: the core of the Playbook, defines the actions to be executed in order. Each Action calls an Ansible module.
  action syntax:
  module: module_parameter=module_value
  Common modules include yum, copy, template, etc.
handlers: event handling operations of the playbook, which will only execute when activated. Multiple triggers will only execute once, and in the order declared
# Basic structure of Playbook
---
- hosts: web
  remote_user: root
  tasks:
  - name: ensure apache is at the latest version
   yum: pkg=httpd state=latest
# Using Ansible's get_url and command modules (suitable for Linux and Windows) to download and install VMware Tools. First, download the VMware Tools tarball, then extract and run the installation script. Note that the specific URL and version number need to be adjusted according to actual conditions.

- name: Install VMware Tools on Linux
  hosts: linux_hosts
  become: yes
  become_method: sudo

  tasks:
    - name: Download VMware Tools tarball
      get_url:
        url: "http://web-download.vmware.com/vmw-download/vms/vmtoolbox/11.x/vmware-tools-distrib/VMwareTools-11.0.0-2080590.tar.gz"
        dest: /tmp/VMwareTools.tar.gz
        mode: '0755'

    - name: Extract VMware Tools tarball
      command: tar zxvf /tmp/VMwareTools.tar.gz -C /tmp/
      args:
        chdir: /tmp/vmware-tools-distrib
        creates: /tmp/vmware-tools-distrib/vmware-install.pl

    - name: Install VMware Tools
      command: /tmp/vmware-tools-distrib/vmware-install.pl --default

Ansible Modules

Using modules in the command line: -m followed by the name of the module; -a followed by the parameters of the module

# Use the copy module to copy the management node file /etc/hosts to all remote hosts /tmp/hosts
ansible all -m copy -a "src=/etc/hosts dest=/tmp/hosts"
# Use the yum module to install the httpd package on remote host web
ansible web -m yum -a "name=httpd state=present"

Using modules in Playbook scripts: the name of the module is before the colon; the parameters of the module are after the colon

---
 tasks:
 - name: ensure apache is at the latest version
   yum: pkg=httpd state=latest

Common Modules

Debugging and testing modules: ping and debug

File modules: copy, template, file

Common Linux operations: user, yum, service, firewalld

Execute shell commands: shell, command

Leave a Comment