红帽RHCE之Ansible-14-roles

Ansible-14-roles

  • 创建roles
[student@workstation ~]$ ansible-galaxy init roles_test
- roles_test was created successfully
[student@workstation ~]$ ls
roles_test
[student@workstation ~]$ cd roles_test/
[student@workstation roles_test]$ ls
defaults  files  handlers  meta  README.md  tasks  templates  tests  vars
[student@workstation roles_test]$ cd ..
[student@workstation ~]$ tree roles_test/
roles_test/
├── 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
  • roles 练习
//开始练习
[student@workstation ~]$ lab role-system start

Setting up workstation for Guided Exercise (role-system):

 · Verifying Ansible installation..............................  SUCCESS
 · Creating working directory..................................  SUCCESS
 · Deploying Ansible inventory.................................  SUCCESS
 · Deploying ansible.cfg.......................................  SUCCESS
 · Downloading roles/requirements.yml file.....................  SUCCESS

[student@workstation ~]$ ls
role-system
[student@workstation ~]$ cd role-system/
[student@workstation role-system]$ ls
ansible.cfg  inventory  roles
[student@workstation role-system]$ ansible-galaxy list
# /home/student/role-system/roles
# /usr/share/ansible/roles
# /etc/ansible/roles

[student@workstation role-system]$ tree ./
./
├── ansible.cfg
├── inventory
└── roles
    └── requirements.yml

1 directory, 3 files
[student@workstation role-system]$ lS
bash: lS: command not found...
Similar command is: 'ls'
[student@workstation role-system]$ LS
bash: LS: command not found...
Similar command is: 'ls'
[student@workstation role-system]$ ls
ansible.cfg  inventory  roles
[student@workstation role-system]$ cat ansible.cfg 
[defaults]
inventory=./inventory
remote_user=devops
roles_path=./roles:/usr/share/ansible/roles:/etc/ansible/roles

#Try me...
#callback_whitelist=profile_tasks

[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
[student@workstation role-system]$ cat inventory 
[controlnode]
workstation.lab.example.com


[na_datacenter]
servera.lab.example.com

[europe_datacenter]
serverb.lab.example.com


[database_servers]
servera.lab.example.com
serverb.lab.example.com


[student@workstation role-system]$ cat roles/requirements.yml 
---

# Sample Roles file
# with examples from Ansible Documentation:


# # from galaxy
# - src: yatesr.timezone
#
# # from GitHub
# - src: https://github.com/bennojoy/nginx
#
# # from GitHub, overriding the name and specifying a specific tag
# - src: https://github.com/bennojoy/nginx
#   version: master
#   name: nginx_role
#
# # from a webserver, where the role is packaged in a tar.gz
# - src: https://some.webserver.example.com/files/master.tar.gz
#   name: http-role
#
# # from Bitbucket
# - src: git+https://bitbucket.org/willthames/git-ansible-galaxy
#   version: v1.4
#
# # from Bitbucket, alternative syntax and caveats
# - src: https://bitbucket.org/willthames/hg-ansible-galaxy
#   scm: hg
#
# # from GitLab or other git-based scm, using git+ssh
# - src: git@gitlab.company.com:mygroup/ansible-base.git
#   scm: git
#   version: "0.1"  # quoted, so YAML doesn't parse this as a floating-point value
#
                   
[student@workstation role-system]$ cat ansible.cfg 
[defaults]
inventory=./inventory
remote_user=devops
roles_path=./roles:/usr/share/ansible/roles:/etc/ansible/roles

#Try me...
#callback_whitelist=profile_tasks

[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
[student@workstation role-system]$ cat inventory 
[controlnode]
workstation.lab.example.com


[na_datacenter]
servera.lab.example.com

[europe_datacenter]
serverb.lab.example.com


[database_servers]
servera.lab.example.com
serverb.lab.example.com


[student@workstation role-system]$ cat configure_time.yml 
---
- name: Time Synchronization
  hosts: database_servers
  roles:
          - rhel-system-roles.timesync
  post_tasks:
          - name: Set timezone
            timezone:
                    name: "{{ host_timezone }}"
            notify: restart crond
  handlers:
          - name: restart crond
            service:
                    name: crond
                    state: restarted
[student@workstation role-system]$ cat 
ansible.cfg         configure_time.yml  group_vars/         inventory           roles/              
[student@workstation role-system]$ cat group_vars/
europe_datacenter/ na_datacenter/     
[student@workstation role-system]$ cat group_vars/europe_datacenter/timezone.yml 
host_timezone: Europe/Helsinki
[student@workstation role-system]$ cat group_vars/na_datacenter/timezone.yml 
host_timezone: America/Chicago


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值