1.配置Ansible的主配置文件中。
[root@dsrw ~]# cd /etc/ansible/roles
[root@dsrw roles]# vim /etc/ansible/ansible.cfg
67#additionalpathstosearchforrolesin,colonseparated
68#roles_path=/etc/ansible/roles
[root@dsrw roles]# ansible-galaxy init apache
- Role apache was created successfully
[root@dsrw roles]# ls
Apache
[root@dsrw roles]# cd apache
[root@dsrw apache]# ls
defaults files handlers meta README.md tasks templates tests vars
2.配置用于定义角色任务的tasks/main.yml
[root@dsrw apache]# vim tasks/main.yml
---
- name: one
yum:
name: httpd
state: latest
3.使用service模块启动httpd网站服务程序,并加入到启动项中。
[root@dsrw apache]# ansible-doc service
EXAMPLES:
- name: Start service httpd, if not started
service:
name: httpd
state: started
[root@dsrw apache]# vim tasks/main.yml
---
- name: one
yum:
name: httpd
state: latest
- name: two
service:
name: httpd
state: started
enabled: yes
4.配置防火墙的允许策略,让其他主机可以正常访问。
[root@dsrw apache]# ansible-doc firewalld
EXAMPLES:
- firewalld:
service: https
permanent: yes
state: enabled
[root@dsrw apache]# vim tasks/main.yml
---
- name: one
yum:
name: httpd
state: latest
- name: two
service:
name: httpd
state: started
enabled: yes
- name: three
firewalld:
service: http
permanent: yes
state: enabled
immediate: yes
5.让每台主机显示的主页文件均不相同。
[root@dsrw apache]# ansible-doc template
EXAMPLES:
- name: Template a file to /etc/files.conf
template:
src: /mytemplates/foo.j2
dest: /etc/file.conf
owner: bin
group: wheel
mode: '0644'
[root@dsrw apache]# vim tasks/main.yml
---
- name: one
yum:
name: httpd
state: latest
- name: two
service:
name: httpd
state: started
enabled: yes
- name: three
firewalld:
service: http
permanent: yes
state: enabled
immediate: yes
- name: four
template:
src: index.html.j2
dest: /var/www/html/index.html
6.setup模块进行查询
[root@dsrw apache]# ansible-doc setup
EXAMPLES:
# Display facts from all hosts and store them indexed by I(hostname) at C(/tmp/>
# ansible all -m setup --tree /tmp/facts
# Display only facts regarding memory found by ansible on all hosts and output >
# ansible all -m setup -a 'filter=ansible_*_mb'
# Display only facts returned by facter.
# ansible all -m setup -a 'filter=facter_*'
# Collect only facts returned by facter.
# ansible all -m setup -a 'gather_subset=!all,!any,facter'
- name: Collect only facts returned by facter
setup:
gather_subset:
- '!all'
- '!any'
- facter
[root@dsrw apache]# ansible all -m setup -a 'filter="*ip*"'
192.168.1.12 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.1.12",
"192.168.122.1"
],
"ansible_all_ipv6_addresses": [
"fe80::d490:51fc:ca52:864"
],
"ansible_default_ipv4": {},
"ansible_default_ipv6": {},
"ansible_fips": false,
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false
7.新建一个与上面的template模块参数相同的文件名称(index.html.j2)。
[root@dsrw apache]# vim templates/index.html.j2
Welcome to {{ ansible_fqdn }} on {{ ansible_all_ipv4_addresses }}
8.做的就是编写一个用于调用apache角色的yml文件,以及执行这个文件。
[root@dsrw apache]# cd ~
[root@dsrw ~]# vim roles.yml
---
- name: 调用自建角色
hosts: all
roles:
- apache
[root@dsrw ~]# ansible-playbook roles.yml
PLAY [调用自建角色] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.1.12]
TASK [apache : one] ************************************************************
changed: [192.168.1.12]
TASK [apache : two] ************************************************************
changed: [192.168.1.12]
TASK [apache : three] **********************************************************
changed: [192.168.1.12]
TASK [apache : four] ***********************************************************
changed: [192.168.1.12]
PLAY RECAP *********************************************************************
192.168.1.12 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
9.客户机浏览器查看
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
请登录后查看评论内容