管理事实。

首先以student用户身份并使用student作为密码登录workstation.
登录workstation
        [kiosk@foundation0 ~]$ ssh student@workstation
        Warning: Permanently added 'workstation,172.25.250.9' (ECDSA) to the list of known hosts.
        Activate the web console with: systemctl enable --now cockpit.socket
在workstation 上,运行 lab data-facts start 命令。该脚本将创建工作目录data-facts,并为它填充ansible 配置文件和主机清单。

1、以student用户身份,在workstation 上更改到 /home/student/data-facts 目录。
        [student@workstation ~]$ cd ~/data-facts
        [student@workstation data-facts]$ 

2、ansible setup模块从系统检索事实。运行一个临时命令,检索webserver 组中所有服务器的事实。输出中将以JSON 格斯显示为servera.lab.example.com 收集的所有事实。检查显示的部分变量。
        [student@workstation data-facts]$ ansible webserver -m setup
        servera.lab.example.com | SUCCESS => {
            "ansible_facts": {
                "ansible_all_ipv4_addresses": [
                    "172.25.250.10"
                ],
                "ansible_all_ipv6_addresses": [
                    "fe80::3b38:6d9e:2e0a:6444",
                    "fe80::e6c5:468e:edb6:9b52"
                ],   ……

3、在workstation 上,创建名为 /home/student/data-facts/custom.fact 的事实文件。该事实文件定义要在servera上安装的软件包和启动的服务。该文件应当如下所示:
        [student@workstation data-facts]$ vim custom.fact
        添加:

        [general]
        package = httpd
        service = httpd
        state = started
        enabled = true

4、创建 setup_facts.yml playbook, 以生成 /etc/ansible/facts.d 运程目录并将 custom.fact 文件保存到目录。
        [student@workstation data-facts]$ vim setup_facts.yml
        添加:

        ---
        - name: Instsall remote facts
          hosts: webserver
          vars:
            remote_dir: /etc/ansible/facts.d
            facts_file: custom.fact
          tasks:
            - name: Create the remote directory
              file:
                state: directory
               recurse: yes
                path: "{{ remote_dir }}"
            - name: Install the new facts
              copy:
                src: "{{ facts_file }}"
                dest: "{{ remote_dir }}"

5、使用setup 模块运行一个临时命令。搜索输出中的 ansible_local 部分。这是应当没有任何自定义事实。
        [student@workstation data-facts]$ ansible webserver -m setup
        servera.lab.example.com | SUCCESS => {
            "ansible_facts": {
                "ansible_all_ipv4_addresses": [
                    "172.25.250.10"
                ],
                "ansible_all_ipv6_addresses": [
            "fe80::e6c5:468e:edb6:9b52"
                ],
        ……

6、运行该playbook前,通过云更新ansible-palybook --syntax-check 来验证其语法是否正确。弱国报告任何错误,请更正后再继续下一步。您因看到类似于下文的输出:
        [student@workstation data-facts]$ ansible-playbook --syntax-check setup_facts.yml

        playbook: setup_facts.yml

7、运行setup_facts.yml playbook.
        [student@workstation data-facts]$ ansible-playbook --syntax-check setup_facts.yml

        playbook: setup_facts.yml
        [student@workstation data-facts]$ ansible-playbook setup_facts.yml

        PLAY [Instsall remote facts] ***************************************************

        TASK [Gathering Facts] *********************************************************
        ok: [servera.lab.example.com]

        TASK [Create the remote directory] *********************************************
        ok: [servera.lab.example.com]

        TASK [Install the new facts] ***************************************************
        changed: [servera.lab.example.com]

        PLAY RECAP *********************************************************************
        servera.lab.example.com    : ok=3    changed=1    unreachable=0    failed=0    skipped=0            rescued=0    ignored=0   

8、现在可以创建主playbook ,以同时使用默认事实和用户事实来配置wevera.在接下来的几步中,您要向playboo 文件添加内容。创建playbook.yml playbook ,使其包含以下内容:
        [student@workstation data-facts]$ vim playbook.yml

        添加:

        ---
        - name: Install Apache and starts the service
          hosts: webserver

          tasks:
            - name: Install the required package
              yum:
                  name: "{{ ansible_facts['ansible_local']['custom']['general']['package'] }}"
                  state: latest

            - name: Start the service
              service:
                  name: "{{ ansible_facts['ansible_local']['custom']['general']['service'] }}"
                  state: "{{ ansible_facts['ansible_local']['custom']['general']['state'] }}"
                  enabled: "{{ ansible_facts['ansible_local']['custom']['general']['enabled'] }}"

9、在运行playbook前,先使用临时命令验证httpd服务目前尚未在servera上运行。
        [student@workstation data-facts]$ ansible servera.lab.example.com -m command -a 'systemctl status httpd' 
        servera.lab.example.com | FAILED | rc=4 >>
        Unit httpd.service could not be found.non-zero return code

10、通过运行ansible-playbook --syntax-check ,验证playbook 的语法。如果报告任何错误,请更正后再继续下一步。您应看到类似于下文的输出:
        [student@workstation data-facts]$ ansible-playbook --syntax-check playbook.yml

        playbook: playbook.yml

11、使用ansible-playbook 命令运行该playbook.观察输出结果,ansible将先安装软件包,然后启用服务。
        [student@workstation data-facts]$ ansible-playbook playbook.yml 
        PLAY [Install Apache and starts the service] ***********************************

        TASK [Gathering Facts] *********************************************************
        ok: [servera.lab.example.com]

        TASK [Install the required package] ********************************************
        changed: [servera.lab.example.com]

        TASK [Start the service] *******************************************************
        changed: [servera.lab.example.com]

        PLAY RECAP *********************************************************************
        servera.lab.example.com    : ok=3    changed=2    unreachable=0    failed=0    skipped=0            rescued=0    ignored=0   

12、使用一个临时命令来执行systemctl ,以确定servera 现在是否在运行httpd 服务。
        [student@workstation data-facts]$ ansible servera.lab.example.com -m command -a 'systemctl status httpd'
        servera.lab.example.com | CHANGED | rc=0 >>
        ● httpd.service - The Apache HTTP Server
           Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
           Active: active (running) since Tue 2022-05-24 05:17:58 CST; 1min 51s ago
             Docs: man:httpd.service(8)
         Main PID: 3279 (httpd)
           Status: "Running, listening on: port 80"
            Tasks: 213 (limit: 4956)
           Memory: 42.8M
           CGroup: /system.slice/httpd.service
        ……

完成
在workstation上,运行 lab data-facts finish 脚本俩清理本实验中创建的资源.
        [student@workstation ~]$ lab data-facts finish

        Cleaning up the lab on servera.lab.example.com:

         · Removing httpd..............................................  SUCCESS
         · Removing custom facts.......................................  SUCCESS

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值