botang 发表于 2020-8-12 20:33:55

ANSIBLE11

---
- name: Mariadb Server is Running
hosts: dbgroup
vars:
    v_mysql:
      - mariadb-server
      - python3-PyMySQL
tasks:
- name: Mariadb Package are Installed
    yum:
      name: "{{ item }}"
      state: latest
    loop: "{{ v_mysql }}"

- name: Start Database Running
    service:
      name: mariadb
      state: started
      enabled: yes

- name: Mariadb Server is Running
hosts: classroom.example.com
vars:
    v_mysql2:
      - mariadb-server
      - python3-PyMySQL
tasks:
- name: Mariadb Package are Installed
    yum:
      name: "{{ item }}"
      state: present
    loop: "{{ v_mysql2 }}"
    when: ansible_distribution == "RedHat"
下面这个有很像考题:

---
- name: Copy httpd Temp Config
hosts: server2.example.com
tasks:
- name: Copy httpdTemp
    copy:
      src: "/home/student/ansible/v6.d/vhosts.conf"
      dest: "/etc/httpd/conf.d/vhosts.conf"
    notify:
      - h_configfile

- name: Create Vhosts Dir
    file:
      dest: "/var/www/html/server2"
      state: directory
    notify:
      - h_direxists_server2

- name: Create Vhosts Dir2
    file:
      dest: /var/www/html/www2
      state: directory
    notify:
      - h_direxists_www2

handlers:
- name: h_direxists_server2
    copy:
      content: "Welcome to server2.example.com."
      dest: "/var/www/html/server2/index.html"

- name: h_direxists_www2
    copy:
      content: "Welcome to www2.example.com."
      dest: "/var/www/html/www2/index.html"

- name: h_configfile
    service:
      name: httpd
      state: restarted

- name: Test Vhosts
hosts: localhost
vars:
    v_url:
      - server2.example.com
      - www2.example.com
tasks:
- name: Connect
    uri:
      url: "http://{{ item }}"
      return_content: yes
      status_code: 200
    loop:
      "{{ v_url }}"
    register: v_result

- name: Show
    debug:
#      var: v_result
      msg:
      The result for "{{ item.content }}."
    loop:
      "{{ v_result.results }}"




get_url:

---
- name: Get URL
hosts: localhost
vars:
    v_url:
      - server1.example.com
      - www1.example.com
      - server2.example.com
      - www2.example.com
vars_files:
    - ./v6.d/www1.pass
tasks:

- name: Mkdir
    file:
      dest: "/home/student/ansible/v6.d/{{ item }}"
      state: directory
    loop:
      "{{ v_url }}"
    register:
      v_result

- name: debug
    debug:
      var: v_result

- name: Test
    debug:
      msg:
      "{{ item.item }}"
    loop:
      "{{ v_result.results }}"

- name: Get URL
    get_url:
      url: "http://{{ item.item }}/index.html"
      dest: "/home/student/ansible/v6.d/{{ item.item }}/index.html"
      username: testuser1
      password: '{{ web_pass }}'
    when: item.state== "directory"
    loop:
      "{{ v_result.results }}"


---
- name: Installing mariadb server
hosts: dbgroup
vars:
    v_db_packages:
      - mariadb-server
      - python3-PyMySQL
    v_db_service: mariadb
    v_resources_url: http://classroom.example.com/pub/
    v_config_file_url: "{{ v_resources_url }}/my.cnf"
    v_config_file_dest: /etc/my.cnf
tasks:
- name: Install Database Package
    yum:
      name: "{{ v_db_packages }}"
      state: present
    notify:
      - h_packagesinstall

- name: Make Sure the Database Service is Running
    service:
      name: "{{ v_db_service }}"
      enabled: yes
      state: started

- name: Get Database Config File
    get_url:
      url: "{{ v_config_file_url }}"
      dest: "{{ v_config_file_dest }}"
      owner: mysql
      group: mysql
      force: yes
    notify:
      - h_configfile

handlers:
- name: h_packagesinstall
    mysql_user:
      name: root
      password: redhat123

- name: h_configfile
    service:
      name: "{{ v_db_service }}"
      state: restarted
      enabled: yes



---
- name: ignore
hosts: server1.example.com
tasks:
    - name: install nopkg
      yum:
      name: nopkg
      state: latest
      ignore_errors: yes




页: [1]
查看完整版本: ANSIBLE11