Friday, 29 March 2019

Ansible Cheat sheet





Install Ansible 


# yum install ansible


Host file configuration 


  • File 

[ansible@kuber2 ~]$ cat /etc/ansible/hosts
    [local]
    localhost
 
    [allhost]
    mfs091.tuxhub.com
    mfs092.tuxhub.com
    mfs093.tuxhub.com

Note:-

  • Host file we config by ansible.cfg


#inventory      = /etc/ansible/hosts


  • Command to  the list of hosts  
  • [ansible@kuber2 ~]$ ansible all  --list-hosts
      hosts (3):    mfs091.tuxhub.com    mfs092.tuxhub.com    mfs093.tuxhub.com[ansible@kuber2 ~]$ ansible all -i /tmp/hosts  --list-hosts  hosts (1):    mfs023.tuxhub.com
    [ansible@kuber2 ~]$ 

Help in ansible


[ansible@kuber2 ~]$ ansible-doc -l



[ansible@kuber2 ~]$ ansible-doc  atomic_host


Create a custom host file 

[ansible@kuber2 ~]$ cat /tmp/hosts
[customhosts]
mfs023.tuxhub.com

[ansible@kuber2 ~]$ 




[ansible@kuber2 ~]$ ansible all  -i /tmp/hosts -m ping 


mfs023.tuxhub.com | SUCCESS => {    "changed": false,     "ping": "pong"}





Override Ansible config :


  • Seqeunce of file ansible.cfg to be read 
  1.   Varibale in env :

[ansible@kuber2 ~]$ export ANSIBLE_CONFIG=/home/ansible/config/ansible.cfg



   2. Current dir from where anible command excuted
   3. home direcotry of user ( /home/user/.anisble.cfg)
   4. /etc/ansible/ansible.cfg



Ansible commandline :

Using Module 

1) ping 



[ansible@kuber2 ~]$ ansible all -m ping
mfs092.tuxhub.com | SUCCESS => {
"changed": false,
"ping": "pong"
}
mfs091.tuxhub.com | SUCCESS => {
"changed": false,
"ping": "pong"
}
mfs093.tuxhub.com | SUCCESS => {
"changed": false,
"ping": "pong"
}
localhost | SUCCESS => {
"changed": false,
"ping": "pong"
}


2) Shell    ( ansible <HOST> -m <module > -a <argument > 'COMMAND'


[ansible@kuber2 ~]$ ansible mfs091.tuxhub.com  -m shell -a  'yum list all | grep python'


Ansible System  Facts :


[ansible@kuber2 ~]$ ansible mfs091.tuxhub.com -m setup 



[ansible@kuber2 facts]$ ansible mfs091.tuxhub.com -m setup --tree /tmp/facts



[ansible@kuber2 facts]$ ansible mfs091.tuxhub.com -m  setup -a 'filter=*ipv*'




[ansible@kuber2 ~]$ ansible mfs091.tuxhub.com -m setup -a 'filter=ansible*'




Playbooks :


[ansible@kuber2 plyabook2]$ cat lynx.yml 
---
- hosts : all
  tasks :
        - name : Package Installtion
          yum : pkg=lynx state=installed update_cache=true


[ansible@kuber2 plyabook2]$


Playbook Varibales :


  1. Direct Vairables 

[ansible@kuber2 plyabook2]$ cat lynx.yml 
---
- hosts : all
  vars:
        cldbnodes : mfs091.tuxhub.com
  tasks :
        - name : Package Installtion on {{cldbnodes}}
          yum : pkg=lynx state=installed update_cache=true


[ansible@kuber2 plyabook2]$


Output


TASK [Package Installtion on mfs091.tuxhub.com] 



 2.  Varibale via files 


[ansible@kuber2 plyabook2]$ cat lynx.yml vars.yml 
---
- hosts : all
  vars_files:
   - vars.yml
  tasks :
        - name : Package Installtion on {{cldbnodes}}
          yum : pkg=lynx state=installed update_cache=true

Var.yml
---
cldbnodes: mfs091.tuxhub.com 


[ansible@kuber2 plyabook2]$ 


3. Run time varibale 

[ansible@kuber2 plyabook2]$ cat runtime.yml 
---

- hosts : all
  user : ansible      # run with ansible user  
  become : yes        # using sudo 
  connection :  ssh   # over ssh 
  gather_facts : no   # do not gather facts
  vars_files:
   - vars.yml
  vars_prompt : 
   - name : pkgtoinstall
     prompt : Install packges 

     private : no


Ansible Target Section :



[ansible@kuber2 plyabook2]$ cat target.yml 
---

- hosts : all
  user : ansible      # run with ansible user  
  become : yes        # using sudo 
  connection :  ssh   # over ssh 
  gather_facts : no   # do not gather facts

[ansible@kuber2 plyabook2]$ 


Ansible Task Section :

[ansible@kuber2 plyabook2]$ cat action.yml 
---

- hosts : all
  user : ansible      # run with ansible user  
  become : yes        # using sudo 
  connection :  ssh   # over ssh 
  gather_facts : no   # do not gather facts
  vars_files:
   - vars.yml
  tasks :
   - name  : Check the lynx install
     action : yum name=lynx state=installed 
        

[ansible@kuber2 plyabook2]$  


Ansible Notify Handler Section :

[ansible@kuber2 plyabook2]$ cat handler.yml
---

- hosts : all
  user : ansible      # run with ansible user  
  become : yes        # using sudo 
  connection :  ssh   # over ssh 
  gather_facts : no   # do not gather facts
  vars_files:
   - vars.yml
  tasks :
   - name  : Install and handler nginx 
     action : yum name=nginx state=installed
     notify : restart nginx
  handlers:
   - name : restart nginx
     action : service name=nginx state=restarted
        

[ansible@kuber2 plyabook2]$


Ansible Register Section :

[ansible@kuber2 outline]$ cat resister.yml 
--- # Outline to playbook translation 
- hosts : all 
  gather_facts : no
  become : true
  become_user: root
  tasks : 
   - name : date/time when playbok starts 
     command : /usr/bin/date
     register : timestamp_start

   - debug : var=timestamp_start


Ansible Dry run section Section :



[ansible@kuber2 plyabook2]$ ansible-playbook playbook.yml --check


Ansible Async polling Section :


[ansible@kuber2 plyabook2]$ grep fork /etc/ansible/ansible.cfg
#forks          = 5

[ansible@kuber2 plyabook2]


The default anisble run 5 nodes at a time but if we need more than 5 mode we need to use async method

[ansible@kuber2 plyabook2]$ cat lynx.yml 
---
- hosts : all
  become : true
  vars_files:
   - vars.yml
  tasks :
        - name : Package Installtion on {{cldbnodes}}
          yum : pkg=lynx state=installed update_cache=true
          async : 300  # wait for 300 second for sucess
          poll : 3     # poll output every 3 second


[ansible@kuber2 plyabook2]$ 


Ansible Varibale Subsitution Section :

[ansible@kuber2 plyabook2]$ cat lynx.yml 
---
- hosts : all
  become : true
  vars_files:
   - vars.yml
  tasks :

        - name : Package Installtion on {{cldbnodes}}



[ansible@kuber2 plyabook2]$ cat vars.yml 
---
cldbnodes: mfs091.tuxhub.com 


[ansible@kuber2 plyabook2]$


[ansible@kuber2 plyabook2]$ cat pkg_installtion.yml 
---
- hosts : all
  become : true
  vars_prompt :
   - name : Installpackege
     prompt : Install packege to be installed 
     private : no
  tasks :
   - name : install {{Installpackege}}
     action : yum name={{Installpackege}} state=installed
     

[ansible@kuber2 plyabook2]$


Ansible Lookup Section :

Loopup is Inbuild functions


[ansible@kuber2 plyabook2]$ cat loopkup.yml 
---
- hosts : all
  become : true
  gather_facts : no
  tasks : 
   - debug : 
      msg: "{{ lookup('env','HOME') }} is the value" 

[ansible@kuber2 plyabook2]$ 


Ansible runonce Section :

It runs only on one system even hosts is all


[ansible@kuber2 plyabook2]$ cat runonce.yml 
---
- hosts : all
  user : ansible 
  gather_facts : no
  become : true
  tasks :
   - name : Run at one time 
     command : /usr/bin/date
     register : result
   - debug : var=result
     run_once : true 

[ansible@kuber2 plyabook2]$ 


Ansible Local action Section : ( 127 .0.0.1) 


[ansible@kuber2 plyabook2]$ cat localaction.yml 
--- 
- hosts : 127.0.0.1 
  connection : local 
  tasks :
   - name : install telent
     action : yum name=telnet state=installed

[ansible@kuber2 plyabook2]$


Ansible Loop Section :


[ansible@kuber2 plyabook2]$ cat loop.yml 
---
- hosts : all
  become : true
  gather_facts : no
  tasks : 
   - name : install via loop 
     action : yum name={{item}} state=installed
     with_items:
      - lynx
      - nginx

[ansible@kuber2 plyabook2]$




Ansible Conditional  Section :




[ansible@kuber2 plyabook2]$ cat conditional.yml
---
- hosts : all
  become: true
  tasks:
   - name : install via ngnix conditional 
     action : yum name=ngnix state=installed
     when : ansible_os_family == "centos" 

[ansible@kuber2 plyabook2]$



Ansible Until Section :



[ansible@kuber2 plyabook2]$ cat until.yml 
--- 
- hosts : all
  become : true
  gather_facts : no 
  tasks :
   - name : test until 
     action : yum name=httpd state=installed 
   - name : verify status 
     shell : systemctl status httpd
     register : result
     until : result.stdout.find("Active (running )") != -1 
     retry : 5 
     delay : 5 
   - debug : var=result
     

[ansible@kuber2 plyabook2]$ 


Ansible valut ( passoword ) 


[ansible@kuber2 plyabook2]$ ansible-vault create secure.yml
New Vault password: 

Confirm New Vault password: 


[ansible@kuber2 plyabook2]$ cat secure.yml 
$ANSIBLE_VAULT;1.1;AES256
35383232613735396438633236613266623432346462333063393061626135396164343830336430
3733663435393065656139613839313832326634666636620a626130353037396631393539373539
66613631393362346538663530633637326439643333623362643766333665373763366531356230
3330333864616530620a613831346637366639663365326562343962646562663532313065366231
6131

[ansible@kuber2 plyabook2]$


[ansible@kuber2 plyabook2]$ ansible-vault view secure.yml
Vault password: 
test1=password

[ansible@kuber2 plyabook2]$ 



Ansible  Include



--- # full include task 
- hosts : webbox
  become : true
  connection : ssh
  gather_facts: no
  tasks :

    - include : plays/pkg.yml




[ansible@kuber2 playbooks]$ cat plays/pkg.yml 
--- # install telnet 

- name : install telent 
  yum : pkg=telnet state=installed
- name : install lynx
  yum :  pkg=lynx state=installed 

[ansible@kuber2 playbooks]$ 



Ansible  Tags 

Just to run verification

[ansible@kuber2 playbooks]$ cat tag.yml 
--- # Tag functionallty yml 
- hosts : webbox 
  become : true 
  gather_facts : no 
  connection: ssh 
  tasks : 
   - name : installe telent and lynx 
     yum : pkg={{item}} state=latest
     with_items : 
        - telnet
        - lynx
     tags : 
        - packages 
   - name : verify telent install
     command : yum list insalled | grep telent 
     tags : 
        - verification
[ansible@kuber2 playbooks]$ 



Execute only tags


[ansible@kuber2 playbooks]$ ansible-playbook tag.yml --tags "verification"

Skip the tags


[ansible@kuber2 playbooks]$ ansible-playbook tag.yml --skip-tags "packages"


Always run the verification ignore only if it skips.

[ansible@kuber2 plyabook2]$ cat tag.yml 
--- # Tag functionallty yml 
- hosts : all
  become : true 
  gather_facts : no 
  connection: ssh 
  tasks : 
   - name : installe telent and lynx 
     yum : pkg={{item}} state=latest
     with_items : 
        - telnet
        - lynx
     tags : 
        - packages 
   - name : verify telent install
     command : yum list insalled | grep telent 
     tags : 

        - always




Ansible  ERROR handle: 

If  ignore_Error : yes then even if first task fails it will continue 



[ansible@kuber2 plyabook2]$ cat errorhandle.yml 
---
- hosts : all
  become : true 
  gather_facts : no 
  tasks : 
   - name : fail command 
     command : /bin/false
     ignore_errors : yes
   - name : Install telent 
     action : yum name=telnet state=installed
   

[ansible@kuber2 plyabook2]$


Ansible  startat / step  :

It will ask to perform or not :


[root@kuber2 plyabook2]# cat startat.yml 
--- # startat example playbook 
- hosts : all
  become : true 
  gather_facts : no 
  connection : ssh
  tasks : 
    - name : install telnet 
      yum : pkg=telnet state=latest 
    - name : install lynx 
      yum : pkg=lynx state=latest 
    - name : list dir 
      shell : ls -l /var

[root@kuber2 plyabook2]# 

Ansible command line  :



[root@kuber2 plyabook2]# cat fromcmdline.yml 
---
- hosts : '{{hosts}}'
  user : '{{user}}'
  become : true
  gather_facts : no
  tasks : 
   - name : Install telent client 
     action : yum  name={{pkg}} state=latest
      

[root@kuber2 plyabook2]# 



[root@kuber2 plyabook2]# ansible-playbook fromcmdline.yml  --extra-vars "hosts=all user=ansible pkg=telnet"




1 comment:

Ansible Cheat sheet

Install Ansible  # yum install ansible Host file configuration  File  [ansible@kuber2 ~]$ cat /etc/ansible/hosts     [loca...