Asible最佳实践-进阶版-RHCA447 -------定义角组变量/主机变量/变量文件
6.1 所有受管节点设置sudo免密
[root@libin libin]# vim /etc/sudoers.d/devops libin ALL=(ALL) NOPASSWD:ALL [root@libin sudoers.d]# scp devops 192.168.124.134:`pwd` |
6.2 自定义ansible目录
[root@libin ansible]# whoami root [root@libin ansible]# su - libin [libin@libin ~]$ mkdir ansible [libin@libin ~]$ cd ansible/ [libin@libin ansible]$ vim ansible.cfg [defaults] inventory = /home/libin/ansible/inventory remote_user = libin remote_ask_pass = false
[inventory] enable_plugins = host_list, virtualbox, yaml, constructed, ini
[privilege_escalation] become=True become_method=sudo become_user=root become_ask_pass=False
[libin@libin ansible]$ cat /etc/ansible/ansible.cfg | grep privi -A5 注:通过找到privi 看它下面的5行
[libin@libin ansible]$ grep -E ^#en /etc/ansible/ansible.cfg #enable_plugins = host_list, virtualbox, yaml, constructed 注: 查看inventory默认支持清单的格式 |
6.3自定义inventory清单ini的格式并为分组定义变量
[libin@libin ansible]$ pwd /home/libin/ansible
[libin@libin ansible]$ vim inventory [web] 192.168.124.132 192.168.124.134
[db] libin.com username=libin ourse_info=DO447 #usernam/ourse_info为libin.com定义变量 libin3.com course_day=12 #course_day为libin3.com 定义变量
[alllibin:children] #定义子组 web db
[web:vars] #为web组定义变量 corse_info=DO447
[libin@libin ansible]$ ansible web -m ping 192.168.124.132 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } 192.168.124.134 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong"
[libin@libin ansible]$ ansible db -m ping libin.com | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } libin3.com | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } |
6.4 查看yaml格式的inventory清单
[libin@libin ansible]$ ansible-inventory --yaml --list -i inventory all: children: alllibin: children: db: hosts: libin.com: username: libin libin3.com: course_day: 12 web: hosts: 192.168.124.132: course_info: DO447 192.168.124.134: course_info: DO447 ungrouped: {}
|
6.5 查看默认清单
[libin@libin ansible]$ ansible-inventory --graph @all: |--@alllibin: | |--@db: | | |--libin.com | | |--libin3.com | |--@web: | | |--192.168.124.132 | | |--192.168.124.134 |--@ungrouped: |
6.6 设置编辑yaml格式剧本时敲TAB的跳跃格式
[libin@libin ansible]$ vim ~/.vimrc autocmd FileType yaml setlocal ai ts=2 sw=2 et |
6.7 自定义inventory清单yaml的格式并定义受管节点和全局变量
course_info: ansible_best_pratic #为全局变量
course_day: 12 #为全局变量
[libin@libin ansible]$ vim hosts
web: hosts: libin.com: username: libin #为libin.com定义变量 libin3.com: course_day: 12 # 为libin3.com定义变量 db: hosts: 192.168.124.132: 192.168.124.134: alllibin: children: web: db: vars: course_info: ansible_best_pratic course_day: 12
[libin@libin ansible]$ vim ansible.cfg inventory = /home/libin/ansible/hosts #如果使用yaml格式清单hosts
[libin@libin ansible]$ vim hosts [libin@libin ansible]$ ansible-inventory --graph #查看默认清单 @all: |--@alllibin: | |--@db: | | |--192.168.124.132 | | |--192.168.124.134 | |--@web: | | |--libin.com | | |--libin3.com |--@ungrouped:
|
6.8 查看清单
-i:可以指定hosts清单,可以定义多个hosts清单名字随意,不-i就是默认清单
[libin@libin ansible]$ ansible -i hosts --list-hosts web hosts (2): libin3.com libin.com [libin@libin ansible]$ ansible -i hosts --list-hosts db hosts (2): 192.168.124.134 192.168.124.132 [libin@libin ansible]$ ansible -i hosts --list-hosts alllibin hosts (4): libin3.com libin.com 192.168.124.134 192.168.124.132 |
6.9 playbook编写格式,使用debug显示我们定义的变量信息
name: 名字(随意)
hosts: 清单
task: 任务
为必备的playbook格式
实战1: [libin@libin ansible]$ pwd /home/libin/ansible
[libin@libin ansible]$ vim libin1.yaml
--- - name: test var for libin hosts: web #我指定web组清单 tasks: - name: debug #如果要显示多个debug信息我们需要写2个debug debug: msg: "{{ course_info }}" #course_info这为全局变量,web组肯定都有 - name: debug debug: msg: "{{ course_day }}" #course_day 这为全局变量,web组肯定都有
[libin@libin ansible]$ ansible-playbook libin1.yaml
实战2:指定inventory清单运行剧本(因为我这里默认清单为hosts)
[libin@libin ansible]$ vim libin1-1.yaml
--- - name: test var for libin hosts: web tasks: - name: debug debug: msg: "{{ course_info }}"
[libin@libin ansible]$ ansible-playbook -i inventory libin1-1.yaml
|
6.10 将inventory 清单ini格式转化为yaml格式
[libin@libin ansible]$ ansible-inventory --yaml --list -i inventory all: children: alllibin: children: db: hosts: libin.com: course_info: DO447 username: libin libin3.com: course_day: 12 web: hosts: 192.168.124.132: course_info: DO447 192.168.124.134: course_info: DO447 ungrouped: {} |
6.11 将inventory 清单ini格式转化为yaml格式并输出重命名清单
--output :输出
[libin@libin ansible]$ ansible-inventory --yaml --list -i inventory --output test1
|
6.12 直接使用转化后的格式清单
[libin@libin ansible]$ ansible-playbook -i test1 libin1-1.yaml
[libin@libin ansible]$ ansible-inventory --graph -i test1 @all: |--@alllibin: | |--@db: | | |--libin.com | | |--libin3.com | |--@web: | | |--192.168.124.132 | | |--192.168.124.134 |--@ungrouped: |
6.13 小结
1.在资产清单中定义变量(yaml格式)6.7 2.ansible-inventory --yaml --list -i inventory --output test1 #通过ini格式输出转化成yaml并且保存到另外一个制定文件中 3.ansible-inventory --graph #查看默认清单 (我这里默认有进行调整,展示可能不同,读者不要被误解) 4. ansible-inventory --graph -i test1 #查看制定的清单 |
6.14 定义分组与主机变量
[libin@libin ansible]$ mkdir group_vars #创建变量文件夹,有利于更好分类 [libin@libin ansible]$ mkdir host_vars [libin@libin ansible]$ vim group_vars/web #yaml的写法要用冒号: user_list: #用户列表,定义web组变量 - user1 #用户1,也叫第一个元素 - user2 #用户2
[libin@libin ansible]$ vim group_vars/db database_version: mysql
[libin@libin ansible]$ vim host_vars/libin.com #定义主机变量 HOSTNAME: libin.com
[libin@libin ansible]$ vim host_vars/libin3.com HOSTNAME: libin3.com
[libin@libin ansible]$ tree group_vars/ host_vars/ group_vars/ #分组的写法 ├── db #必须要为清单中的分组 └── web host_vars/ #主机的写法,这里不要写hosts否则有问题 ├── libin3.com #必须要为清单中的主机 └── libin.com
0 directories, 4 files
实战1: 我这里的默认清单为hosts(里面包括了libin.com)
#指定libin.com运行剧本,会优先调用我定义的组变量和主机变量 [libin@libin ansible]$ cp libin1-1.yaml libin2-1.yaml
--- - name: test var for libin hosts: libin.com tasks: - name: debug debug: msg: "{{ user_list }} {{ HOSTNAME }}"
[libin@libin ansible]$ ansible-playbook libin2-1.yaml
实战2: hosts: libin.com #我修改为192.168.124.134时与libin3.com变化 192.168.124.134在db组,而我并没有定义user_list变量;libin3.com在web组,定义了web的user_list变量,也定义了HOSTNAME 变量
实战3:只取用户变量的第一个元素[0] 注:如果我只取用户列表的第一个元素时的playbook的写法 [libin@libin ansible]$ vim libin2-1.yaml
--- - name: test var for libin hosts: libin.com tasks: - name: debug debug: msg: "{{ user_list[0] }} {{ HOSTNAME }}"
实战4:192.168.124.134在db组,我定义了db组的变量database_version: mysql [libin@libin ansible]$ vim libin2-1.yaml
--- - name: test var for libin hosts: 192.168.124.134 tasks: - name: debug debug: msg: "{{ database_version }}"
实战5:192.168.124.134主机在db组,我只定义database_version: mysql,没有定义HOSTNAME [libin@libin ansible]$ vim libin2-1.yaml
--- - name: test var for libin hosts: 192.168.124.134 tasks: - name: debug debug: msg: "{{ database_version }} {{ HOSTNAME }}"
(注:这里报错的原因为,我playbook里面的hosts为192.168.124.134,而在我定义的主机变量里面并没有192.168.124.134的HOSTNAME) 解决方案: [libin@libin ansible]$ vim host_vars/192.168.124.134 #写上该主机变量 HOSTNAME: libin3.com
|
6.15 定义变量文件
实战1:全局变量,针对所有任务生效 [libin@libin ansible]$ pwd /home/libin/ansible
这里我的默认清单还是hosts
[libin@libin ansible]$ vim vars_file user_list: - rhca - hunan_changsha
[libin@libin ansible]$ cp libin2-1.yaml libin3-1.yaml [libin@libin ansible]$ vim libin3-1.yaml
--- - name: test var for libin hosts: libin.com vars_files: #使用文档模块定义文档的路径 - vars_file tasks: - name: debug debug: msg: " {{ HOSTNAME }} {{ user_list[0] }}"
注:#libin.com在web组HOSTNAME 单独定了主机变量, user_list[0] 取变量文件变量的第一个元素
实战2: 全局变量,针对所有任务生效 [libin@libin ansible]$ vim libin3-1.yaml
--- - name: test var for libin hosts: libin.com vars_files: - vars_file vars: - rhca_name: libin447 tasks: - name: debug debug: msg: " {{ HOSTNAME }} {{ user_list[0] }}"
- name: debug1 debug: msg: "{{ rhca_name }}"
注:#因为我vars变量模块定义了rhca_name 的变量libin447
实战3: 局部变量设置(标红部分,两种方式定义变量看个人),不要搞混啦 变量设置 set_fact模块
--- - name: test var for libin hosts: libin.com vars_files: - vars_file vars: - rhca_name: libin447 tasks: - name: debug debug: msg: " {{ HOSTNAME }} {{ user_list[0] }}"
- name: debug1 debug: msg: "{{ rhca_name }}"
- name: 变量设置 set_facts模块 set_fact: rhca_name2: "welcom to study rhca447"
- name: debug2 debug: msg: "{{ rhca_name2 }}"
[libin@libin ansible]$ tree . ├── ansible.cfg ├── group_vars │ ├── db │ └── web ├── hosts ├── host_vars │ ├── 192.168.124.134 │ ├── libin3.com │ └── libin.com ├── inventory ├── libin1-1.yaml ├── libin1.yaml ├── libin2-1.yaml ├── libin3-1.yaml ├── test1 └── vars_file
2 directories, 14 files |
6.16小结(role角色了解,传递变量)
6.14定义分组与主机变量 1、变量设置: 2、变量文件夹: group_vars \ host_vars 3、变量文件: vars_file(变量文件,可以随便取) vars_files: #使用文档模块定义文档的路径,这为ansible语法不能写错 4、剧本中局部变量: set_fact模块 5、在角色中定义变量; 角色目录中vars的main.yaml文件,在该文件中定义变量
[libin@libin ansible]$ pwd /home/libin/ansible
[libin@libin ansible]$ yum provides ansible-galaxy #需要装包ansible-galaxy [libin@libin ansible]$ sudo yum install -y ansible-2.9.27-1.el7.noarch
[libin@libin ansible]$ yum search rhel role | grep rhel #安装角色 [libin@libin ansible]$ sudo yum install -y rhel-system-roles.noarch
[libin@libin ansible]$ ansible-galaxy init httpd #初始化httpd - Role httpd was created successfully
角色文件已经定义
[libin@libin ansible]$ cd httpd/ [libin@libin ansible]$ tree httpd/ httpd/ ├── defaults │ └── main.yml ├── files ├── handlers │ └── main.yml ├── meta │ └── main.yml ├── README.md ├── tasks │ └── main.yml ├── templates ├── tests │ ├── inventory │ └── test.yml └── vars #写变量的地方 └── main.yml
6、以ansible-playbook -e的方式传递变量 [libin@libin ansible]$ cp libin2-1.yaml libin4-1.yaml [libin@libin ansible]$ vim libin4-1.yaml
--- - name: test var for libin hosts: 192.168.124.134 tasks: - name: debug debug: msg: "{{ CA_NAME }}"
[libin@libin ansible]$ ansible-playbook -e CA_NAME=RHCA-447 libin4-1.yaml
|
标签:进阶,Asible,--,RHCA447,yaml,ansible,hosts,libin,com From: https://www.cnblogs.com/libin-linux/p/16785893.html