Ansible Basics

 Ansible Basics

Ansible
Ansible

Connections:

# Sample Inventory File

web1 ansible_host=server1.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123!
web2 ansible_host=server2.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123!
web3 ansible_host=server3.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123!
db1 ansible_host=server4.company.com ansible_connection=winrm ansible_user=administrator ansible_password=Password123!

Whereas,
  • web1 & db1 denotes alias name for your server.
  • ansible_host denotes your Server host IP / Name.
  • ansible_connection denotes the type of your connection (for Linux: ssh & for windows: winrm)
  • ansible_user denotes the user name of your server
  • ansible_ssh_pass, ansible_ssh_key (for Linux) / ansible_password (for windows)

Create Groups:

# Sample Inventory File
# Web Servers

web1 ansible_host=server1.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123!

web2 ansible_host=server2.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123!

web3 ansible_host=server3.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123!

# Database Servers

b1 ansible_host=server4.company.com ansible_connection=winrm ansible_user=administrator ansible_password=Password123!

[web_servers]
web1
web2
web3

[db_servers]
db1

Whereas,
  • [Group name] denotes the name of the group
  • Sub-items denote Group items

Parent Group:

[parent_group:children]
child_group1
child_group2

Whereas,
  • parent_group denotes the name of your parent group
  • child_group1 indicates the name of the groups you want to merge 
For Eg:

sql_db1 ansible_host=sql01.xyz.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Lin$Pass

sql_db2 ansible_host=sql02.xyz.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Lin$Pass

web_node1 ansible_host=web01.xyz.com ansible_connection=winrm ansible_user=administrator ansible_password=Win$Pass

web_node2 ansible_host=web02.xyz.com ansible_connection=winrm ansible_user=administrator ansible_password=Win$Pass

web_node3 ansible_host=web03.xyz.com ansible_connection=winrm ansible_user=administrator ansible_password=Win$Pass

[db_nodes]
sql_db1
sql_db2

[web_nodes]
web_node1
web_node2
web_node3

[boston_nodes]
sql_db1
web_node1

[dallas_nodes]
sql_db2
web_node2
web_node3

[us_nodes:children]
boston_nodes
dallas_nodes

Q: Update the playbook with a play to Execute a script on all web server nodes. The script is located at /tmp/install_script.sh

Ans: Playbook.yml are written in YAML Language as follows:

-

    name: 'Execute a script on all web server nodes'

    hosts: web_nodes

    tasks: 

        -

            name: 'Execute a script on all web server nodes'

            script: /tmp/install_script.sh

        -

            name: 'start httpd services'

            service: 'name=httpd state=started'

Most used Ansible Modules:

1. Package management
    name: how to install any package
    dnf / apt / yum:
        name: <packagename>
        state: latest

2. Service
-
    name: Start service foo, based on running process /usr/bin/foo
    service:
        name: foo
        pattern: /usr/bin/foo
        state: started

3. Copy
    name: Copy file with owner and permission, using symbolic representation
    copy:
        src: /srv/myfiles/foo.conf
        dest: /etc/foo.conf
        owner: foo
        group: foo
        mode: u=rw,g=r,o=r

4. Debug
    name: Write some content in a file /tmp/foo.txt
    copy:
        dest: /tmp/foo.txt
        content: |
            Good Morning!
            Awesome sunshine today.
        register: display_file_content
    name: Debug display_file_content
        debug:
            var: display_file_content
            verbosity: 2
5. File
    name: Create a directory if it does not exist
    file:
        path: /etc/some_directory
        state: directory
        mode: '0755' 

6. Lineinfile
    name: Add a line to a file if the file does not exist, without passing regexp
    lineinfile:
        path: /etc/resolv.conf
        line: 192.168.1.99 foo.lab.net foo
        create: yes 

7. Git
    git:
        repo: https://github.com/ansible/ansible-examples.git
        dest: /src/ansible-examples
        separate_git_dir: /src/ansible-examples.git 

8. Cli_command 
    name: configurable backup path
    cli_config:
        config: "{{ lookup('template', 'basic/config.j2') }}"
        backup: yes
        backup_options:
            filename: backup.cfg
            dir_path: /home/user 

9. Archive 
    name: Create a bz2 archive of multiple files, rooted at /path
    archive:
        path:
            - /path/to/foo
            - /path/wong/foo
            dest: /path/file.tar.bz2
            format: bz2 

10. Command 
    name: Change the working directory to somedir/ and run the command as db_owner if /path/to/database does not exist.
    command: /usr/bin/make_database.sh db_user db_name
        become: yes
        become_user: db_owner
        args:
            chdir: somedir/
            creates: /path/to/database

Ansible Core Components

  1. Inventories
  2. Modules
  3. Variables
  4. Facts
  5. Plays
  6. Playbooks
  7. Configuration Files

Basic Commands:

# ansible <group> -m <module> -a <arguments>
  • Check Ansible Version
    • ansible --version
  • Dry Run
    • ansibal-palybook playbook.yml --check

  • Managing services
    • Starting a service
      • # ansible <group> -m service -a “name=httpd state=started”
    • Stopping a service
      • # ansible <group> -m service -a “name=httpd state=stopped”
    • Restarting a service
      • # ansible <group> -m service -a “name=httpd state=restarted”
  • Managing Packages
    • Check if the package is installed & update it
      • # ansible <group> -m yum -a “name=httpd state=latest”
    • Check if the package is installed & don’t update it
      • # ansible <group> -m yum -a “name= httpd state=present”
    • Check if the package is at a specific version
      • # ansible <group> -m yum -a “name= httpd-1.8 state=present”
    • Check if the package is not installed
      • # ansible <group> -m yum -a “name= httpd state=absent”
  • Check the connectivity of hosts
    • # ansible <group> -m ping
  • Rebooting hosts
    • # ansible <group> -a “/sbin/reboot”
  • Checking the host’s system information
    • # ansible <group> -m setup | less
    • # ansible <group> -m setup -a “filter=ansible_distribution”
  • Transferring files
    • # ansible <group> -m copy -a “src=/home/ansible dest=/tmp/home”
  • Managing users
    • Creating a new user
      • # ansible <group> -m user -a “name=ansible password=<encrypted password>”
    • Deleting a user
      • # ansible <group> -m user -a “name=ansible state=absent”
  • Changing permissions & ownership
    • Changing permission for a file
      • # ansible <group> -m file -a “dest=/home/ansible/file1.txt mode=777”
    • Changing the ownership of a file
      • # ansible <group> -m file -a “dest=/home/ansible/file1.txt mode=777 owner=ansible group=ansible”

  • Tag Formatting
    • ansibal-palybook playbook.yml --tags “install”
    • ansibal-palybook playbook.yml --skip-tags “install”

Comments

People also Look For

All about DevOps (A Complete Guide to DevOps)

How to Implement Microservice Coded In Hackathon Event

Upcoming DevOps trends

All about Cloud Computing