[root@afei opt]# vim /etc/ansible/ansible.cfg
#roles_path = /etc/ansible/roles# uncomment this to disable SSH key host checking#host_key_checking = False# change the default callback, you can only have one 'stdout' type enabled at a time.#stdout_callback = skippy
## Ansible ships with some plugins that require whitelisting,
## this is done to avoid running all of a type by default.
## These setting lists those that you want enabled for your system.
## Custom plugins should not need this unless plugin author specifies it.# enable callback plugins, they can output to stdout but cannot be 'stdout' type.#callback_whitelist = timer, mail# Determine whether includes in tasks and handlers are "static" by/roles_path
3.用ansible-galaxy创建角色框架
[root@afei roles]# ansible-galaxy init httpd
- Role httpd was created successfully
[root@afei roles]# ls
httpd
[root@afei roles]# cd httpd/[root@afei httpd]# ls
defaults files handlers meta README.md tasks templates tests vars
[root@afei roles]# tree httpd/
httpd/
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── templates
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml
8 directories,8 files
4.定义角色内容
[root@afei roles]# cd httpd/[root@afei httpd]# ls
defaults files handlers meta README.md tasks templates tests vars
[root@afei httpd]# cd tasks/[root@afei tasks]# ls
main.yml
[root@afei httpd]# ls
defaults files handlers meta README.md tasks templates tests vars
[root@afei httpd]# cd templates/[root@afei templates]# touch httpd.conf.j2
[root@afei tasks]# vim main.yml
# tasks file for httpd- name: install httpd
yum:
name: httpd
state: present
- name: config httpd
template:
src:../template/httpd.conf.j2
dest:/etc/httpd/conf/httpd.conf
- name: service httpd
service:
name: httpd
state: started
5.将受控主机上的httpd配置文件传到控制机配置文件目录里面
[root@localhost conf]# scp httpd.conf 192.168.240.133:/opt/roles/httpd/templates/httpd.conf.j2
The authenticity of host '192.168.240.133 (192.168.240.133)' can't be established.
ECDSA key fingerprint is SHA256:1iyMo7Tk2s7Q5e9olRKAOudhT5hqseOcKVBjk8jYAoM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.240.133'(ECDSA) to the list of known hosts.
root@192.168.240.133's password:
httpd.conf
6.写handlers任务
[root@afei templates]# cd ../handlers/[root@afei handlers]# ls
main.yml
[root@afei handlers]# vim main.yml
---# handlers file for httpd- name: restart httpd
service:
name: httpd
state: restarted
7.进入任务tasks里面写notify
[root@afei tasks]# vim main.yml
# tasks file for httpd- name: install httpd
yum:
name: httpd
state: present
- name: config httpd
template:
src:../template/httpd.conf.j2
dest:/etc/httpd/conf/httpd.conf
notify:- restart httpd
- name: service httpd
service:
name: httpd
state: started