RHCE8.0:编写和运行PLAYBOOK
来自CloudWiki
目录
实训目的
在此目录中,使用文本编辑器创建名为site.yml的playbook。此playbook中含有一个play,它 应当以web主机组的成员为目标。该playbook应使用任务来确保受管主机上满足下列条款:
- 利用yum模块确保存在httpd软件包。
- 利用copy模块将本地的files/index. html文件复制到各受管主机上的/var/www/html/index.html
- 利用service模块启动和启用httpd服务。
您可以使用ansible-doc命令帮助了解各个模块所需的关键字。
编写了 playbook后,验证其语法,然后使用ansible-playbook运行playbook来实施配置
实训步骤
配置文件
在当前目录(/root)下新建目录0528
mkdir 0528
cd 0528
ansible.cfg
vi ansible.cfg
[defaults] inventory = /root/0528/hosts host_key_checking = False
inventory文件
vi hosts
[master] localhost ansible_connection=local ansible_ssh_user=root 10.0.0.30 ansible_ssh_user=root [slave] 10.0.0.32 ansible_ssh_user=root
验证:列出配置过的主机列表
ansible all --list-host
hosts (3): localhost 10.0.0.30 10.0.0.32
检验远程登录是否成功:
ansible all -a "date +'%Y-%m-%d %T'"
[root@ecs-1f97 0515]# [DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the controller starting with Ansible 2.12. Current version: 3.6.8 (default, Nov 16 2020, 16:55:22) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]. This feature will be removed from ansible-core in version 2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg. /usr/local/lib/python3.6/site-packages/ansible/parsing/vault/__init__.py:44: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography. The next release of cryptography will remove support for Python 3.6. from cryptography.exceptions import InvalidSignature localhost | CHANGED | rc=0 >> 2023-05-15 11:41:12 121.36.16.185 | CHANGED | rc=0 >> 2023-05-15 11:41:12 123.249.25.135 | CHANGED | rc=0 >> 2023-05-15 11:41:12 123.249.32.196 | CHANGED | rc=0 >> 2023-05-15 11:41:13
准备网页
vi index.html
<!doctype html> <html> <head> <meta charset="utf-8" /> <title>我的第一个 HTML 页面</title> </head> <body> <h2>我的第一个网页</h2> <p>这是我的第一个网页</p> <p>这是我的第一个网页,虽然很简单,但是很有意义。</p> </body> </html>
编写play
vi site.yml
--- - name: Install and start Apache HTTPD hosts: slave tasks: - name: httpd package is present yum: name: httpd state: present - name: correct index.html is present copy: src: /root/0528/index.html dest: /var/www/html/index.html - name: httpd is started service: name: httpd state: started enabled: true
语法检查
ansible-playbook --syntax-check site.yml
playbook: site.yml
执行play
ansible-playbook site.yml
PLAY [Install and start Apache HTTPD] *********************************************************** TASK [Gathering Facts] ************************************************************************** ok: [localhost] ok: [10.0.0.30] TASK [httpd package is present] ***************************************************************** ok: [localhost] ok: [10.0.0.30] TASK [correct index.html is present] ************************************************************ changed: [localhost] ok: [10.0.0.30] TASK [httpd is started] ************************************************************************* changed: [localhost] changed: [10.0.0.30] PLAY RECAP ************************************************************************************** 10.0.0.30 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 localhost : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
验证
在浏览器上输入受控机ip地址,看看能打开服务器上的网页吗 ?