# On RHEL
dnf install -y ansible-core
# On mac
brew install ansible
# If you dont already have a ssh key pair
ssh-keygen -t rsa -b 4096
# Copy your public key to host
ssh-copy-id -o StrictHostKeyChecking=no <user>@<ip_address>
ansible is for adhoc commands
# List all hosts configured in inventory
ansible --list-hosts all
# ping
ansible all -m ping # Run on all
ansible g1 -m ping # Run on group of nodes
ansible node1 -m ping # Run on single node named `node1` in inventory file
# command
ansible node1 -m command -a "hostname"
# Gather Facts
ansible node1 -m ansible.builtin.setup >> node1_facts.json
# Check Syntax of all playbook in this repo
aplaybook playbooks/* --syntax-check
ansible-playbook playbooks/index.html.j2 --syntax-check # Should give error
# See what hosts will be targetted. --list-hosts
ansible-playbook -l g123 playbooks/helloworld.yaml --list-hosts
# Run helloworld playbook.
# -u rc -c ssh|local -e variables.yaml -i inventory1 -l subset
ansible-playbook -i inventory -l all playbooks/helloworld.yaml
ansible-playbook -l all playbooks/helloworld.yaml # Run on all
# --step , step through each task 1 by 1
ansible-playbook -l node1 playbooks/helloworld.yaml --step
# Playbook to gather facts
ansible-playbook -l node1 playbooks/gatherFacts.yaml
- inventory
- on mac
- /etc/hosts
- /Users/arslankhan/.ssh/config
- ansible.cfg
- variable file
- good to put it in
host_vars
folder
- good to put it in
- Gather facts: is implicit by default i.e. true
- Quotes: Put
single quotes
insidedouble quotes
."''"
- Escape single quotes that are used as plain texts not to segment
"command='su root -c \'touch /etc/sudoers.d/rc\'' responses=Password=billu"
- Essitially put escape character
\
with evertthing except first"
before command and first'
before sudo. No escape ch when these" and '
close as well.
- Command vs Shell: Careful about characters with
command
module. Some cannot be processed through the shell, so variables like$HOME
and operations like"<", ">", "|", and "&"
will not work.- In such cases use
shell
module - command is safer and preferred over shell
- In such cases use
When adding a new host to inventory. Its a good idea to put all its variables in host_vars/<hostname>.yaml
file
In Ansible, when dealing with variable precedence, the order of precedence from highest to lowest is as follows:
- Variables defined in the playbook: These variables are defined within the playbook itself using
vars
orvars_files
directives. - Variables defined in inventory: Variables defined within the inventory file or in inventory
group_vars
orhost_vars
directories. - Variables defined in roles: Variables defined within roles, either in
defaults/main.yml
,vars/main.yml
, or usingvars_files
. - Variables defined in the playbook directory: Variables defined in
host_vars
takes precedance overgroup_vars
within the playbook directory. - Variables defined in the
ansible.cfg
file: These are global variables set in theansible.cfg
configuration file. - Environment variables: Variables set in the environment where Ansible is executed. These take precedence over variables set in configuration files.
# CHECK Which variables a host is picking up or prioritising
ansible-inventory --host node1
ansible-playbook -l node1 playbooks/motd_set.yaml -e @variables.yaml # Give variables from a file
ansible-playbook -l node1 playbooks/motd_set.yaml -e "motd='FROM CLI : Savvy!'" # Give variable value in CLI
ansible-playbook -l node1 playbooks/motd_set.yaml -e @variables.yaml -e "welcome_message='FROM CLI - Defined'" # Later will be used
# --connection=local so it does not try to ssh into the mac
ansible-playbook -l localhost playbooks/helloworld.yaml -c local
ansible-playbook -l localhost playbooks/mac.yaml --connection=local
# To run as root
ansible-playbook -l localhost playbooks/mac.yaml --connection=local --ask-become-pass
# list
ansible-inventory --inventory inventory --list # All details in yaml format
ansible-inventory --list
ansible-inventory --graph # All hosts and groups
ansible-inventory --graph --vars # What variables is each host picking up
# Get info on host
ansible-inventory --host node1
# You can run anisble playbooks on any host as long as you can SSH into that machine
# Install sshpass on mac
brew install sshpass
# View current config
ansible-config view
#ansible.cfg preference order
1. ANSIBLE_CONFIG (environment variable if set)
2. ansible.cfg (in the current directory) -> `our method`
3. ~/.ansible.cfg (in the home directory)
4. /etc/ansible/ansible.cfg
cat <<EOF > /etc/ansible/ansible.cfg
[defaults]
inventory = ./inventory
remote_user = rc
private_key_file = ./keys/id_rsa
ansible_ssh_common_args = -o ControlMaster=auto -o ControlPersist=60s -o
EOF
# Install Fedora system roles
ansible-galaxy collection install fedora.linux_system_roles
# New file
ansible-vault create passwords.yaml
# if password file is encrypted & you are happy to be prompted
ansible-playbook -l node9 playbooks/ping.yaml -e @secrets/passwords-encrypted.yaml --ask-vault-pass
# if password file is encrypted & DO NOT want to be prompted
ansible-playbook -l node9 playbooks/ping.yaml -e @secrets/passwords-encrypted.yaml --vault-password-file secrets/password-vault
# if password file is NOT encrypted
ansible-playbook -l node9 playbooks/ping.yaml -e @secrets/passwords-plaintext.yaml