角色的创建、编写以及管理

本文详细介绍了如何在Ansible中创建和管理角色,包括使用`ansible-galaxy`创建角色框架,定义角色内容,处理文件传输以及编写handler任务。步骤涵盖了从角色创建到在inventory test中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.创建角色
[root@afei opt]# touch playbook.yml
[root@afei opt]# mkdir roles
[root@afei opt]# ls
playbook.yml  roles

2.如果在roles子目录中找不到角色会去ansible配置文件中设置的roles_path指定的目录中找
[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
8.查看roles目录下面的inventory test等内容
[root@afei tasks]# cd ../tests/
[root@afei tests]# cat inventory 
localhost

[root@afei tests]# cat test.yml 
---
- hosts: localhost
  remote_user: root
  roles:
    - httpd[root@afei tests]# 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值