Skip to content

Instantly share code, notes, and snippets.

@tuxfight3r
Last active April 21, 2022 00:36
Show Gist options
  • Save tuxfight3r/38b41e7a3bb549552cbc to your computer and use it in GitHub Desktop.
Save tuxfight3r/38b41e7a3bb549552cbc to your computer and use it in GitHub Desktop.
ansible basics
#Install Ansible from EPEL
yum install ansible ansible-lint
#Update config file for ansible
cat >> /etc/ansible/hosts <<EOF
192.168.0.1
[web_servers]
192.168.0.20
[app_servers]
192.168.0.30
192.168.0.40
EOF
#ping all the hosts prompting password
ansible -m ping -u root all --ask-pass
#ping only the web_server prompting password
ansible -m ping -u root web_servers --ask-pass
#copy local ssh pub key to remote hosts prompting password
ansible all -m copy -a "src=/root/.ssh/id_rsa.pub dest=/root/.ssh/authorized_keys owner=root group=root mode=0600" -u root --ask-pass
#Delete a file from remote servers
ansible all -m shell -a "rm -v /root/.ssh/authorized_keys " -u root
#run a system command
ansible app_servers -m shell -a "rpm -qa | grep httpd"
ansible all -m shell -a "id;whoami"
#update a package using yum
ansible app_servers -m yum -a "name=wget state=latest" -u root
#check a service state
ansible app_servers -m service -a "name=httpd state=started" -u root
ansible app_servers -m service -a "name=httpd state=stopped" -u root
#manage authorized keys
ansible all -m authorized_key -a "user=root key='{{lookup('file', '/root/.ssh/id_rsa.pub')}}' path=/root/.ssh/authorized_keys manage_dir=no"
#print ansible facter
ansible app_servers -m facter
#Run a ansible playbook
ansible-playbook app_server.yaml -f 10
cat >> app_server.yaml <<EOF
---
- hosts: app_servers
remote_user: root
tasks:
- yum: name={{item}} state=latest
with_items:
- httpd
- mod_ssl
- nc
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment