Class notes:
ansible-class-notes-Dec-11-2020
PB
- set up
- docker install
- image centos
- os continer
- config web server
CN -> ssh (IP) enable
Docker, -> OS -> Continer
Run time environment.
Docker
PODMAN
CRI-O
kubernetes
DevOps
- ansible
===============================
# cat web.yaml
- hosts: w1
tasks:
- command: "date"
- package:
name: "httpd"
- service:
name: "httpd"
state: "started"
# ap.web.yaml
successful, but if type miss-type a work, what happens? all your task fails..
control the error which is called error handling...
what we want to do it, if somehing fails, continue. lets try with ignore_errors key word
[root@master wk-11]# vi web.yaml
- hosts: w1
tasks:
- command: "dates"
ignore_errors: yes
- package:
name: "httpd"
- service:
name: "httpd"
state: "started"
[root@master wk-11]# ansible-playbook web.yaml
TASK [command] *****************************************************************************************
fatal: [w1]: FAILED! => {"changed": false, "cmd": "dates", "msg": "[Errno 2] No such file or directory: b'dates': b'dates'", "rc": 2}
...ignoring
know what you are doing. can you ignore the error?
- hosts: w1
tasks:
- command: "dates"
ignore_errors: yes
- package:
name: "httpd"
- service:
name: "httpd"
state: "started"
# cat kb.conf.j2
Listen 8080
# ap web.yaml
# ap -v web.yaml
always checks changed features... idompotence
- we see, even there is no change, its keep running.
- and also service is keep restarting...
notify
we tell template or copy module, notify if there is any change...
it will do remaining handling....
we can create handler
its like
[root@master wk-11]# cat web.yaml
- hosts: w1
tasks:
- command: "dates"
ignore_errors: yes
- package:
name: "httpd"
- copy:
dest: "/var/www/html/index.html"
content: "Hello hi"
- template:
dest: /etc/httpd/conf.d/kb.conf
src: "kb.conf.j2"
notify: web services
handlers:
- name: web services
service:
name: "httpd"
state: "restarted"
- debug:
msg: "test"
- service:
name: "haproxy"
[root@master wk-11]# cat kb.conf.j2
Listen 8080
[root@master wk-11]#
# ap web.yaml
Nothing is changed.
Pls disable firewall and selinux and change
lets change the port and run it again.
[root@master wk-11]# cat kb.conf.j2
Listen 85
[root@master wk-11]# ansible-playbook web.yaml
review the output.
use when or lets use variable...
- hosts: w1
vars:
- os_name: ansible_facts[ 'distribution' ]
tasks:
- command: "dates"
ignore_errors: yes
- package:
name: "httpd"
- copy:
dest: "/var/www/html/index.html"
content: "Hello hi"
- template:
dest: /etc/httpd/conf.d/kb.conf
src: "kb.conf.j2"
notify: web services
handlers:
- name: web services
service:
name: "httpd"
state: "restarted"
- debug:
msg: "test"
- service:
name: "haproxy"
# you can use register if this task changed, run that ...
lets say you have different pkg name
- hosts: w1
vars:
- os_name: ansible_facts[ 'distribution' ]
- p1: "httpd"
- p2: "apache2"
tasks:
- command: "dates"
ignore_errors: yes
- package:
name: p1
when: os_name == "RedHat" and os_var == 7
- copy:
dest: "/var/www/html/index.html"
content: "Hello hi"
- template:
dest: /etc/httpd/conf.d/kb.conf
src: "kb.conf.j2"
notify: web services
handlers:
- name: web services
service:
name: "httpd"
state: "restarted"
- debug:
msg: "test"
- service:
name: "haproxy"
# you can use register if this task changed, run that ...
can you write code in such that it finds ip , os version and software that you want to install.. ??
lets create extra files
OS specific variable files
# vi RedHat-7.yml
-p: "httpd"
# vi RedHat-6.yml
-p: "httpd"
# vi Ubuntu-14.04.yml
-p: "apache2"
- hosts: w1
vars_files:
- "RedHat-6.yaml"
vars:
- os_name: ansible_facts[ 'distribution' ]
tasks:
- command: "dates"
ignore_errors: yes
- package:
name: p1
when: os_name == "RedHat" and os_var == 7
- copy:
dest: "/var/www/html/index.html"
content: "Hello hi"
- template:
dest: /etc/httpd/conf.d/kb.conf
src: "kb.conf.j2"
notify: web services
handlers:
- name: web services
service:
name: "httpd"
state: "restarted"
- debug:
msg: "test"
- service:
name: "haproxy"
# you can use register if this task changed, run that ...
This way also, you have lots of file and hard to manage. You need something intellegent decision to make.
in all files, name is different, but the variable is same. OS is different..
# cat web.yaml
- hosts: w1
vars_files:
- "RedHat-6.yaml"
vars:
- os_name: ansible_facts[ 'distribution' ]
tasks:
- command: "dates"
ignore_errors: yes
- package:
name: p
- copy:
dest: "/var/www/html/index.html"
content: "Hello hi"
- template:
dest: /etc/httpd/conf.d/kb.conf
src: "kb.conf.j2"
notify: web services
handlers:
- name: web services
service:
name: "httpd"
state: "restarted"
- debug:
msg: "test"
- service:
name: "haproxy"
ansible-class-notes-Dec-11-2020
PB
- set up
- docker install
- image centos
- os continer
- config web server
CN -> ssh (IP) enable
Docker, -> OS -> Continer
Run time environment.
Docker
PODMAN
CRI-O
kubernetes
DevOps
- ansible
===============================
# cat web.yaml
- hosts: w1
tasks:
- command: "date"
- package:
name: "httpd"
- service:
name: "httpd"
state: "started"
# ap.web.yaml
successful, but if type miss-type a work, what happens? all your task fails..
control the error which is called error handling...
what we want to do it, if somehing fails, continue. lets try with ignore_errors key word
[root@master wk-11]# vi web.yaml
- hosts: w1
tasks:
- command: "dates"
ignore_errors: yes
- package:
name: "httpd"
- service:
name: "httpd"
state: "started"
[root@master wk-11]# ansible-playbook web.yaml
TASK [command] *****************************************************************************************
fatal: [w1]: FAILED! => {"changed": false, "cmd": "dates", "msg": "[Errno 2] No such file or directory: b'dates': b'dates'", "rc": 2}
...ignoring
know what you are doing. can you ignore the error?
- hosts: w1
tasks:
- command: "dates"
ignore_errors: yes
- package:
name: "httpd"
- service:
name: "httpd"
state: "started"
# cat kb.conf.j2
Listen 8080
# ap web.yaml
# ap -v web.yaml
always checks changed features... idompotence
- we see, even there is no change, its keep running.
- and also service is keep restarting...
notify
we tell template or copy module, notify if there is any change...
it will do remaining handling....
we can create handler
its like
[root@master wk-11]# cat web.yaml
- hosts: w1
tasks:
- command: "dates"
ignore_errors: yes
- package:
name: "httpd"
- copy:
dest: "/var/www/html/index.html"
content: "Hello hi"
- template:
dest: /etc/httpd/conf.d/kb.conf
src: "kb.conf.j2"
notify: web services
handlers:
- name: web services
service:
name: "httpd"
state: "restarted"
- debug:
msg: "test"
- service:
name: "haproxy"
[root@master wk-11]# cat kb.conf.j2
Listen 8080
[root@master wk-11]#
# ap web.yaml
Nothing is changed.
Pls disable firewall and selinux and change
lets change the port and run it again.
[root@master wk-11]# cat kb.conf.j2
Listen 85
[root@master wk-11]# ansible-playbook web.yaml
review the output.
use when or lets use variable...
- hosts: w1
vars:
- os_name: ansible_facts[ 'distribution' ]
tasks:
- command: "dates"
ignore_errors: yes
- package:
name: "httpd"
- copy:
dest: "/var/www/html/index.html"
content: "Hello hi"
- template:
dest: /etc/httpd/conf.d/kb.conf
src: "kb.conf.j2"
notify: web services
handlers:
- name: web services
service:
name: "httpd"
state: "restarted"
- debug:
msg: "test"
- service:
name: "haproxy"
# you can use register if this task changed, run that ...
lets say you have different pkg name
- hosts: w1
vars:
- os_name: ansible_facts[ 'distribution' ]
- p1: "httpd"
- p2: "apache2"
tasks:
- command: "dates"
ignore_errors: yes
- package:
name: p1
when: os_name == "RedHat" and os_var == 7
- copy:
dest: "/var/www/html/index.html"
content: "Hello hi"
- template:
dest: /etc/httpd/conf.d/kb.conf
src: "kb.conf.j2"
notify: web services
handlers:
- name: web services
service:
name: "httpd"
state: "restarted"
- debug:
msg: "test"
- service:
name: "haproxy"
# you can use register if this task changed, run that ...
can you write code in such that it finds ip , os version and software that you want to install.. ??
lets create extra files
OS specific variable files
# vi RedHat-7.yml
-p: "httpd"
# vi RedHat-6.yml
-p: "httpd"
# vi Ubuntu-14.04.yml
-p: "apache2"
- hosts: w1
vars_files:
- "RedHat-6.yaml"
vars:
- os_name: ansible_facts[ 'distribution' ]
tasks:
- command: "dates"
ignore_errors: yes
- package:
name: p1
when: os_name == "RedHat" and os_var == 7
- copy:
dest: "/var/www/html/index.html"
content: "Hello hi"
- template:
dest: /etc/httpd/conf.d/kb.conf
src: "kb.conf.j2"
notify: web services
handlers:
- name: web services
service:
name: "httpd"
state: "restarted"
- debug:
msg: "test"
- service:
name: "haproxy"
# you can use register if this task changed, run that ...
This way also, you have lots of file and hard to manage. You need something intellegent decision to make.
in all files, name is different, but the variable is same. OS is different..
# cat web.yaml
- hosts: w1
vars_files:
- "RedHat-6.yaml"
vars:
- os_name: ansible_facts[ 'distribution' ]
tasks:
- command: "dates"
ignore_errors: yes
- package:
name: p
- copy:
dest: "/var/www/html/index.html"
content: "Hello hi"
- template:
dest: /etc/httpd/conf.d/kb.conf
src: "kb.conf.j2"
notify: web services
handlers:
- name: web services
service:
name: "httpd"
state: "restarted"
- debug:
msg: "test"
- service:
name: "haproxy"
No comments:
Post a Comment