Ansible

Basic Commands

Check / Execute Cmd / Get Vars

# ping all hosts
ansible -i inventory -m ping all
# execute command
ansible -i inventory -m shell -a '[[ -f /etc/hostname ]] && /usr/bin/cat /etc/hostname' all
ansible -i inventory -m shell -a '/bin/systemctl restart apache2.service' web
# get hosts facts
ansible -i inventory -m setup web
ansible -i inventory -m setup base -a "filter=ansible_os_family"

Filter by OS Family

Playbook’s imports filtered by os_family

- name: Check OS Family
  hosts: base
  gather_facts: True
  tasks:
    - debug: var=hostvars[inventory_hostname]['ansible_facts']['os_family']

- name: Import Common Network
  import_tasks: lib/network/common_network.yml
- name: Import Debian Network
  import_tasks: lib/network/debian_network.yml
  when: ansible_os_family == "Debian"
- name: Import RedHat Network
  import_tasks: lib/network/redhat_network.yml
  when: ansible_os_family == "RedHat"