ansible期末1(wps)

3-1 在 workstation 上创建名为/ansible/playbooks/users.yml 的 playbook 完成以下任务:

(1) 在agent1和agent2上分别建立如下的用户和组,并设置所有用户密码为本人学号。
(2) 在agent1上删除OaManager用户,并删除其家目录


- name: agent1 and agent2 create group user
  hosts: all
  gather_facts: no
  vars:
    group_list: ['manager','consumer']
    con_list: ['1','3','5','7','9','11']
  tasks:
    - name: create group
      group:
        name: "{{ item }}"
        state: present
      with_items:
        - "{{ group_list }}"

    - name: create user ma
      user:
        name: "ma{{ item }}"
        group: manager
        password: "{{ '2103010225' | password_hash('sha512') }}"
        state: present
      with_sequence:
        count=15

    - name: create user con
      user:
        name: "con{{ item }}"
        group: consumer
        password: "{{ '2103010225' | password_hash('sha512') }}"
        state: present
      with_items:
        - "{{ con_list }}"
- name: del user OaManager
  hosts: agent1
  gather_facts: no
  tasks:
    - name: del user
      user:
        name: OaManager
        remove: yes
        state: absent

3-2 在 workstation 上创建名为/ansible/playbooks/createUsers.yml 的 playbook 完成以下任务:

(1) 在agent1和agent2上分别建立如下的用户和组,并设置所有用户密码为本人学号。
(2) 在agent2上删除testUser用户,并删除其家目录。

- name: agent1
  hosts: agent1
  gather_facts: no
  tasks:
    - name: create group oaUser
      group:
        name: oaUser
        state: present

    - name: create user usr
      user:
        name: "usr{{ item }}"
        group: oaUser
        password: "{{ '2103010225' | password_hash('sha512') }}"
        state: present
      with_sequence:
        count=25

- name: agent2
  hosts: agent2
  gather_facts: no
  vars:
    emp_list: ['1','3','5','7','9','11','13','15']
  tasks:
    - name: create group employee
      group:
        name: employee
        state: present

    - name: create user emp
      user:
        name: "emp{{ item }}"
        group: employee
        password: "{{ '2103010225' | password_hash('sha512') }}"
        state: present
      with_items:
        - "{{ emp_list }}"
    - name: del user testUser
      user:
        name: testUser
        remove: yes
        state: absent

4.生成硬件报告

创建一个名为/ansible/playbooks/hwreport.yml 的 playbook ,它将在所有受管节点上生成含有以下信息的输出文件 /opt/hwreport.txt :        
    HardWare details:
    Memory:  xxMB以 MB 表示的总内存大小
    BIOS:  xx 版本
    Disk:   sda1  xxMB, sda2 xxMB.     
- name: hwreport
  hosts: webservers
  tasks:
    - name: create file
      copy:
        dest: /opt/hwreport.txt
        content: |
    - name:  2
      shell: echo -e 'HardWare details':' \nMemory':'  {{ ansible_memtotal_mb }}  \nBIOS':' {{ ansible_bios_version }} \nsda1 ':' {{ ansible_devices.sda.partitions.sda1.size }} \nsda2 ':'  {{ ansible_devices.sda.partitions.sda1.size }}   ' > /opt/hwreport.txt

5.配置 cron 作业

创建一个名为/ansible/playbooks/cron.yml 的 playbook :
该 playbook 在 dbservers主机组中的受管节点上运行
配置 cron 作业,该作业每隔 2 分钟运行并执行以下命令:
xxxx-xx-xx xx:xx “ansible in progress” 【xxxx-xx-xx xx:xx为当前时间,年-月-日 时:分】

- hosts: dbservers
  gather_facts: no
  tasks:
    - name: create cron
      cron:
        name: ansible in progress
        minute: "*/2"
        job: 'date "+%Y-%m-%d %H:%M:%S"  "ansible in progress" '

6.在 workstation 上创建名为/ansible/playbooks/safty.yml,实现远程关闭agent1和agent2防火墙和selinux,并分别实现重启后保持生效。

- name: stop selinux
  hosts: webservers
  tasks:
    - name: stop firewalld
      service:
        name: firewalld
        state: stopped
        enabled: no

    - name: selinux disable
      replace:
        path: /etc/selinux/config
        regexp: '^SELINUX=enforcing'
        replace: 'SELINUX=disabled'

    - name: setenforce 0
      shell: " setenforce 0 "

7-1 在 workstation 上创建名为/ansible/playbooks/copyfile.yml, 拷贝来自workstation的/data/news.txt 到agentX的/opt/目录。(X为1或2)

(1) 如果托管主机agentX存在/var/news.txt,则不拷贝。
(2) 如果托管主机agentX和agent2不存在/var/news.txt,则不拷贝。

7-2 在 workstation 上创建名为/ansible/playbooks/copyfile.yml, 拷贝来自workstation的/data/school.txt 到agentX的/opt/目录。(X为1或2)

(1) 如果托管主机agentX存在/opt/school.txt,则不拷贝。
(2) 如果托管主机agentX和agent2不存在/opt/school.txt,则不拷贝

- name: copyfile
  hosts: webservers
  gather_facts: no
  tasks:
    - name: 1
      copy:
        src: /data/school.txt
        dest: /opt/
        force: no

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