DEV Community

feng wei
feng wei

Posted on • Edited on

First Glance at Ansible

What is ansible

Ansible is an open-source IT automation tool that automates provisioning, configuration management, application deployment, orchestration, and many other IT processes. Ansible is written in Python and uses OpenSSH for transport.

Hands-on Ansible

  1. Install ansible on a Linux machine, which is called control node. Use "ssh-keygen" and "ssh-copy-id"commands to generate ssh key and copy it to managed nodes for authentication
  2. Playbooks are the simplest way in Ansible to automate repeating tasks in the form of reusable and consistent configuration files. Playbooks are scripts defined in YAML files and contain any ordered set of steps to be executed on managed nodes.

[My first playbook]

- hosts: webserver
  remote_user: whocare

  tasks:
  - name: make ~/whocare directory
    ansible.builtin.file:
      path: ~/whocare
      state: directory

  - name: Copy file 
    copy:
     src: /home/whocare.deb
     dest: /home/whocare.deb
     owner: whocare
     group: whocare
     mode: '0700'

  - name: install whocare
    become: true
    become_method: sudo
    ansible.builtin.apt:
      deb: /home/whocare.deb

  - name: copy dat file to /opt/Tanium/TaniumClient
    become: true
    become_method: sudo # need "-K" parameter, which is short form "--ask-become-pass". $ ansible-playbook <1.yaml> -K. 
    copy:
      src: /home/whocare.dat
      dest: /opt/whocare.dat

  - name: restart service
      become: true
      become_method: sudo
      service:
        name: whocare
        state: restarted
Enter fullscreen mode Exit fullscreen mode

3.Ansible configuration file. Default location is /etc/ansible/ansible.cfg. However, you can customize your owner configuration file and put it in your ansible playbook folder to "overwrite" default config. My ansible config file, for example:

[defaults]
inventory = ~/ansible_code/inventory/inventory.ini
private_key_file = ~/.ssh/ansible #private key!!! I put public key wrong and took me sometime for troubleshooting SSH key login issue.
log_path = ~/ansible_code/ansible.log #Log file to save all output for recording and troubleshooting
Enter fullscreen mode Exit fullscreen mode

Top comments (0)