ansible

install

  • pip case

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    # (optional) install pip
    #python3 -m ensurepip --upgrade
    #python3 -m pip --version

    # or
    apt install python3-pip

    # config pip source ( pip version > 10.0.0)
    python3 -m pip config set global.index-url https://mirrors.aliyun.com/pypi/simple

    # install
    python3 -m pip install ansible # or with '--user' which just install on current user
  • rocky case

    1
    2
    # Rocky / RHEL / CentOS / Alma / Rocky
    sudo dnf install -y ansible
  • ubuntu case

    1
    2
    3
    4
    sudo apt update
    sudo apt install -y software-properties-common
    sudo add-apt-repository --yes --update ppa:ansible/ansible
    sudo apt install -y ansible

uninstall

1
2
3
4
5
6
7
8
9
# pip install case
python3 -m pip uninstall -y ansible ansible-core

# Rocky/RHEL/CentOS package case
sudo dnf remove -y ansible ansible-core

# Ubuntu/Debian package case
sudo apt remove --purge -y ansible ansible-core
sudo apt autoremove -y

config

changed_when: false

The command and shell modules default to reporting changed: true because Ansible cannot know if the command modified the system. If you run a read-only command (like grep or df), you should use changed_when: false.

command

list nodes

1
2
3
4
5
# all nodes
ansible all --list-hosts

# list specify group nodes
ansible client_servers -i inventory/hosts.yaml --list-hosts

run a quick command

1
2
3
4
5
6
7
8
9
10
ansible <pattern> -m <module> -a "<arguments>"

# Run on a single specific node using the shell module
ansible all -m shell -a "df -h | grep /dev/nvme" --limit "node1"

# Run on multiple specific nodes
ansible all -m shell -a "systemctl status kubelet" -l "node1,node2"

# Run on an exact IP address
ansible app_group -m shell -a "podman ps -a" --limit "192.168.1.10"

use specify inventory

1
ansible-playbook -e "@inventory/group_vars/xxx.yml" -i inventory/hosts-xx.yml playbooks/xxx.yml