MalcolmChalmers.com

Anible Tips and Tricks

HOME
List Hosts

# to list hosts in a group
ansible <group_name> --list-hosts
e.g.
    ansible webservers --lists-hosts
    ansible all --list-hosts

Username in inventory file

# how to specify a different username and password in the inventory file

[web]
server1 ansible_host=192.168.1.10 ansible_user=alice ansible_password=Secret123
server2 ansible_host=192.168.1.11 ansible_user=bob ansible_password=AnotherPass

playbook - count files

---
- name: Count files in /var/log/audit and log result
  hosts: all
  become: true
  gather_facts: false

  tasks:
    - name: Count files in /var/log/audit
      ansible.built.shell: "ls -1 /var/log/audit | wc -l"
      register: file_count
      changed_when: false

    - name: Write hostname and count to local log file
      ansible.builtin.delegate_to: localhost
      lineinfile:
        path: "./audit_file_counts.log"
        line: "{{ inventory_hostname }}: {{ file_count.stdout }}"
        create: yes

playbook - ping

---
- name: Ping a host
  hosts: all
  gather_facts: no

  tasks:
    - name: Ping the host
      ansible.builtin.ping:


HOME