botang 发表于 2020-8-15 22:10:49

ANSIBLE19大型任务

---
- name: Import
hosts: webgroup
tasks:
- name: Include
    include_tasks: v9.d/environment.yml
    vars:
      v_package: httpd
      v_service: httpd

- name: Import
    import_tasks: v9.d/firewall.yml
    vars:
      v_firewall_pkg: firewalld
      v_firewall_svc: firewalld
      v_rule:
      - http
      - https

- name: Copy Files
    import_tasks: v9.d/placeholder.yml
    vars:
      v_file:
      - "/var/www/html/index.html"

- name: Import Playbook
import_playbook: v9.d/test.yml
vars:
    v_url:
      - "http://192.168.0.101"
      - "http://192.168.0.102"



改进后:

---
- name: Import
hosts: webgroup
tasks:
- name: Include
    include_tasks: v9.d/environment.yml
    vars:
      v_package: httpd
      v_service: httpd

- name: Import
    import_tasks: v9.d/firewall.yml
    vars:
      v_firewall_pkg: firewalld
      v_firewall_svc: firewalld
      v_rule:
      - http
      - https

- name: Copy Files
    import_tasks: v9.d/placeholder.yml
    vars:
      v_file:
      - "/var/www/html/index.html"

handlers:
- name: h_config
    service:
      name: httpd
      state: restarted
      enabled: yes





- name: Import Playbook
import_playbook: v9.d/test.yml
vars:
    v_url:
      - "http://192.168.0.101"
      - "http://192.168.0.102"


---
- name: Create Placeholder File
copy:
    content: "# This file was moved to /etc/other.conf"
    dest: "{{ item }}"
loop:
    "{{ v_file }}"

- name: delete config
file:
    path: "/etc/httpd/conf.d/vhosts.conf"
    state: absent
notify:
    - h_config

或者绿的变黄的:

---
- name: Create Placeholder File
copy:
    content: "# This file was moved to /etc/other.conf"
    dest: "{{ item }}"
loop:
    "{{ v_file }}"

- name: delete config
file:
    path: "/etc/httpd/conf.d/vhosts.conf"
    state: absent
changed_when: true
notify:
    - h_config

了解一下:connection: local,改进一下test.yml:
---
- name: Test Web Service
hosts: localhost
connection: local
tasks:
- name: Connect to Internet Web Server
    uri:
      url: "{{ item }}"
      status_code: 200
      return_content: yes
    loop:
      "{{ v_url }}"
    register: v_result
    when:
      ansible_facts['architecture'] == "x86_64"

- name: Show the Result
    debug:
      msg: "{{ item.content }}AND   {{ item.status }}"
    loop:
      "{{ v_result.results }}"

ansible-playbook的命令行参数:
$ ansible-playbook-i ./inventory.pyv9-1_webgroup.yml -e v_state=absent
页: [1]
查看完整版本: ANSIBLE19大型任务