botang 发表于 2020-8-15 16:50:47

ANSIBLE15

---
- name: Playbook Control Lab
hosts: server3.example.com
vars_files: ./v7.d/vars.yml
tasks:
- name: show1
    fail:
      msg: "the {{ inventory_hostname }} did not meet minmum reqs"
    when: >
      ansible_memfree_mb < min_ram_mb or
      ansible_distribution != "RedHat"

# Install All Packages
- name: Install Packages
    yum:
      name: "{{ packages }}"
      state: latest

# Enable and Start Services
- name: enable
    service:
      name: "{{ item }}"
      state: started
      enabled: yes
    loop: "{{ services }}"
    ignore_errors: yes

# Block of Config Tasks
- name: Setting Up the SSL Cert Directory
    block:
    - name: Create SSL Dir
      file:
      path: "{{ ssl_cert_dir }}"
      state: directory

    - name: Copy File
      copy:
      src: "{{ item.src }}"
      dest: "{{ item.dest }}"
      loop: "{{ web_config_files }}"
      notify: h_copyfile

    rescue:
    - name: Configuration Error Messge
      debug:
      msg: >
          Welcome to xxxxxxxxxxxxxx
          cccccccccccccc
          cccccccccccccc

    always:
    - name: Welcome
      debug:
      msg: "Welcome to 123456"

- name: Config
    firewalld:
      service: "{{ item }}"
      permanent: yes
      state: enabled
      immediate: yes
    loop:
      - http
      - https

handlers:
- name: h_copyfile
    service:
      name: httpd
      state: restarted



---
- name:
hosts: srvgroup
tasks:
- name: Create A File and Set Permissions
    file:
      path: /srv/v7-2.txt
      owner: root
      group: root
      mode: 0600
      state: touch
      setype: public_content_t

- name: Using Selinux and Permanent
    sefcontext:
      target: /srv/v7-2.txt
      setype: public_content_t
      state: present

- name: Copy File Resideing on the Classroom
    copy:
      src: /etc/passwd
      dest: /srv
      force: yes

- name: Geting File from Remote Machine
    fetch:
      src: /etc/passwd
      dest: /tmp/test-passwd

- name: Certain One Line in File
    lineinfile:
      path: /srv/v7-2.txt
      line: "welcome to v7-2"
      create: yes

- name: Certain Lines in File
    blockinfile:
      path: /srv/v7-2.txt
      block: |
      first line welcome to v7-2
      second line welocme to v7-2
      state: present

- name: Delete File
    file:
      dest: /srv/v7-2.txt
      state: absent
    tags: tag1

- name: Check
    stat:
      path: /srv/v7-2.txt
    register: v_check
    tags: tag2

- name: Debug
    debug:
      var: v_check
    tags: tag3

- name: Message
    debug:
      msg: this file
    when: v_check.failed == false

- name: Synchronize
    synchronize:
      src: v7.d/secure
      dest: /srv/secure

$ ansible-playbookv7-2_srvgroup.yml    --step

PLAY **********************************************************************************************************************************************************
Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue: ***************************************************************************************************************

TASK ***************************************************************************************************************************************************
ok:
ok:
ok:
Perform task: TASK: Create A File and Set Permissions (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Create A File and Set Permissions (N)o/(y)es/(c)ontinue: *********************************************************************************************

TASK *********************************************************************************************************************************
changed:
changed:
changed:
Perform task: TASK: Using Selinux and Permanent (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Using Selinux and Permanent (N)o/(y)es/(c)ontinue: ***************************************************************************************************

TASK ***************************************************************************************************************************************
changed:
changed:
changed:
Perform task: TASK: Copy File Resideing on the Classroom (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Copy File Resideing on the Classroom (N)o/(y)es/(c)ontinue: ******************************************************************************************

TASK ******************************************************************************************************************************
changed:
changed:
changed:
Perform task: TASK: Geting File from Remote Machine (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Geting File from Remote Machine (N)o/(y)es/(c)ontinue: ***********************************************************************************************

TASK ***********************************************************************************************************************************
changed:
changed:
changed:
Perform task: TASK: Certain One Line in File (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Certain One Line in File (N)o/(y)es/(c)ontinue: ******************************************************************************************************

TASK ******************************************************************************************************************************************
changed:
changed:
changed:
Perform task: TASK: Certain Lines in File (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Certain Lines in File (N)o/(y)es/(c)ontinue: *********************************************************************************************************

TASK *********************************************************************************************************************************************
changed:
changed:
changed:
Perform task: TASK: Delete File (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Delete File (N)o/(y)es/(c)ontinue: *******************************************************************************************************************

TASK *******************************************************************************************************************************************************
changed:
changed:
changed:
Perform task: TASK: Check (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Check (N)o/(y)es/(c)ontinue: *************************************************************************************************************************

TASK *************************************************************************************************************************************************************
ok:
ok:
ok:
Perform task: TASK: Debug (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Debug (N)o/(y)es/(c)ontinue: *************************************************************************************************************************

TASK *************************************************************************************************************************************************************
ok: => {
    "v_check": {
      "changed": false,
      "failed": false,
      "stat": {
            "exists": false
      }
    }
}
ok: => {
    "v_check": {
      "changed": false,
      "failed": false,
      "stat": {
            "exists": false
      }
    }
}
ok: => {
    "v_check": {
      "changed": false,
      "failed": false,
      "stat": {
            "exists": false
      }
    }
}
Perform task: TASK: Message (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Message (N)o/(y)es/(c)ontinue: ***********************************************************************************************************************

TASK ***********************************************************************************************************************************************************
ok: => {
    "msg": "this file"
}
ok: => {
    "msg": "this file"
}
ok: => {
    "msg": "this file"
}
Perform task: TASK: Synchronize (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Synchronize (N)o/(y)es/(c)ontinue: *******************************************************************************************************************


---
- name: Flat
hosts: srvgroup
tasks:
- name: fetch
    fetch:
      src: /var/log/secure
      dest: v7.d/
      flat: false

- name: Copy A File to Managed hosts and Atrributes
    copy:
      src: /etc/passwd
      dest: /srv
      owner: devops
      group: sharedgroup
      mode: u+rw,g-wx,o-rwx
      setype: samba_share_t

- name: Using the File Module to Ensure Selinux File Context
    file:
      path: /srv/passwd
      seuser: _default
      serole: _default
      setype: _default
      selevel: _default
~                           
---
- name: Add Block of Test to a File
hosts: srvgroup
tasks:
- name: Copy File
    copy:
      src: /etc/passwd
      dest: /srv

- name: Add A Single
    blockinfile:
      path: /srv/passwd
      block: |
      welcome to xxxxx
      welcome to xxxxx
      state: present

- name: Import Two Lines of Test of A File
    lineinfile:
      path: /srv/welcome2
      line: |
      welcome to yyyyyy
      welcome to yyyyyy
      create: yes

---
- name: Add Block of Test to a File
hosts: srvgroup
tasks:
- name: Remove All File
    file:
      path: "{{ item }}"
      state: absent
    loop:
      - /srv/passwd
      - /srv/welcome2
    tags:
      - tag1




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