Ansible-14-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
//开始练习
[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