ansible期末3(wps)

11. 在 workstation 上创建名为/ansible/playbooks/ansible.yml 的 playbook 显示主机的信息:

(1)在所有受管主机中运行此 playbook
(2)此 playbook 在 /var/www/html/ansible_details.html 文件中创建一行信息,包含以下内容:”受管主机的短主机名;受管主机的物理内核数量;受管主机默认网卡的 IPV4 地址;受管主机默认网卡的MAC地址”
(3)内容如下例:
Example output: agent2 4 123.123.123.123 DE:AD:BE:EF:DE:AD:BE:EF
(4)并将ansible_details.html文件上传到管理主机的/ansible/data目录。

- name:
  hosts: webservers
  tasks:
    - name: create file
      copy:
        dest: /var/www/html/ansible_details.html
        content: |
    - name: 2
      shell:  echo  ' Example output ':'  {{ ansible_nodename }} {{ ansible_processor_vcpus }} {{ ansible_default_ipv4.address }}  {{ ansible_default_ipv4.macaddress}}' > /var/www/html/ansible_details.html
    - name: update
      fetch:
        src: /var/www/html/ansible_details.html
        dest: /ansible/data/
        force: yes
复制代码

12. 在 workstation 上创建名为/ansible/playbooks/tagdocs.yml 的 playbook 完成以下任务:

(1) /data/sport.txt文件在 workstation 上
(2)运行/ansible/playbooks/tagdocs.yml –tags “production” 将agentX本机的/data/sport.txt文件拷贝 到 agent1的/var/www/html/tag1.html
(3)运行/ansible/playbooks/tagdocs.yml –tags “backup” 将拷贝 /data/sport.txt 到 agent2 的/var/www/html/tag2.html


复制代码

13. 在 workstation 上创建名为/ansible/playbooks/saveabort.yml 完成以下任务:

在所有受管主机上运行
(1) 创建 / opt /ansible_abort.txt 的文件,包含my node is also agent#,
agent # 代表此 playbook 运行在 agent1, agent2
(2) 如果文件 /opt/ansible_abort.txt 已存在,则不要做任何修改

14. 实现错误处理。在 workstation 上创建名为/ansible/playbooks/mariadb.yml 的 playbook:

(1) 在 agent1 上尝试安装mariadb、mariadb-server,并启动服务
(2) 如果软件包安装或服务启动失败,playbook 获取任务信息并显示
(3) 即使一个任务执行失败,其它所有任务也必须被执行
(4) 如果安装任务失败,为了便于排错,需要显示 debug 信息,以及 Installation failed
(5) 如果配置任务失败,为了便于排错,需要显示服务启动失败的 debug 信息,以及 Starting failed

- name: Install and start MariaDB
  hosts: webservers
  tasks:
    - name: Install MariaDB and MariaDB server
      yum:
        name:
          - mariadb
          - mariadb-server
        state: present
      ignore_errors: true
      register: install_result

    - name: Start MariaDB service
      service:
        name: mariadb
        state: started
      ignore_errors: true
      register: service_result

    - name: Display install debug information if failed
      debug:
        msg: "Installation failed"
      when: install_result is failed

    - name: Display service start debug information if failed
      debug:
        msg: "Starting failed"
      when: service_result is failed
~
复制代码

15. 创建和使用角色

根据下列要求,在 /ansible/playbooks/roles 中创建名为 apache 的角色:
(1) httpd 软件包已安装,设为在系统启动时启用并启动
(2) 关闭防火墙
(3) 模板文件 index.html.j2 已存在,用于创建具有以下输出的文件 /var/www/html/index.html :
Welcome to HOSTNAME on IPADDRESS
其中,HOSTNAME 是受管节点的完全限定域名,IPADDRESS 则是受管节点的 IP 地址。
(4) 创建一个名为 /ansible/playbooks/apache.yml 的 playbook:
该 play 在 webservers 主机组中的主机上运行并将使用 apache 角色

# 创建角色
ansible-galaxy init apache 
复制代码
# roles/apache/tasks/main.yml
# tasks file for apache
- name: install apache
  yum:
    name: httpd
    state: present

- name: stop firewalld
  service:
    name: "{{ item }}"
    state: started
    enabled: yes
  loop:
    - httpd
    - firewalld

- name: stop
  firewalld:
    service: http
    permanent: yes
    immediate: yes
    state: enabled

- name: copy
  template:
    src: index.html.j2
    dest: /var/www/html/index.html
复制代码
# index.html.js2
Welcome to {{ ansible_hostname }} on {{ ansible_all_ipv4_addresses}}
复制代码
# apache.yml
- name: use role
  hosts: webservers
  roles:
    - apache
复制代码

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论