IT automation - Ansible

Ansible

Requirement

Quick start

1
2
apt-get install ansible
ansible localhost -m ping

Run task at remote host

  • setup password-less ssh connection
    1
    2
    3
    4
    5
    # add remote servers
    echo "[webservers]" >> /etc/ansible/hosts
    echo "www[01:50].example.com" >> /etc/ansible/hosts
    # test remoteservers
    ansible remoteservers -a "/bin/echo hello"

Other usage

1
2
3
4
5
6
7
8
9
10
# run as python2
ansible localhost -m ping -e 'ansible_python_interpreter="/usr/bin/env python"'
# run as python3
ansible localhost -m ping -e 'ansible_python_interpreter="/usr/bin/env python3"'

# run apt install screen
ansible 172.17.0.3 -m apt -a "name=screen"

# run `uptime`
ansible client -m shell -a "uptime"

Reference

https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html