4-Ansible For Remote Clients
4-Ansible For Remote Clients
IP Range 205.168.20.[11:14]
[servers]
server1 ansible_ssh_host=10.91.50.110 Aliases
server2 ansible_ssh_host=10.91.50.111
server3 ansible_ssh_host=10.91.50.112
server4 ansible_ssh_host=10.91.50.113
server5 ansible_ssh_host=10.91.50.114
server6 ansible_ssh_host=10.91.50.115
[appserver]
server1
server2
[webserver]
server3
server4
[dbservers]
server5
server6
• Inventory host file can either be static or dynamic (using additional plug-ins)
• Generate SSH Keys on the control node and copy over to clients for password less
SSH connections
# ssh-keygen
# Leave everything default and enter
# ssh-copy-id 10.253.1.18
# ssh-copy-id 10.253.1.20
• Run Ansible add-hoc to ping remote nodes (make sure hosts file has remote clients IPs)
# ansible all -m ping
# ansible –a “uptime” all (To run a command on the remote clients) By: Imran Afzal
www.utclisolutions.com
Check Remote Clients Connectivity
# su - root
# cd /etc/ansible/playbooks
# vim clientstatus.yml
---
- name: “Check remote clients connectivity status”
hosts: all
tasks:
- name: Test connectivity
ping:
# anisble-playbook clientstatus.yml
---
- name: Copy file from local to remote clients Description of the playbook
hosts: all Run it on all hosts
tasks: Run the following task(s)
- name: Copying file Description of the task
become: true Transfer as a current user
copy: Run copy module
src: /home/iafzal/some.cfg
dest: /tmp
Source of the file
owner: iafzal
group: iafzal
Destination of the file
mode: 0644
Change ownership and file permissions
# Login to ControlNode
# vim filepermission.yml
---
- name: Change file permissions
hosts: all
tasks:
- name: Files Permissions
file:
path: /home/iafzal/linux2 File location
mode: a+w Permissions
# Login to LinuxCleint1
# rpm –qa | grep http
# systemctl status firewalld
# Login to ControlNode
# cd /home/ansible/playbooks
# vim httpsetup.yml
# vim shellscript.yml
---
- name: Playbook for shell script Description of the playbook
hosts: all or 10.253.1.115 Run on client1
---
- name: Create a cron job
hosts: all
tasks:
- name: Schedule cron:
cron:
name: This job is scheduled by Ansible
minute: “0”
hour: “10”
day: “*” Day of the month
month: “*”
weekday: “4“ Day of the week
user: root
job: “/home/iafzal/cfile.sh”
---
- name: Playbook for creating users Description of the playbook
hosts: all Run on all clients
Please note: Ansible does not allow us to pass a cleartext password through the user module
# vim changepass.yml
---
- name: Add or update user password
hosts: all
tasks:
- name: Change “george” password
user:
name: george
update_password: always
password: "{{ newpassword|password_hash('sha512’) }}”
• To run this playbook, run the command as below. This will input the newpassword variable
that will be used by our playbook
# vim killprocess.yml
---
- name: Find a process and kill it
hosts: 10.253.1.115
tasks:
- name: Get running processes from remote host
ignore_errors: yes
shell: "ps -few | grep top | awk '{print $2}'"
register: running_process
---
- name: httpd and telnet
hosts: all
tasks:
- name: Install httpd
yum:
name: httpd
state: present
• Some Ansible distribution does not come with parted and mount module
tasks:
- name: create new partition
parted:
name: files
label: gpt
device: /dev/sdb
number: 1
state: present
part_start: 1MiB
part_end: 1GiB
- name: Create xfs filesystem
filesystem:
dev: /dev/sdb1
fstype: xfs
- name: Create mount directory
file:
path: /data
state: directory
- name: mount the filesystem
mount:
src: /dev/sdb1
fstype: xfs By: Imran Afzal
state: mounted
www.utclisolutions.com