Created by on 11/12/2020 , Last updated: 01/08/2021.
👉  github shields Star me if it’s helpful.
Ansible is an IT automation tool.

1. Install Ansible

    python -m virtualenv ansible  # Create a virtualenv if one does not already exist
source ansible/bin/activate   # Activate the virtual environment
pip install ansible

2. Inventory

    # Default location: /etc/ansible/hosts.
# Use -i <path> to specify a different inventory file
Prod ansible_ssh_user=[username] ansible_ssh_host=[ip_address] ansible_ssh_port=22 ansible_ssh_private_key_file=[ssh-key-file-path]

3. Playbook

    # playbook.yml
- name: Playbook Example
  hosts: Prod
    - name: Pull GitHub Repo (with SSH ForwardAgent enabled)
        repo: '[email protected]:xxx/xxx.git'
        dest: /dest/path
    - name: Run shell command in specific directory
      shell: npm install
        chdir: /git-repo/path
    - name: Sync Files
        src: source/path
        dest: /source/path
    - name: Mange supervisor processs
      supervisorctl: name=[process-name] state=[start|stopped]
    - name: Update nginx config
      template: src=templates/nginx_conf.j2
    - name: Reload nginx
      service: name=nginx state=reloaded

4. ansible.cfg

    # lookup order:
#1. File specified by the ANSIBLE_CONFIG environment variable
#2. ./ansible.cfg (current directory)
#3. ~/.ansible.cfg (home directory)
#4. /etc/ansible/ansible.cfg
transport = ssh

ssh_args = -o ForwardAgent=yes -o IdentitiesOnly=yes -o ControlMaster=auto -o ControlPersist=4h -o ControlPath=~/.ssh/%h-%p-%r

5. Run playbook

    ansible-playbook -i path-to-my-inventory playbook.yml

6. Enable SSH ForwardAgent

    # ~/.ssh/config
Host [server-address-here] [ip-address-here]
    ForwardAgent yes

Maitained by, Copy Rights @ CC BY-NC-SA 4.0