首先以student用户身份并使用student作为密码登录workstation.
在workstation 上,运行lab deploy-manage start 命令。此脚本将确保受管主机 servera 可在网络上访问。
[student@workstation ~]$ lab deploy-manage start
Setting up workstation for lab exercise work:
· ansible package is installed on workstation................. SUCCESS
创建目录 /home/student/deploy-manage ,拥于包含本练习的文件。更改到这个新建的目录
[student@workstation ~]$ mkdir ~/deploy-manage
[student@workstation ~]$ cd ~/deploy-manage
编辑新文件 ansible.vfg
[student@workstation deploy-manage]$ vim ansible.cfg
添加:
[defaults]
inventory = ./inventory
编辑新的静态清单文件inventory :含 四个主机
· [myself]应包含主机localhost;
· [intranetweb]应包含主机servera.lab.example.com;
· [internetweb]应包含主机serverb.lab.example.com;
· [web]应包含intranetweb和internetweb主机组。
[student@workstation deploy-manage]$ vim inventory
添加:
[myself]
localhost
[intranetweb]
servera.lab.example.com
[internetweb]
serverb.lab.example.com
[web:children]
intranetweb
internetweb
测试主机组配置:
[student@workstation deploy-manage]$ ansible myself --list-hosts
hosts (1):
localhost
[student@workstation deploy-manage]$ ansible intranetweb --list-hosts
hosts (1):
servera.lab.example.com
[student@workstation deploy-manage]$ ansible internetweb --list-hosts
hosts (1):
serverb.lab.example.com
[student@workstation deploy-manage]$ ansible web --list-hosts
hosts (2):
servera.lab.example.com
serverb.lab.example.com
[student@workstation deploy-manage]$ ansible all --list-hosts
hosts (3):
localhost
servera.lab.example.com
serverb.lab.example.com
在ansible.cfg文件中添加[privilege_escalation] 部分,将ansible配置为受管主机上运行任务是自动使用 sudo 命令从student 切换到 root . ansible还应配置为提示您输入 student用户用于sudo命令的密码
[student@workstation deploy-manage]$ vim ansible.cfg
添加:
[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = true
再次运行ansible --list-hosts命令,以验证现在是否提示您输入sudo 密码。提示输入sudo密码时,请输入student,即使它不用于本次空运行
[student@workstation deploy-manage]$ ansible intranetweb --list-hosts
BECOME password:
hosts (1):
servera.lab.example.com
完成:
清理本练习
[student@workstation deploy-manage]$ lab deploy-manage finish
Finishing lab exercise work:
· Cleaning workstation:....................................... SUCCESS