环境说明

name ip 备注
ansible 192.168.200.3 用来执行ansible的主机
controller 192.168.200.23 管理节点
compute 192.168.200.6 计算节点

题目要求:

使用OpenStack平台创建三台云主机进行实验,云主机镜像使用提供的CentOS_7.5_x86_64_XD.qcow2镜像,Ansible节点flavor使用2核/4G内存/40G硬盘;controller节点flavor使用4核/8G内存/100G硬盘;compute接点flavor使用4核/8G内存/100G硬盘+50G临时磁盘。节点规划表中的IP地址为作者的IP地址,在进行实操案例的时候,按照自己的环境规划网络与IP地址。Ansible节点安装好Ansible服务。然后做好Ansible节点对controller和compute节点的无秘钥访问操作。

1. 配置ansible无秘钥登录

配置Ansible节点无秘钥登录controller和compute节点。配置完无秘钥登录后,使用Ansible节点ssh连接测试。(若云主机已是无秘钥访问的,则不用配置无秘钥)

在ansible的工作目录下/root/openstack(默认情况是/etc/ansible)下设置:

  1. [root@ansible openstack]# cat ansible.cfg
  2. [defaults]
  3. inventory = /root/openstack/hosts
  4. remote_user = root
  5. host_key_checking = False
  6. [root@ansible openstack]# cat hosts
  7. [controller]
  8. controller ansible_user=root ansible_ssh_pass=000000
  9. [compute]
  10. compute ansible_user=root ansible_ssh_pass=000000

2.创建角色目录

安装好ansible在工作目录下创建roles目录并进入

(1)创建各个组件的roles

  1. [root@ansible roles]# ansible-galaxy init init #此文件用来初始化
  2. [root@ansible roles]# ansible-galaxy init mysql
  3. [root@ansible roles]# ansible-galaxy init keystone
  4. [root@ansible roles]# ansible-galaxy init glance
  5. [root@ansible roles]# ansible-galaxy init nova-controller
  6. [root@ansible roles]# ansible-galaxy init nova-compute
  7. [root@ansible roles]# ansible-galaxy init neutron-controller
  8. [root@ansible roles]# ansible-galaxy init neutron-compute
  9. [root@ansible roles]# ansible-galaxy init cinder-controller
  10. [root@ansible roles]# ansible-galaxy init cinder-compute
  11. [root@ansible roles]# ansible-galaxy init swift-controller
  12. [root@ansible roles]# ansible-galaxy init swift-compute
  13. [root@ansible roles]# ansible-galaxy init dashboard
  14. [root@ansible roles]# ansible-galaxy init

image.png

(2)配置init角色

init角色是关键的一环要谨慎,进入init这个角色进行添加所需的模板文件和repo文件
下面这个文件是作为模板文件里面的值作为变量然后在group_vars内的all文件赋值

  1. [root@ansible init]# ls
  2. defaults files handlers meta README.md tasks templates tests vars
  3. [root@ansible init]# ls templates/ #将这个模板文件存放在这里并改为j2
  4. openrc.sh.j2

(2.1)openrc.sh.j2文件

  1. # vi openrc.sh.j2
  2. #--------------------system Config--------------------##
  3. #Controller Server Manager IP. example:x.x.x.x
  4. HOST_IP={{controller_ip}}
  5. #Controller HOST Password. example:000000
  6. HOST_PASS={{password}}
  7. #Controller Server hostname. example:controller
  8. HOST_NAME={{controller_name}}
  9. #Compute Node Manager IP. example:x.x.x.x
  10. HOST_IP_NODE={{compute_ip}}
  11. #Compute HOST Password. example:000000
  12. HOST_PASS_NODE={{password}}
  13. #Compute Node hostname. example:compute
  14. HOST_NAME_NODE={{compute_name}}
  15. #--------------------Chrony Config-------------------##
  16. #Controller network segment IP. example:x.x.0.0/16(x.x.x.0/24)
  17. network_segment_IP={{network_segment_ip}}/24
  18. #--------------------Rabbit Config ------------------##
  19. #user for rabbit. example:openstack
  20. RABBIT_USER=openstack
  21. #Password for rabbit user .example:000000
  22. RABBIT_PASS={{password}}
  23. #--------------------MySQL Config---------------------##
  24. #Password for MySQL root user . exmaple:000000
  25. DB_PASS={{password}}
  26. #--------------------Keystone Config------------------##
  27. #Password for Keystore admin user. exmaple:000000
  28. DOMAIN_NAME=demo
  29. ADMIN_PASS={{password}}
  30. DEMO_PASS={{password}}
  31. #Password for Mysql keystore user. exmaple:000000
  32. KEYSTONE_DBPASS={{password}}
  33. #--------------------Glance Config--------------------##
  34. #Password for Mysql glance user. exmaple:000000
  35. GLANCE_DBPASS={{password}}
  36. #Password for Keystore glance user. exmaple:000000
  37. GLANCE_PASS={{password}}
  38. #--------------------Nova Config----------------------##
  39. #Password for Mysql nova user. exmaple:000000
  40. NOVA_DBPASS={{password}}
  41. #Password for Keystore nova user. exmaple:000000
  42. NOVA_PASS={{password}}
  43. #--------------------Neturon Config-------------------##
  44. #Password for Mysql neutron user. exmaple:000000
  45. NEUTRON_DBPASS={{password}}
  46. #Password for Keystore neutron user. exmaple:000000
  47. NEUTRON_PASS={{password}}
  48. #metadata secret for neutron. exmaple:000000
  49. METADATA_SECRET={{password}}
  50. #Tunnel Network Interface. example:x.x.x.x
  51. #INTERFACE_IP=
  52. {% if ansible_fqdn == "controller" %}
  53. INTERFACE_IP={{controller_ip}}
  54. {% elif ansible_fqdn == "compute" %}
  55. INTERFACE_IP={{compute_ip}}
  56. {% endif %}
  57. #External Network Interface. example:eth1
  58. INTERFACE_NAME={{external_network}}
  59. #External Network The Physical Adapter. example:provider
  60. Physical_NAME={{physical_name}}
  61. #First Vlan ID in VLAN RANGE for VLAN Network. exmaple:101
  62. minvlan=2
  63. #Last Vlan ID in VLAN RANGE for VLAN Network. example:200
  64. maxvlan=200
  65. #--------------------Cinder Config--------------------##
  66. #Password for Mysql cinder user. exmaple:000000
  67. CINDER_DBPASS={{password}}
  68. #Password for Keystore cinder user. exmaple:000000
  69. CINDER_PASS={{password}}
  70. #Cinder Block Disk. example:md126p3
  71. BLOCK_DISK={{cider_name}}
  72. #--------------------Swift Config---------------------##
  73. #Password for Keystore swift user. exmaple:000000
  74. SWIFT_PASS={{password}}
  75. #The NODE Object Disk for Swift. example:md126p4.
  76. OBJECT_DISK={{swift_name}}
  77. #The NODE IP for Swift Storage Network. example:x.x.x.x.
  78. STORAGE_LOCAL_NET_IP={{storage_local_net_ip}}
  79. #--------------------Heat Config----------------------##
  80. #Password for Mysql heat user. exmaple:000000
  81. HEAT_DBPASS={{password}}
  82. #Password for Keystore heat user. exmaple:000000
  83. HEAT_PASS={{password}}
  84. #--------------------Zun Config-----------------------##
  85. #Password for Mysql Zun user. exmaple:000000
  86. ZUN_DBPASS={{password}}
  87. #Password for Keystore Zun user. exmaple:000000
  88. ZUN_PASS={{password}}
  89. #Password for Mysql Kuryr user. exmaple:000000
  90. KURYR_DBPASS={{password}}
  91. #Password for Keystore Kuryr user. exmaple:000000
  92. KURYR_PASS={{password}}
  93. #--------------------Ceilometer Config----------------##
  94. #Password for Gnocchi ceilometer user. exmaple:000000
  95. CEILOMETER_DBPASS={{password}}
  96. #Password for Keystore ceilometer user. exmaple:000000
  97. CEILOMETER_PASS={{password}}
  98. #--------------------AODH Config----------------##
  99. #Password for Mysql AODH user. exmaple:000000
  100. AODH_DBPASS={{password}}
  101. #Password for Keystore AODH user. exmaple:000000
  102. AODH_PASS={{password}}
  103. #--------------------Barbican Config----------------##
  104. #Password for Mysql Barbican user. exmaple:000000
  105. BARBICAN_DBPASS={{password}}
  106. #Password for Keystore Barbican user. exmaple:000000
  107. BARBICAN_PASS={{password}}

(2.2)存放变量的文件

  1. [root@ansible openstack]# cd group_vars/
  2. [root@ansible group_vars]# cat all
  3. controller_ip: 192.168.200.23
  4. controller_name: controller
  5. compute_ip: 192.168.200.6
  6. compute_name: compute
  7. password: '000000'
  8. cider_name: vda1
  9. swift_name: vdb
  10. network_segment_ip: 192.168.200.0
  11. external_network: eth1
  12. physical_name: provider
  13. storage_local_net_ip: 192.168.200.3

3.编写各个角色的playbook

(3.1)init的main文件

  1. [root@ansible roles]# cd init/tasks/
  2. [root@ansible tasks]# cat main.yml
  3. - name: remote old repo
  4. shell: 'rm -rf /etc/yum.repos.d/*'
  5. - name: add new repo
  6. copy: src=/root/xiandian.repo dest=/etc/yum.repos.d/
  7. - name: yum install iaas-xiandian
  8. yum: name=iaas-xiandian
  9. - name: template openrc.sh
  10. template: src=openrc.sh.j2 dest=/etc/xiandian/openrc.sh
  11. - name: install iaas-pre-host.sh
  12. shell: iaas-pre-host.sh

(3.2)glance的main文件

  1. [root@ansible roles]# cd glance/tasks/
  2. [root@ansible tasks]# cat main.yml
  3. - name: install glance
  4. shell: iaas-install-glance.sh

(3.3)mysql的main文件

  1. [root@ansible roles]# cd mariadb/tasks/
  2. [root@ansible tasks]# cat main.yml
  3. - name: install mariadb
  4. shell: iaas-install-mysql.sh

(3.4)keystone的main文件

  1. root@ansible roles]# cd keyston/tasks/
  2. [root@ansible tasks]# cat main.yml
  3. - name: install keystone
  4. shell: iaas-install-keystone.sh

(3.5)nova的main文件

  1. [root@ansible roles]# cat nova-controller/tasks/main.yml
  2. - name: install nova-controller
  3. shell: iaas-install-nova-controller.sh
  4. [root@ansible roles]# cat nova-compute/tasks/main.yml
  5. - name: install nova-compute
  6. shell: iaas-install-nova-compute.sh

(3.6)neutron的main文件

  1. [root@ansible roles]# cat neutron-controller/tasks/main.yml
  2. - name: install neutron-controller
  3. shell: iaas-install-neutron-controller.sh
  4. [root@ansible roles]# cat neutron-compute/tasks/main.yml
  5. - name: install neutron-compute
  6. shell: iaas-install-neutron-compute.sh

(3.7)cinder的main文件

  1. [root@ansible roles]# cat cinder-controller/tasks/main.yml
  2. - name: install cinder-controller
  3. shell: iaas-install-cinder-controller.sh
  4. [root@ansible roles]# cat cinder-compute/tasks/main.yml
  5. - name: install cinder-compute
  6. shell: iaas-install-cinder-compute.sh

(3.8)swift的main文件

  1. [root@ansible roles]# cat swift-controller/tasks/main.yml
  2. - name: install swift-controller
  3. shell: iaas-install-swift-controller.sh
  4. [root@ansible roles]# cat swift-compute/tasks/main.yml
  5. - name: install swift-compute
  6. shell: iaas-install-swift-compute.sh

(3.9)heat的main文件

  1. [root@ansible roles]# cat heat/tasks/main.yml
  2. - name: instal heat
  3. shell: iaas-install-heat.sh

(3.10)dashboard的main文件

  1. [root@ansible roles]# cat dashboard/tasks/main.yml
  2. - name: install dashboard
  3. shell: iaas-install-dashboard.sh

4.编写引入文件

  1. [root@ansible openstack]# ll
  2. total 12
  3. -rw-r--r-- 1 root root 95 Nov 11 00:13 ansible.cfg
  4. drwxr-xr-x 2 root root 17 Nov 10 23:44 group_vars
  5. -rw-r--r-- 1 root root 129 Nov 10 21:18 hosts
  6. -rw-r--r-- 1 root root 337 Nov 11 01:05 install_openstack.yml
  7. drwxr-xr-x 16 root root 275 Nov 10 21:14 roles
  8. [root@ansible openstack]# cat install_openstack.yml
  9. ---
  10. - hosts: controller
  11. remote_user: root
  12. roles:
  13. - init
  14. - mariadb
  15. - keyston
  16. - glance
  17. - nova-controller
  18. - neutron-controller
  19. - dashboard
  20. - cinder-controller
  21. - swift-controller
  22. - heat
  23. - hosts: compute
  24. remote_user: root
  25. roles:
  26. - init
  27. - nova-compute
  28. - neutron-compute
  29. - cinder-compute

5.目录结构:

最终的目录结构为这样:

  1. [root@ansible openstack]# tree
  2. .
  3. ├── ansible.cfg
  4. ├── group_vars
  5. └── all
  6. ├── hosts
  7. ├── install_openstack.yml
  8. └── roles #下面的目录我删除了一些不然太长了
  9. ├── cinder-compute
  10. ├── defaults
  11. └── main.yml
  12. └── vars
  13. └── main.yml
  14. ├── cinder-controller
  15. ├── default
  16. └── main.yml
  17. ├── dashboard
  18. ├── defaults
  19. └── vars
  20. └── main.yml
  21. ├── glance
  22. ├── defaults
  23. └── main.yml
  24. └── vars
  25. └── main.yml
  26. ├── heat
  27. ├── defaults
  28. └── main.yml
  29. └── vars
  30. └── main.yml
  31. ├── init
  32. ├── defaults
  33. └── main.yml
  34. ├── keyston
  35. ├── defaults
  36. └── main.yml
  37. ├── mariadb
  38. ├── defaults
  39. └── main.yml
  40. ├── neutron-compute
  41. ├── defaults
  42. └── main.yml
  43. ├── neutron-controller
  44. ├── defaults
  45. └── main.yml
  46. ├── nova-compute
  47. ├── defaults
  48. └── main.yml
  49. ├── nova-controller
  50. ├── defaults
  51. └── main.yml
  52. ├── swift-compute
  53. ├── defaults
  54. └── main.yml
  55. └── swift-controller
  56. ├── defaults
  57. └── main.yml
  58. └── main.yml

6.验证,执行

  1. [root@ansible openstack]# ansible-playbook --syntax-check install_openstack.yml #验证
  2. [WARNING]: Found both group and host with same name: controller
  3. [WARNING]: Found both group and host with same name: compute
  4. playbook: install_openstack.yml
  5. [root@ansible openstack]# ansible-playbook install_openstack.yml #执行

…………………….
……………………………..
……………………………………其实我没做出来

疑问?

  1. 主机第二块网卡
  2. compute的磁盘
  3. 在init这个角色内运行iaas-pre-host.sh 这个脚本后重启问题