考试培训:
1、考题的地址和系统名称对应,最好带纸笔记录;
2、当前我们实验的密码是readhat
3、考试时候必须使用greg用户,放置到/home/greg/ansible/
(1)第一题
cp /etc/ansible/ansible.cfg /home/greg/ansible/ansible.cfg
host_key_checking = False
remote_user = root
(2)第二题创建和运行ansible临时命令
vim /home/greg/ansible/adhoc.sh
ansible all -m yum_repository -a 'name=“EX294_BASE” description=“EX294 base soft” basefile=“http://…” gpgkey=“xxxxx” enabled=yes ’
ansible all -m yum_repository -a 'name=“EX294_STEAM” description=“EX294 base soft” basefile=“http://…” gpgkey=“xxxxx” enabled=yes ’
bash adhoc.sh执行命令
(3)安装软件包
ansible-doc playbook(查阅如何配置)
vim /home/greg/ansible/packages.yml
-
name: 安装软件包A
hosts: dev,test,prod
tasks:- name: one
yum:
name: php
state: latest
-name: two
yum:
name: mariadb
state: latest
- name: one
-
name: 安装软件包B
host: dev
task:
-name: one
yum:
name: “@RPM Developmet Tools”
state: latest
-name: two
shell: “yum update * -y”
最后执行命令:
ansible-playbook packages.yml
(4)使用RHEL系统角色
vim /home/greg/ansible/timesync.yml
yum install
cd /usr/share/ansible/roles/
cp /usr/share/doc/rhel-system-roles/timesync/example-timesync.yml timesync.yml
ansible-galaxy list
vim timesync.yml
- hosts: all
times
-hostname: 172.25.254.254
…
运行脚本
ansible-playbook timensync.yuml
此处红色报错不要关注,毕竟没有fase
(5)使用ansible Galaxy安装角色
vim /home/greg/ansible/roles/requirements.yuml
mkdir roles
- src: http://materiasl/glaxy.tar
name: balancer - src: http://mate/glas2.tar
name: phpinfo
如果考题没有要求。无须安装,
ansible-galaxy -r requirement.yml
ansible-galaxy -r /roles/requirement.yml
(6)创建和使用角色
角色:本地角色、galaxy角色、外部导入角色
ansible-galaxy init apache
ansible-galaxy -list
ls
cd apache/
cd task
查看文档ansible-doc yum
vim main.yml
- name: one
yum:
name: httpd
state: latest - name: two
shell: “systemctl enable httpd” —取巧
state:started
enabled: yes - name:three
shell: firewall-cmd --add-service=http —取巧
firewalld:
service: http
permanet: yes用久生效
immediate: yes当前生效
state:enable - name: four
temlate:
src: index.html.j2
dest: /var/html/index.html
cd /template
touch index.html
查看文档ansible-doc firewalld
查看文档ansible-doc template
ansible all -m setup -a ‘filter=“fqdn”’
ansible all -m setup -a ‘filter=“ip”’
ansible_fqdn
ansible_default_ipv4
cd /template/index.html.j2
Welcome to { {ansible_fqdn} } on { { ansible_default_ipv4.address } }
(7)从ansible galaxy使用角色
vim /home/greg/ansible/roles.yml
连续三天:ansible