Tuesday, December 29, 2020

Ansible - Error / exception handling

 Eye exam -- call ->

Error / exception handling
--------------------------

Loop/Dictionary


Command module
---------------

google
ansible command
look for command module

running code on same host - localhost

# ansible localhost --list-hosts

no entry needed on inventory for localhost

# cat mycommand.yaml
- hosts: localhost
  tasks:
  - command: date



- hosts: localhost
  tasks:
  - command: date
    register: x # to print the output, store on variable x

  - debug:
      var: x

# Command module always run


- hosts: localhost
  tasks:
  - command: date
    register: x # to print the output, store on variable x

  - debug:
      var: x
  - debug:
     msg: "Welcome ...."

# Command module always run


[root@master Dec29]# cat cmd.yaml
- hosts: localhost
  tasks:
  - command:  mkdir /tmp/users
    register: x # to print the output, store on variable x

  - debug:
      var: x
  - debug:
     msg: "Welcome ...."

# Command module always run
# command module does not support idompoent

[root@master Dec29]# ansible-playbook cmd.yaml

if you
----------------------------------

- hosts: localhost
  tasks:
  - command:  mkdir /tmp/users
    register: x # to print the output, store on variable x
    ignore_errors: yes  # if you run second time, it will not run

  - debug:
      var: x
  - debug:
     msg: "Welcome ...."

# Command module always run
# command module does not support idompoent


[root@master Dec29]# ansible-playbook cmd.yaml


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

Check the condition

- hosts: localhost
  tasks:
  - command: ls /tmp/users
    register: s
    ignore_errors: yes

  - command:  mkdir /tmp/users
    register: x # to print the output, store on variable x
    ignore_errors: yes  # if you run second time, it will not run

  - debug:
      var: x
  - debug:
     msg: "Welcome ...."

# Command module always run
# command module does not support idompoent
~

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

- hosts: localhost
  tasks:
  - command: ls /tmp/users
    register: s #
    ignore_errors: yes

  - command:  mkdir /tmp/users
    #register: x        # to print the output, store on variable x
    #ignore_errors: yes  # if you run second time, it will not run
    register: x
    when: s.rc != 0  # test if dir exists. Its for idompotence

  - debug:
      var: x
  - debug:
     msg: "Welcome ...."

# Command module always run
# command module does not support idompoent
~


---------------------
- hosts: localhost
  tasks:
  - command: ls /tmp/users
    register: s #
    ignore_errors: yes

  - command:  mkdir /tmp/users
    #register: x        # to print the output, store on variable x
    #ignore_errors: yes  # if you run second time, it will not run
    register: x
    when: s.rc != 0  # test if dir exists. Its for idompotence

  - debug:
      var: x

  - debug:
     msg: "Welcome ...."

# Command module always run
# command module does not support idompoent


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



Role - people create and upload to public repo (Ansible-Galaxy)

google for ansible galaxy and browse through

They are categories based on os, network, cloud ..... and more ..

you can also upload your playbook.

[root@master Dec29]# cat /etc/passwd | wc -l

| is a facility offered y shell


To run this command, you have to use shell module instead of command module


- hosts: localhost
  tasks:
#  - command: cat /etc/passwd | wc -l
# | will throw error. You can't just use pipe. Its a facility.
# use shell module
  - shell: cat /etc/passwd | wc -l



How many ansible galaxy we have
[root@master Dec29]# ansible-galaxy list
# /root/wk20-Roles
- myapache, (unknown version)



ansible-galazy role -h
ansible-galazy role search apache



go to ansible-galaxy site and search for apache


# ansible-version

we are on latest version as of dec 29, 2020

[root@master roles]# ansible-galaxy role install geerlingguy.apache
- downloading role 'apache', owned by geerlingguy
- downloading role from https://github.com/geerlingguy/ansible-role-apache/archive/3.1.4.tar.gz
- extracting geerlingguy.apache to /root/wk20-Roles/geerlingguy.apache
- geerlingguy.apache (3.1.4) was installed successfully
[root@master roles]# ls
[root@master roles]# pwd
/etc/ansible/roles
[root@master roles]# ansible-galaxy list
# /root/wk20-Roles
- myapache, (unknown version)
- geerlingguy.apache, 3.1.4
[root@master roles]#

[root@master roles]# cd /root/wk20-Roles
[root@master wk20-Roles]# ls



1:09



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. ...