Setup Hosts File
Question. Create an Ansible file called
/root/hosts
with the hosts in your network.servers:
controlplane
node01
Solution.
You may use ini or yaml format for ansible hosts files
Add this to file
/root/hosts
ini
[servers] controlplane node01
Check functionality and connectivity
Question. Verify connectivity to all hosts with ping module and save the output to
/root/ping_check
.Solution:
Executing ping module against both servers
ansible servers -i /root/hosts -m ping
Executing ping module against both servers and output to
/root/ping_check
ansible servers -i /root/hosts -m ping > /root/ping_check
Add inventory for future environments
Question. Your team is going to be adding new servers to the environment that haven't been built yet. Update your inventory file at
/root/hosts
to reflect the new environments.Update the /root/hosts file
Use ansible-inventory commands to view the file values.
Solution:
Update the /root/hosts file to have a test and prod environment.
vi /root/hosts
and thenesc
+: wq
to save.[servers] controlplane node01 [test_env] controltest type=client node01test type=server [prod_env] controlprod type=client node01prod type=server [non-prod:children] servers test_env
Now that you've updated the inventory for the new systems, view the inventory with ansible-inventory to see the host groups.
ansible-inventory -i /root/hosts --list
To see that in graph output use this command.
ansible-inventory -i /root/hosts --graph
To see that in yaml output.
ansible-inventory -i /root/hosts --list -y
Ad Hoc to gather data
Question. Verify your hosts file and test ad hoc commands to your servers
Verify servers uptime
Verify OS type of servers
Verify date and time of servers
Hint:
Create a file
/root/version
with the Ansible Facts for the distribution of each server.Create a file
/root/date
with the Ansible Facts for the current date of each server.
Solution:
cat /root/hosts
Check server uptime
ansible servers -i /root/hosts -m shell -a 'uptime'
Setup module gives so much information you can use during playbook execution.
ansible servers -i /root/hosts -m setup
Cut that output down a bit so you can just check the host distribution information
ansible servers -i /root/hosts -m setup -a 'filter=ansible_distribution'
Send this output to the required file
ansible servers -i /root/hosts -m setup -a 'filter=ansible_distribution' > /root/version
Cut that output down a bit so you can just check the host time information
ansible servers -i /root/hosts -m setup -a 'filter=ansible_date_time'
Send this output to the required file
ansible servers -i /root/hosts -m setup -a 'filter=ansible_date_time' > /root/date
Note:
-i = Inventory
-m = Module
-a = Module Argument
Ad Hoc to copy and edit
Question. Verify your
/root/hosts
file and/root/configfile.cfg
Create a directory on your servers for deployments named
/opt/deployment
Push over your config file
Change the values of var1 to equal 111111 only in the config file in
/opt/deployment
Solution:
cat /root/hosts cat /root/configfile.cfg
Create a Directory on each server named
/opt/deployment
ansible servers -i /root/hosts -m file -a 'path=/opt/deployment state=directory'
Copy over your
/root/configfile.cfg
to that directoryansible servers -i /root/hosts -m copy -a 'src=/root/configfile.cfg dest=/opt/deployment'
Let's fix a bad configuration line from 000000 to 111111 with the lineinfile module
ansible servers -i /root/hosts -m lineinfile -a "path=/opt/deployment/configfile.cfg regexp='^var1' line='var1=111111'"
Quick verification that it looks good on all servers
ansible servers -i /root/hosts -m shell -a 'cat /opt/deployment/configfile.cfg'
Create a playbook to stage an application.
Question. Verify your host's file
Create a deploy playbook called
/root/deploy.yml
to push the file/root/deploy.tar.gz
over to all servers in the/opt
directory.Solution:
cat /root/hosts vim /root/deploy.yml
Yaml for playbook
--- - name: Start of Deployer playbook hosts: servers vars: gather_facts: True become: False tasks: - name: Copy deploy.tar.gz over at {{ ansible_date_time.iso8601_basic_short }} copy: src: /root/deploy.tar.gz dest: /opt/deploy.tar.gz checksum: c6cd21b75a4b300b9228498c78afc6e7a831839e
Get sha1 checksum of the archive.
sha1sum /root/deploy.tar.gz
Run Playbook and verify that everything pushed correctly
ansible-playbook -i /root/hosts /root/deploy.yml
Manual verify for all
ansible servers -i /root/hosts -m shell -a 'ls -l /opt/deploy.tar.gz'