Thursday, December 10, 2020

Ansible - Install software - capture output - debug

 Ansible - Notes
----------------------------

what we want ?

Step1. Install software

RAL: Module
package : specify name of the software-package. say httpd for RHEL
what is you have some other OS, say ubuntu, software for httpd is apache2.
 
RHEL7 -> facts -> yum ..
ubuntu -> apt get

since package name is different of different OS, ansible can't help you on this situation.

How to install the software -ansible takes care of it.

You have to tell what you want. This is going to be part of playbook.


package
- httpd -> install this package if os=rehl
- apache2 -> install this software only if os=ubuntu

in ansible, we don't use if else or case.

we write condition using when...

Condition is like -
install httpd when os is redhat


how they find os name? - using facts -> facts gather all info about the system and stored under ansible_facts.

You can store os name on osname variable.


Note: You need to know the manual steps before you can autimate the tasks.

Lets look at the example.


- hosts: myweb
  tasks:
  - package:
      name: "httpd"

if you run this playbook, it will fails


- hosts: myweb
  tasks:
  - package:
      name: "apache2"

it works on ubuntu

--------------------


$ cat myweb.yaml
- hosts: myweb
  tasks:
  - package:
      name: "httpd"


- hosts: myweb
  tasks:
  - package:
      name: "apache2"

You get all variables by running
# ansible 192.168.10.55 -m setup | less

search for RedHat
you will seee ansible_distribution



$ cat myweb.yaml
- hosts: myweb
  vars:
  - x: "redhat"
  tasks:
  - package:
      name: "httpd"
    when: x == "redhat"

see the variable x has a redhat value, if condition meets it runs

# ap myweb.yaml

and it is successful

lets say, its not a good practice to hardcode the OS name, so lets try this way,

$ cat myweb1.yaml
- hosts: myweb
  vars:
  - os_name: ansible_facrs[ansible_distribution]
  tasks:
  - package:
      name: "httpd"
    when: os_name == "redhat"

[root@master wk10]# ansible-playbook myweb1.yaml


you see it skips. see the case? facts is uppercase


[root@master wk10]# cat myweb1.yaml

- hosts: myweb
  vars:
  - os_name: ansible_facrs[ansible_distribution]
  tasks:
  - package:
      name: "httpd"
    when: os_name == "RedHat"


[root@master wk10]# ansible-playbook myweb1.yaml


it still skip

------------------------------------------
lets debug what happening


[root@master wk10]# more myweb1.yaml

- hosts: myweb
  vars:
#  - os_name: ansible_facrs[ansible_distribution]
  - x: "John"
  tasks:
  - package:
      name: "httpd"
    when: os_name == "RedHat"


  - debug:
#      var: ansible_facts[ansible_distribution]
#      var: os_name
      var: x

# String intropolation or something
  - debug"
      msg: " Hey {{ x }}
#      msg: "Hi Hello {{ os_name }}
[root@master wk10]#

------------------------------------------

Lets re-write it again...
- hosts: myweb
  vars:
  - os_name: "{{ ansible_facts['distribution'] }}"
  - x: "John"
  tasks:
  - package:
      name: "httpd"
    when: os_name == "RedHat"


  - package:
      name: "apache2"
    when: os_name == "Debian"

  - debug:
      var: x

[root@master wk10]# ansible-playbook myweb.yaml

Finally it is successful...



ad-hoc command

ansible 192.168.10.20 -m command -a date
[root@master wk10]# ansible worker1 -m command -a date
worker1 | CHANGED | rc=0 >>
Thu Dec 10 11:27:05 EST 2020


$ cat anc.yaml
- hosts: worker1
  tasks:
  -  command: date

  - debug:
      msg: "hi test !!!"

    

when you run the playbook, they hide the output.

You can use -v option to see the output..

[root@master wk10]# cat abc.yaml
- hosts: worker1
  tasks:
  - command: date

  - debug:
      msg: "hi test !!!"
[root@master wk10]# ansible-playbook -v abc.yaml

review output
changed -> true -> made change...

- v shows the output in detail...
[root@master wk10]# ansible-playbook -v myweb.yaml


debug module, only run when changed is false ...

how do we do this?

When you run the playbook, by default they hide the output. so use -v to see the out put of the command module..

store all the output to register variable x
x contains entire output of task output.


[root@master wk10]# cat  abc.yaml
- hosts: worker1
  tasks:
  - command: date
    register: x

  - debug:
      msg: "hi test !!!"

  - debug:
      var: x


This time, entire output is display. because we say to print debug module to store the output to variable x and print it.
[root@master wk10]# ansible-playbook abc.yaml



if you review the output x has all values in array like format

to print particular value
you have to do like
x.rc



- hosts: worker1
  tasks:
  - command: date
    register: x

  - debug:
      msg: "hi test !!!"

  - debug:
      var: x.rc

[root@master wk10]# ansible-playbook abc.yaml

ok: [worker1] => {
    "x.rc": "0"


----------------
[root@master wk10]# cat abc.yaml
- hosts: worker1
  tasks:
  - command: date
    register: x

  - debug:
      msg: "hi test !!!"
# run only if rc=0
    when: x.rc == 0

  - debug:
      var: x.rc


change and run it again
[root@master wk10]# ansible-playbook abc.yaml



Use with not

- hosts: worker1
  tasks:
  - command: date
    register: x

  - debug:
      msg: "hi test !!!"
# run only if rc=0
    when: x.rc != 0

  - debug:
      var: x.rc


[root@master wk10]# ansible-playbook abc.yaml

lets modify the yaml file

[root@master wk10]# cat abc.yaml
- hosts: worker1
  tasks:
  - command: date
    register: x

  - service:
      name: "httpd"
      state: "started"
    register: y
  - debug:
      msg: "hi test !!!"
# run only if rc=0
    when: x.rc != 0

  - debug:
      #var: x.rc
      var: y


and run it now,

[root@master wk10]# ansible-playbook abc.yaml

check failed: false

you can write condition here as well.. if this successful, do next ...

if package is not installed, do not start service - does not make sense...


start the service only if software is running.

if this successful, write the firewall rule...

ask debug module to

  - debug"
      msg: "final message"
    when: x.rc == 0 && y.failed == false
# when both conditions are true, then only run the next tasks.


[root@master wk10]# cat abc.yaml
- hosts: worker1
  tasks:
  - command: date
    register: x

  - service:
      name: "httpd"
      state: "started"
    register: y

  - debug:
      msg: "hi test !!!"
# run only if rc=0
    when: x.rc != 0

  - debug:
      #var: x.rc
      var: y

  - debug:
      msg: "final message"
    when: x.rc == 0 and y.failed == false
[root@master wk10]#

[root@master wk10]# ansible-playbook abc.yaml


[root@master wk10]# cat ../myhosts

[mylb]
master  ansible_user=root ansible_ssh_pass=changeme ansible_connection=ssh

[myweb]
worker1 ansible_user=root ansible_ssh_pass=changeme ansible_connection=ssh
worker2 ansible_user=root ansible_ssh_pass=changeme ansible_connection=ssh

[nfs_server]
master  ansible_user=root ansible_ssh_pass=changeme ansible_connection=ssh

[nfs_clients]
worker1 ansible_user=root ansible_ssh_pass=changeme ansible_connection=ssh


Write code in such a way that it runs..
think what if you have diff os,
what to do with new IP added, how to configure...


========================================


aws -> facts -> output
public_ip came none...


- store the output to one variable and print the public ip..





No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...