Group: Example
ansible -i <inventory file> <group> -m <command:ping> -u <user> (if not same as user on machine)
ansible -i <inventory file> <group> -m <command:ping> -u <user> --ask-pass
ansible -i <inventory file> <group> -m <command:ping> -u <user> -K
ansible <group> -a "free -h"
If you don’t add a module (-m), it will automatically add -m commands
to the task.
---
- name: Set authorized key took from file
hosts: all
tasks:
- name: Set authorized key took from file
authorized_key:
user: <user>
state: present
key: "{{ lookup('file', '/home/<user>/.ssh/id_rsa.pub') }}"
To run it on new servers
ansible-playbook playbook.yml -e "ansible_user=<user> ansible_ssh_pass=<userpassword>"
To run it on servers where key is already added (pointless)
ansible-playbook playbook.yml
To run it on a specific group
ansible-playbook playbook.yml -l <group>
---
- name: Update server
hosts: all
become: yes
tasks:
- name: Run apt upgrade
apt:
upgrade: yes
update_cache: yes
- name: run dist-upgrade
apt:
upgrade: dist
To run sudo command
ansible-playbook playbook.yml -e "ansible_sudo_pass=<password>"