This article is basically a guide on using
Ansible.
1. Obtain
a Environment Variable – To retrieve a Environment variable we can use
- name: Copy JAVA_HOME location to variable
command: bash -c -l "echo
$JAVA_HOME"
register: java_loc
- name: show debug
debug: var=java_loc
2. Retrieve
executables – In order to retrieve executables available in remote machine.
Ex- to get where bash resides we can use
-
name: Where is Bash
command: bash -c -l "which bash"
register: whereis_bash
-
name: show Bash
debug: var=whereis_bash
3. Wait
for Port – When ever we run a script and wait for particular port to start
we can use
- name: Wait for the nexus-iq Port to start
wait_for: port=8070 delay=60
4. Template
Variables – When ever we want to pass variables to template we can use
-
name: copy nexus-iq.sh start script
template:
src: nexus-iq.j2
dest: "{{nexus_iq_dir}}/nexus-iq.sh"
mode: "0755"
vars:
java_path: "{{java_loc.stdout}}"
nexus_iq_path: "{{nexus_deploy_location}}"
bash_is: "{{whereis_bash.stdout}}"
5. Update
environment variables – We use source command to update environment
variables .this can be done in Ansible as
- name: Source Bashrc to Update Env
Variables
shell: source {{ installation_user_home }}/.bashrc
6. Replace
– There are some cases where we might need to replace things in the obtained
values from remote hosts. We can do that in anisble as,
- name: Copy the jetty-https Template to
the Remote Machine
template:
src: jetty-https.j2
dest:
"{{nexus_dir}}/nexus-{{nexus_version}}/conf/jetty-https.xml"
mode: "0644"
backup: "yes"
vars:
OBF: "{{ generated_password.stdout | replace('\n', '') |
replace('^M', '') }}"
7. Pipelining
- pipelining reduces the number of ssh connections required to execute a module
on the remote server, by piping scripts to the ssh session instead of copying
it.
This results in the performance
improvement.
Note - pipe ling will only work if the
option requiretty is disabled on all remote machines in the sudoers file
[ssh_connection]
pipelining = True
8. Turn
off Facts -if you are not using any facts in your playbook ,we can disable
that using the
- hosts: dev
gather_facts: False
9. Step
by Step Task - an ansible playbook will run all the tasks in a sequential
way. What if we want to check before running the task. --step in Ansible will
let you decide which tasks you want to run
ansible-playbook sample.yml –step
10. Dry
Run – Some time we want our Ansible scripts to run but with out making any
changes. This is something like testing our Scripts. We can do this as
ansible-playbook playbook.yml –check
11. Pause
a Playbook – In Some cases we want out playbook to pause until some other
action on the remote machine is done. We can do this by adding,
pause: prompt="waiting 60
Seconds" minutes=1 seconds=30
12.
List Tasks – There may be cases where we want to check the tasks available
before running them. This can done by
[root@vx111a delegate]# ansible-playbook
--list-tasks sample-playbook.yml
playbook: sample-playbook.yml
play #1 (cent): TAGS: []
Install zenity TAGS: []
configure zenity TAGS: []
Tell Master TAGS: []
13. Syntax
Checking – In order to check the syntax of the playbook we can use
[root@vx111a delegate]# ansible-playbook
--syntax-check sample-playbook.yml
14. Verbose
Mode – Run Ansible playbook in verbose mode as,
[root@vx111a delegate]# ansible-playbook
--verbose sample-playbook.yml
15. Delegate
– There are some cases where we want our actions to be delegated to other
machines. To “delegate” tasks to the control machine, or any other host that
the control machine can SSH to we can do
-
name: Tell Master
action: shell echo "{{ansible_fqdn}} done" >> /tmp/list
delegate_to: 172.16.202.97
In the above snippet we are delegating our
task to 172.16.202.97 Machine
16. Send
Mail – There are cases where we need to send mail based on Our actions. We
can do this in Ansible using
-
name: Send Mail Alert
local_action: mail
host="127.0.0.1"
subject="[Ansible]
Testing Mail"
body="Hello the Play
book ran successfully"
to="XXX@gmail.com"
from="root"
More to Come. I will be adding more of the
use cases in Ansible.
Happy learning J
No comments :
Post a Comment