安装和配置Ansible
安装Ansible
[root@bastion ~]#安装Ansible服务[root@bastion ~]yum install ansible.noarch -y
配置Ansible
[greg@bastion ~]$ # 定义静态清单文件[greg@bastion ~]$ vim ./ansible/inventory[greg@bastion ~]$ cat ./ansible/inventory[dev]172.25.250.9[test]172.25.250.10[prod]172.25.250.11172.25.250.12[balancers]172.25.250.13[webservers:children]prod[greg@bastion ansible]$ cat ansible.cfg[defaults]inventory = /home/greg/ansible/inventoryroles_path = /home/greg/ansible/roles[privilege_escalation]become=Truebecome_method=sudobecome_user=root[greg@bastion ansible]$ mkdir /home/greg/ansible/roles[greg@bastion ansible]$ #可通过Ansible默认的配置文件来参考,路径为/etc/ansible/ansible.cfg
配置yum源
[greg@bastion ansible]$ cat /home/greg/ansible/adhoc.sh#!/bin/bashansible 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 "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 "[greg@bastion ansible]$ chmod +x /home/greg/ansible/adhoc.sh
安装软件包
---- hosts: [dev,test,prod]tasks:- name: Install php and mariadb servicesyum:name: php,mariadbstate: installed- hosts: [dev]tasks:- name: Install RPM Development Toolsyum:name: '@RPM Development Tools'state: installed- name: Update the service versionyum:name: '*'state: latest
使用RHEL系统角色之 rhel-system-roles.timesync
#按照题目要求将系统rhel-system-roles.timesync角色内容复制到题目指定位置[greg@bastion roles]$ cp -r /usr/share/ansible/roles/rhel-system-roles.timesync/ /home/greg/ansible/roles/timesync[greg@bastion ansible]$ cat timesync.yml---- hosts: allvars:timesync_ntp_servers:- hostname: 172.25.250.250pool: yesiburst: yesroles:- role: timesync[greg@bastion ansible]$ #运行playbook[greg@bastion ansible]$ ansible-playbook timesync.yml
使用Ansible Galaxy安装角色
#通过角色文件安装[greg@bastion ansible]$ ansible-galaxy install -r roles/requirements.yml -p roles[greg@bastion roles]$ cat requirements.yml---- src: http://host.domain8.example.com/ex300/haproxy.tar.gzname: balancer- src: http://host.domain8.example.com/ex300/phpinfo.tar.gzname: phpinfo[greg@bastion roles]$#查看已下载角色[greg@bastion ansible]$ ansible-galaxy list# /home/greg/ansible/roles- timesync, (unknown version)- balancer, (unknown version)- phpinfo, (unknown version)[greg@bastion ansible]$
通过角色创建http服务
#在roles目录下创建apache角色[greg@bastion roles]$ ansible-galaxy init apache- apache was created successfully[greg@bastion roles]$ lsapache balancer requirements.yml timesync[greg@bastion roles]$#编写task文件[greg@bastion apache]$ cd tasks/[greg@bastion tasks]$ cat main.yml---# tasks file for apache- name: Install httpd serviceyum:name: httpdstate: installed- name: Enable httpd serviceservice:name: httpdstate: startedenabled: true- name: Enable firewall serviceservice:name: firewalldstate: startedenabled: true- name: Enable http service in firewallfirewalld:service: httpimmediate: yespermanent: yesstate: enabled- name: Copy http index.htmltemplate:src: index.html.j2dest: /var/www/html/index.html[greg@bastion tasks]$# 创建j2文件[greg@bastion apache]$[greg@bastion apache]$ cd ./templates/[greg@bastion templates]$ cat index.html.j2Welcome to {{ansible_fqdn}} on {{ansible_default_ipv4.address}}[greg@bastion templates]$# 创建playbook[greg@bastion ansible]$ cat newroles.yml---- hosts: webserversname: Init http serviceroles:- role: apache[greg@bastion ansible]$
从Ansible Galaxy中使用角色
[greg@bastion ansible]$ cat roles.yml---- hosts: all- hosts: balancersroles:- role: balancer- hosts: webserversroles:- role: phpinfo
创建和使用逻辑卷
[greg@bastion ansible]$ cat /home/greg/ansible/lv.yml---- hosts: lvgrouptasks:- name: volume exsit or notshell: 'vgdisplay research'register: exsit_or_notignore_errors: yes- name: if not exsit, will display the messagefail:msg: "Volume group done not exist"when: exsit_or_not.rc != 0- name: if exsit, will create the lvblock:- name: Create the lv for 1500Mlvol:vg: researchlv: datasize: 1500mrescue:- name: Display the message,if does not have enough spacesdebug:msg: "Could not create logical volume of that size"- name: Create the lv for 800Mlvol:vg: researchlv: datasize: 800m- name: Format the lv for ext4filesystem:dev: /dev/research/datafstype: ext4force: yes
创建并使用磁盘分区
[greg@bastion ansible]$ cat partition.yml---- hosts: diskgrouptasks:- name: Disk exist or notshell: 'ls /dev/vdb'register: disk_exist_or_notignore_errors: yes- name: If disk does not exsit, will display messagefail:msg: "disk does not exist"when: disk_exist_or_not.rc != 0- name: If disk exsit, Create a main partitionblock:- name: Create a main partition for 1500Mparted:device: /dev/vdbnumber: 1part_end: 1500MiBstate: presentrescue:- name: Can not create a 1500M partitiondebug:msg: 'could not create partation of that size'- name: Create a main partition for 800Mparted:device: /dev/vdbnumber: 1part_end: 800MiBstate: present- name: Format the partitionfilesystem:dev: /dev/vdb1fstype: ext4force: yes- name: Mount the partitionmount:src: /dev/vdb1path: /newpartfstype: ext4state: mounted
生成主机文件
[greg@bastion ansible]$ cat hosts.j2127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6{% for host in groups['all'] %}{{ hostvars[host]['ansible_default_ipv4']['address']}} {{ hostvars[host]['ansible_fqdn'] }} {{ hostvars[host]['ansible_hostname'] }}{% endfor %}[greg@bastion ansible]$[greg@bastion ansible]$ cat hosts.yml---- hosts: all- hosts: devtasks:- name: Generate the host file of servertemplate:src: /home/greg/ansible/hosts.j2dest: /etc/myhosts[greg@bastion ansible]$
修改文件内容
[greg@bastion ansible]$ cat issue.yml---- hosts: alltasks:- name: For devcopy:content: "Development\n"dest: /etc/issuewhen: inventory_hostname in groups['dev']- name: For testcopy:content: "Test\n"dest: /etc/issuewhen: inventory_hostname in groups['test']- name: For productioncopy:content: "Production\n"dest: /etc/issuewhen: inventory_hostname in groups['prod'][greg@bastion ansible]$
创建Web内容目录
[greg@bastion ansible]$ cat webcontent.yml---- hosts: devroles:- role: apachetasks:- name: Create groupgroup:name: webdev- name: Create Directoryfile:path: /webdevgroup: webdevstate: directorymode: 2775setype: httpd_sys_content_t- name: Create softlinkfile:src: /webdevdest: /var/www/html/webdevstate: link- name: Create a filecopy:content: "Development\n"dest: /webdev/index.htmlsetype: httpd_sys_content_t[greg@bastion ansible]$
生成硬件报告
[greg@bastion ansible]$ cat hwreport.yml---- hosts: alltasks:- name: Remove the file, if existshell: "rm -rf /root/hwreport.txt"- name: Download the fileget_url:url: "http://host/ex300/hwreport.empty"dest: /root/hwreport.txt- name: Edit the value of the HOSTlineinfile:path: /root/hwreport.txtregexp: '^HOST'line: 'HOST={{inventory_hostname}}'- name: Edit the value of the MEMORYlineinfile:path: /root/hwreport.txtregexp: '^MEMORY'line: 'MEMORY={{ansible_memtotal_mb}}MB'- name: Edit the value of the BIOSlineinfile:path: /root/hwreport.txtregexp: '^BIOS'line: 'BIOS={{ansible_bios_version}}'- name: Edit the value of the DISK_SIZE_VDAlineinfile:path: /root/hwreport.txtregexp: '^DISK_SIZE_VDA'line: 'DISK_SIZE_VDA={{ansible_devices.vda.size}}'- name: Edit the value of the DISK_SIZE_VDBblock:- name: If DISK VDB existlineinfile:path: /root/hwreport.txtregexp: '^DISK_SIZE_VDB'line: 'DISK_SIZE_VDB={{ansible_devices.vdb.size}}'rescue:- name: If DISK VDB does not existlineinfile:path: /root/hwreport.txtregexp: '^DISK_SIZE_VDB'line: 'DISK_SIZE_VDB=NONE'[greg@bastion ansible]$[greg@bastion ansible]$[greg@bastion ansible]$[greg@bastion ansible]$
创建密码库
[greg@bastion ansible]$ cat /home/greg/ansible/secret.txtredhat[greg@bastion ansible]$ cat locker.ymlpw_developer: redhatpw_manager: redhat[greg@bastion ansible]$ ansible-vault encrypt --vault-id=secret.txt locker.ymlEncryption successful[greg@bastion ansible]$ ansible-vault view --vault-id=secret.txt locker.ymlpw_developer: redhatpw_manager: redhat[greg@bastion ansible]$ cat locker.yml$ANSIBLE_VAULT;1.1;AES256313430353335666532626534613538393163653739373939613437323864313862623738383238306163323965323630346330346461623362663964623666610a343536653564303464373535376530356230333533306636666132343635396566666331326565316633336465323639356431643136333636653166373039620a376536623764643137346333646335383139633039353063356162663466666438306239333366343365303661623663636338613334653935323032353763636432383262626436363666633533396461383264346635396136343130383134[greg@bastion ansible]$
创建用户账号
---- hosts: [dev,test]vars_files:- user_list.yml- locker.ymltasks:- name: Create a groupgroup:name: devopsstate: present- name: Create a useruser:name: "{{ item.name }}"comment: "{{ item.job }}"groups: devopspassword: "{{ pw_developer | password_hash('sha512') }}"when: item.job == 'developer'loop: "{{users}}"- hosts: [prod]vars_files:- user_list.yml- locker.ymltasks:- name: Create a groupgroup:name: opsmgrstate: present- name: Create a useruser:name: "{{ item.name }}"groups: opsmgrcomment: "{{ item.job }}"password: "{{ pw_manager | password_hash('sha512') }}"when: item.job == 'manager'loop: "{{users}}"
更新Ansible库的密码
[greg@bastion ansible]$ ansible-vault rekey salaries.ymlVault password:New Vault password:Confirm New Vault password:Rekey successful[greg@bastion ansible]$
配置Selinux
[greg@bastion ansible]$ cp -r /usr/share/ansible/roles/rhel-system-roles.selinux/ roles/selinux[greg@bastion ansible]$ ls[greg@bastion ansible]$ cp /usr/share/doc/rhel-system-roles/selinux/example-selinux-playbook.yml selinux.yml[greg@bastion ansible]$ ls
配置Cron任务
[greg@bastion ansible]$ cat cron.yml---- hosts: alltasks:- name: Create a user for cron taskuser:name: natashastate: present- name: Create a cron taskcron:name: loggerminute: '*/2'user: natashajob: logger "EX200 in progress"[greg@bastion ansible]$
