安装和配置Ansible

安装Ansible

  1. [root@bastion ~]#安装Ansible服务
  2. [root@bastion ~]yum install ansible.noarch -y

配置Ansible

  1. [greg@bastion ~]$ # 定义静态清单文件
  2. [greg@bastion ~]$ vim ./ansible/inventory
  3. [greg@bastion ~]$ cat ./ansible/inventory
  4. [dev]
  5. 172.25.250.9
  6. [test]
  7. 172.25.250.10
  8. [prod]
  9. 172.25.250.11
  10. 172.25.250.12
  11. [balancers]
  12. 172.25.250.13
  13. [webservers:children]
  14. prod
  15. [greg@bastion ansible]$ cat ansible.cfg
  16. [defaults]
  17. inventory = /home/greg/ansible/inventory
  18. roles_path = /home/greg/ansible/roles
  19. [privilege_escalation]
  20. become=True
  21. become_method=sudo
  22. become_user=root
  23. [greg@bastion ansible]$ mkdir /home/greg/ansible/roles
  24. [greg@bastion ansible]$ #可通过Ansible默认的配置文件来参考,路径为/etc/ansible/ansible.cfg

配置yum源

  1. [greg@bastion ansible]$ cat /home/greg/ansible/adhoc.sh
  2. #!/bin/bash
  3. ansible all -m yum_repository -a "name=EX294_BASE description='EX294 base software' baseurl=ftp://host.domain8.example.com/dvd/BaseOS enabled=yes gpgcheck=yes gpgkey=tp://host.domain8.example.com/dvd/RPM-GPG-KEY-redhat-release "
  4. ansible all -m yum_repository -a "name=EX294_STREAM description='EX294 stream software' baseurl=ftp://host.domain8.example.com/dvd/AppStream enabled=yes gpgcheck=yes gpgkey=ftp://host.domain8.example.com/dvd/RPM-GPG-KEY-redhat-release "
  5. [greg@bastion ansible]$ chmod +x /home/greg/ansible/adhoc.sh

安装软件包

  1. ---
  2. - hosts: [dev,test,prod]
  3. tasks:
  4. - name: Install php and mariadb services
  5. yum:
  6. name: php,mariadb
  7. state: installed
  8. - hosts: [dev]
  9. tasks:
  10. - name: Install RPM Development Tools
  11. yum:
  12. name: '@RPM Development Tools'
  13. state: installed
  14. - name: Update the service version
  15. yum:
  16. name: '*'
  17. state: latest

使用RHEL系统角色之 rhel-system-roles.timesync

  1. #按照题目要求将系统rhel-system-roles.timesync角色内容复制到题目指定位置
  2. [greg@bastion roles]$ cp -r /usr/share/ansible/roles/rhel-system-roles.timesync/ /home/greg/ansible/roles/timesync
  3. [greg@bastion ansible]$ cat timesync.yml
  4. ---
  5. - hosts: all
  6. vars:
  7. timesync_ntp_servers:
  8. - hostname: 172.25.250.250
  9. pool: yes
  10. iburst: yes
  11. roles:
  12. - role: timesync
  13. [greg@bastion ansible]$ #运行playbook
  14. [greg@bastion ansible]$ ansible-playbook timesync.yml

使用Ansible Galaxy安装角色

  1. #通过角色文件安装
  2. [greg@bastion ansible]$ ansible-galaxy install -r roles/requirements.yml -p roles
  3. [greg@bastion roles]$ cat requirements.yml
  4. ---
  5. - src: http://host.domain8.example.com/ex300/haproxy.tar.gz
  6. name: balancer
  7. - src: http://host.domain8.example.com/ex300/phpinfo.tar.gz
  8. name: phpinfo
  9. [greg@bastion roles]$
  10. #查看已下载角色
  11. [greg@bastion ansible]$ ansible-galaxy list
  12. # /home/greg/ansible/roles
  13. - timesync, (unknown version)
  14. - balancer, (unknown version)
  15. - phpinfo, (unknown version)
  16. [greg@bastion ansible]$

通过角色创建http服务

  1. #在roles目录下创建apache角色
  2. [greg@bastion roles]$ ansible-galaxy init apache
  3. - apache was created successfully
  4. [greg@bastion roles]$ ls
  5. apache balancer requirements.yml timesync
  6. [greg@bastion roles]$
  7. #编写task文件
  8. [greg@bastion apache]$ cd tasks/
  9. [greg@bastion tasks]$ cat main.yml
  10. ---
  11. # tasks file for apache
  12. - name: Install httpd service
  13. yum:
  14. name: httpd
  15. state: installed
  16. - name: Enable httpd service
  17. service:
  18. name: httpd
  19. state: started
  20. enabled: true
  21. - name: Enable firewall service
  22. service:
  23. name: firewalld
  24. state: started
  25. enabled: true
  26. - name: Enable http service in firewall
  27. firewalld:
  28. service: http
  29. immediate: yes
  30. permanent: yes
  31. state: enabled
  32. - name: Copy http index.html
  33. template:
  34. src: index.html.j2
  35. dest: /var/www/html/index.html
  36. [greg@bastion tasks]$
  37. # 创建j2文件
  38. [greg@bastion apache]$
  39. [greg@bastion apache]$ cd ./templates/
  40. [greg@bastion templates]$ cat index.html.j2
  41. Welcome to {{ansible_fqdn}} on {{ansible_default_ipv4.address}}
  42. [greg@bastion templates]$
  43. # 创建playbook
  44. [greg@bastion ansible]$ cat newroles.yml
  45. ---
  46. - hosts: webservers
  47. name: Init http service
  48. roles:
  49. - role: apache
  50. [greg@bastion ansible]$

从Ansible Galaxy中使用角色

  1. [greg@bastion ansible]$ cat roles.yml
  2. ---
  3. - hosts: all
  4. - hosts: balancers
  5. roles:
  6. - role: balancer
  7. - hosts: webservers
  8. roles:
  9. - role: phpinfo

创建和使用逻辑卷

  1. [greg@bastion ansible]$ cat /home/greg/ansible/lv.yml
  2. ---
  3. - hosts: lvgroup
  4. tasks:
  5. - name: volume exsit or not
  6. shell: 'vgdisplay research'
  7. register: exsit_or_not
  8. ignore_errors: yes
  9. - name: if not exsit, will display the message
  10. fail:
  11. msg: "Volume group done not exist"
  12. when: exsit_or_not.rc != 0
  13. - name: if exsit, will create the lv
  14. block:
  15. - name: Create the lv for 1500M
  16. lvol:
  17. vg: research
  18. lv: data
  19. size: 1500m
  20. rescue:
  21. - name: Display the message,if does not have enough spaces
  22. debug:
  23. msg: "Could not create logical volume of that size"
  24. - name: Create the lv for 800M
  25. lvol:
  26. vg: research
  27. lv: data
  28. size: 800m
  29. - name: Format the lv for ext4
  30. filesystem:
  31. dev: /dev/research/data
  32. fstype: ext4
  33. force: yes

创建并使用磁盘分区

  1. [greg@bastion ansible]$ cat partition.yml
  2. ---
  3. - hosts: diskgroup
  4. tasks:
  5. - name: Disk exist or not
  6. shell: 'ls /dev/vdb'
  7. register: disk_exist_or_not
  8. ignore_errors: yes
  9. - name: If disk does not exsit, will display message
  10. fail:
  11. msg: "disk does not exist"
  12. when: disk_exist_or_not.rc != 0
  13. - name: If disk exsit, Create a main partition
  14. block:
  15. - name: Create a main partition for 1500M
  16. parted:
  17. device: /dev/vdb
  18. number: 1
  19. part_end: 1500MiB
  20. state: present
  21. rescue:
  22. - name: Can not create a 1500M partition
  23. debug:
  24. msg: 'could not create partation of that size'
  25. - name: Create a main partition for 800M
  26. parted:
  27. device: /dev/vdb
  28. number: 1
  29. part_end: 800MiB
  30. state: present
  31. - name: Format the partition
  32. filesystem:
  33. dev: /dev/vdb1
  34. fstype: ext4
  35. force: yes
  36. - name: Mount the partition
  37. mount:
  38. src: /dev/vdb1
  39. path: /newpart
  40. fstype: ext4
  41. state: mounted

生成主机文件

  1. [greg@bastion ansible]$ cat hosts.j2
  2. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  3. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  4. {% for host in groups['all'] %}
  5. {{ hostvars[host]['ansible_default_ipv4']['address']}} {{ hostvars[host]['ansible_fqdn'] }} {{ hostvars[host]['ansible_hostname'] }}
  6. {% endfor %}
  7. [greg@bastion ansible]$
  8. [greg@bastion ansible]$ cat hosts.yml
  9. ---
  10. - hosts: all
  11. - hosts: dev
  12. tasks:
  13. - name: Generate the host file of server
  14. template:
  15. src: /home/greg/ansible/hosts.j2
  16. dest: /etc/myhosts
  17. [greg@bastion ansible]$

修改文件内容

  1. [greg@bastion ansible]$ cat issue.yml
  2. ---
  3. - hosts: all
  4. tasks:
  5. - name: For dev
  6. copy:
  7. content: "Development\n"
  8. dest: /etc/issue
  9. when: inventory_hostname in groups['dev']
  10. - name: For test
  11. copy:
  12. content: "Test\n"
  13. dest: /etc/issue
  14. when: inventory_hostname in groups['test']
  15. - name: For production
  16. copy:
  17. content: "Production\n"
  18. dest: /etc/issue
  19. when: inventory_hostname in groups['prod']
  20. [greg@bastion ansible]$

创建Web内容目录

  1. [greg@bastion ansible]$ cat webcontent.yml
  2. ---
  3. - hosts: dev
  4. roles:
  5. - role: apache
  6. tasks:
  7. - name: Create group
  8. group:
  9. name: webdev
  10. - name: Create Directory
  11. file:
  12. path: /webdev
  13. group: webdev
  14. state: directory
  15. mode: 2775
  16. setype: httpd_sys_content_t
  17. - name: Create softlink
  18. file:
  19. src: /webdev
  20. dest: /var/www/html/webdev
  21. state: link
  22. - name: Create a file
  23. copy:
  24. content: "Development\n"
  25. dest: /webdev/index.html
  26. setype: httpd_sys_content_t
  27. [greg@bastion ansible]$

生成硬件报告

  1. [greg@bastion ansible]$ cat hwreport.yml
  2. ---
  3. - hosts: all
  4. tasks:
  5. - name: Remove the file, if exist
  6. shell: "rm -rf /root/hwreport.txt"
  7. - name: Download the file
  8. get_url:
  9. url: "http://host/ex300/hwreport.empty"
  10. dest: /root/hwreport.txt
  11. - name: Edit the value of the HOST
  12. lineinfile:
  13. path: /root/hwreport.txt
  14. regexp: '^HOST'
  15. line: 'HOST={{inventory_hostname}}'
  16. - name: Edit the value of the MEMORY
  17. lineinfile:
  18. path: /root/hwreport.txt
  19. regexp: '^MEMORY'
  20. line: 'MEMORY={{ansible_memtotal_mb}}MB'
  21. - name: Edit the value of the BIOS
  22. lineinfile:
  23. path: /root/hwreport.txt
  24. regexp: '^BIOS'
  25. line: 'BIOS={{ansible_bios_version}}'
  26. - name: Edit the value of the DISK_SIZE_VDA
  27. lineinfile:
  28. path: /root/hwreport.txt
  29. regexp: '^DISK_SIZE_VDA'
  30. line: 'DISK_SIZE_VDA={{ansible_devices.vda.size}}'
  31. - name: Edit the value of the DISK_SIZE_VDB
  32. block:
  33. - name: If DISK VDB exist
  34. lineinfile:
  35. path: /root/hwreport.txt
  36. regexp: '^DISK_SIZE_VDB'
  37. line: 'DISK_SIZE_VDB={{ansible_devices.vdb.size}}'
  38. rescue:
  39. - name: If DISK VDB does not exist
  40. lineinfile:
  41. path: /root/hwreport.txt
  42. regexp: '^DISK_SIZE_VDB'
  43. line: 'DISK_SIZE_VDB=NONE'
  44. [greg@bastion ansible]$
  45. [greg@bastion ansible]$
  46. [greg@bastion ansible]$
  47. [greg@bastion ansible]$

创建密码库

  1. [greg@bastion ansible]$ cat /home/greg/ansible/secret.txt
  2. redhat
  3. [greg@bastion ansible]$ cat locker.yml
  4. pw_developer: redhat
  5. pw_manager: redhat
  6. [greg@bastion ansible]$ ansible-vault encrypt --vault-id=secret.txt locker.yml
  7. Encryption successful
  8. [greg@bastion ansible]$ ansible-vault view --vault-id=secret.txt locker.yml
  9. pw_developer: redhat
  10. pw_manager: redhat
  11. [greg@bastion ansible]$ cat locker.yml
  12. $ANSIBLE_VAULT;1.1;AES256
  13. 31343035333566653262653461353839316365373937393961343732386431386262373838323830
  14. 6163323965323630346330346461623362663964623666610a343536653564303464373535376530
  15. 35623033353330663666613234363539656666633132656531663333646532363935643164313633
  16. 3636653166373039620a376536623764643137346333646335383139633039353063356162663466
  17. 66643830623933336634336530366162366363633861333465393532303235376363643238326262
  18. 6436363666633533396461383264346635396136343130383134
  19. [greg@bastion ansible]$

创建用户账号

  1. ---
  2. - hosts: [dev,test]
  3. vars_files:
  4. - user_list.yml
  5. - locker.yml
  6. tasks:
  7. - name: Create a group
  8. group:
  9. name: devops
  10. state: present
  11. - name: Create a user
  12. user:
  13. name: "{{ item.name }}"
  14. comment: "{{ item.job }}"
  15. groups: devops
  16. password: "{{ pw_developer | password_hash('sha512') }}"
  17. when: item.job == 'developer'
  18. loop: "{{users}}"
  19. - hosts: [prod]
  20. vars_files:
  21. - user_list.yml
  22. - locker.yml
  23. tasks:
  24. - name: Create a group
  25. group:
  26. name: opsmgr
  27. state: present
  28. - name: Create a user
  29. user:
  30. name: "{{ item.name }}"
  31. groups: opsmgr
  32. comment: "{{ item.job }}"
  33. password: "{{ pw_manager | password_hash('sha512') }}"
  34. when: item.job == 'manager'
  35. loop: "{{users}}"

更新Ansible库的密码

  1. [greg@bastion ansible]$ ansible-vault rekey salaries.yml
  2. Vault password:
  3. New Vault password:
  4. Confirm New Vault password:
  5. Rekey successful
  6. [greg@bastion ansible]$

配置Selinux

  1. [greg@bastion ansible]$ cp -r /usr/share/ansible/roles/rhel-system-roles.selinux/ roles/selinux
  2. [greg@bastion ansible]$ ls
  3. [greg@bastion ansible]$ cp /usr/share/doc/rhel-system-roles/selinux/example-selinux-playbook.yml selinux.yml
  4. [greg@bastion ansible]$ ls

配置Cron任务

  1. [greg@bastion ansible]$ cat cron.yml
  2. ---
  3. - hosts: all
  4. tasks:
  5. - name: Create a user for cron task
  6. user:
  7. name: natasha
  8. state: present
  9. - name: Create a cron task
  10. cron:
  11. name: logger
  12. minute: '*/2'
  13. user: natasha
  14. job: logger "EX200 in progress"
  15. [greg@bastion ansible]$