The use of ansible roles in automatic operation and maintenance

1. Introduction to roles

  • Based on a known file structure, roles automatically loads some vars, files, tasks, and handlers. Based on Roles
    Grouping content makes it easy to share roles with other users.

2.ping the playbook s of all hosts

2.1 previous writing

[root@liang roles]# cat ping.yml
---
- hosts: all
  tasks:
    - name: ping host
      ping: 

2.2 roles

a) Create a hierarchical directory

[root@liang 2018-07-26]# mkdir roles/ping/tasks/ -p
[root@liang 2018-07-26]# cd roles/
[root@liang roles]# ll
total 8
drwxr-xr-x 3 root root 4096 Jul 26 08:30 ping
-rw-r--r-- 1 root root   39 Jul 26 08:35 site.yml

b) Create the header file site.yml

[root@liang roles]# cat site.yml ###Header file
---
- hosts: all
  roles:   ###Primary directory roles
    - ping ###Secondary directory ping

c) Create a file to control the ping role

[root@liang roles]# ls ping/tasks/
main.yml 
[root@liang roles]# cat ping/tasks/main.yml ###ping the role's entry
---
- name: ping host
  ping: 

d) Directory structure

[root@liang 2018-07-26]# tree 
.
└── roles 
    ├── ping
    │   └── tasks
    │       └── main.yml
    └── site.yml

2.3 test

[root@liang roles]# ansible-playbook ping.yml 

PLAY [all] *********************************************************************************

TASK [Gathering Facts] *********************************************************************
ok: [10.0.0.130]
ok: [10.0.0.131]

TASK [ping host] ***************************************************************************
ok: [10.0.0.131]
ok: [10.0.0.130]

PLAY RECAP *********************************************************************************
10.0.0.130                 : ok=2    changed=0    unreachable=0    failed=0   
10.0.0.131                 : ok=2    changed=0    unreachable=0    failed=0    
4.2roles test
[root@liang roles]# ansible-playbook site.yml 

PLAY [all] *********************************************************************************

TASK [Gathering Facts] *********************************************************************
ok: [10.0.0.130]
ok: [10.0.0.131]

TASK [ping : ping host] ********************************************************************
ok: [10.0.0.131]
ok: [10.0.0.130]

PLAY RECAP *********************************************************************************
10.0.0.130                 : ok=2    changed=0    unreachable=0    failed=0   
10.0.0.131                 : ok=2    changed=0    unreachable=0    failed=0   

[root@liang roles]# 

Tags: ansible

Posted on Fri, 31 Jan 2020 14:56:44 -0500 by crazycaddy