RHCE8.0实训:实施多个PLAYBOOK

来自CloudWiki
跳转至: 导航搜索

实训目的

在本练习中,您将创建包含多个play的playbook,再用它来在受管主机上执行配置任务

实训步骤

将目录更改到 0603 工作目录。

[student@workstation -]$ cd ~/0603

vi ansible.cfg:

[defaults]
inventory =hosts
host_key_checking = False
remote_user=maxin

[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False

vi inventory

[master]
localhost ansible_connection=local ansible_ssh_user=root
10.0.0.30 ansible_ssh_user=root
[slave]
10.0.0.40 ansible_ssh_user=root

新建 playbook: intranet.yml

 
---
  - name: Enable intranet services
    hosts:slave
    become: yes
    tasks:
      - name: latest version of httpd and firewalld installed
        yum:
          name:
            - httpd
            - firewalld
          state: latest
      - name: test html page is installed
        copy:
          content: "Welcome to the example.com intranet!\n"
          dest: /var/www/html/index.html
      - name: firewalld enabled and running
        service:
          name: firewalld
          enabled: true
          state: started
      - name: firewalld permits access to httpd srevice
        firewalld:
          service: http
          permanent: true
          state: enabled
          immediate: yes
      - name: httpd enabled and running
        service:
          name: httpd
          enabled: true
          state: started
  - name: Testintranet web server
    hosts: localhost
    become: no
    tasks:
      - name: connect to intranet web server
        uri:
          url: http://10.0.0.40
          return_content: yes
          status_code: 200

语法验证

[root@localhost playbook-multi]# ansible-playbook --syntax-check intranet.yml

[DEPRECATION WARNING]: The firewalld module has been moved to the ansible.posix
 collection. This feature will be removed from community.general in version
2.0.0. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.

playbook: intranet.yml

执行play

ansible-playbook intranet.yml

PLAY [Testintranet web server] *************************************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [connect to intranet web server] ******************************************
ok: [localhost] => {"accept_ranges": "bytes", "changed": false, "connection": "close", "content": "  <!doctype html>\n  <html>\n      <head>\n           <meta charset=\"utf-8\" />\n           <title>我的第一个 HTML 页面</title>\n      </head>\n\n      <body>\n           <h2>我的第一个网页</h2>\n           <p>这是我的第一个网页</p>\n           <p>这是我的第一个网页,虽然很简单,但是很有意义。</p>\n      </body>\n  </html>\n", "content_length": "358", "content_type": "text/html; charset=UTF-8", "cookies": {}, "cookies_string": "", "date": "Sun, 14 Feb 2021 07:19:43 GMT", "elapsed": 0, "etag": "\"166-5bb355c0386ad\"", "last_modified": "Sat, 13 Feb 2021 10:38:54 GMT", "msg": "OK (358 bytes)", "redirected": false, "server": "Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips mod_fcgid/2.3.9", "status": 200, "url": "http://10.0.0.30"}

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0