创建三台RedHat虚拟主机,配置ip,本地yum(rhel-8.2-x86_64-dvd.iso ),关闭防火墙和SELinux;方便管理分别命名为ansible,node1,node2。安装ansible_soft.tar.gz,配置ansible,主机清单如下
[test]
node[1:2]
利用alice(普通用户,权限为alice ALL=(ALL) NOPASSWD:ALL
)角色解决此问题。
创建名叫hard的角色,该角色可以安装、启动、开机自启数据库mariadb服务,还准备了动态文件xyz.j2,
里面是变量“主机名-ip”的格式,调用hard角色就能将这个文件拷贝到被控主机的/root中并命名xyz.txt,但需要配合when判断,只有主机名包含"node"字符串的主机才拷贝。
Playbook(剧本)写到需要配合when判断,只有主机名包含"node"字符串的主机才拷贝。
问题出现了。
[alice@ansible ansible]$ sudo ansible-playbook inst_mariadb_tem.yml
ERROR! Syntax Error while loading YAML.
did not find expected key
The error appears to be in '/root/ansible/roles/hard/tasks/main.yml': line 16, column 16, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
# when: "'node' in ansible_hostname"
when: 'node' in ansible_hostname
^ here
This one looks easy to fix. It seems that there is a value started
with a quote, and the YAML parser is expecting to see the line ended
with the same kind of quote. For instance:
when: "ok" in result.stdout
Could be written as:
when: '"ok" in result.stdout'
Or equivalently:
when: "'ok' in result.stdout"
此时的roles/hard/tasks/main.yml文件中的引起错误
when: 'node' in ansible_hostname
按照报错提示改为
when: "'node' in ansible_hostname.stdout"
以为错误改完了,没想到
fatal: [node2]: FAILED! => {"msg": "The conditional check ''node' in ansible_hostname.stdout' failed.
The error was: error while evaluating conditional ('node' in ansible_hostname.stdout):
Unable to look up a name or access an attribute in template string
({% if 'node' in ansible_hostname.stdout %} True {% else %} False {% endif %}).\n
Make sure your variable name does not contain invalid characters like '-':
argument of type 'AnsibleUndefined' is not iterable\n\nThe error appears to be in
'/root/ansible/roles/hard/tasks/main.yml': line 11, column 3,
but may\nbe elsewhere in the file depending on the exact syntax problem.\n\n
The offending line appears to be:\n\n
enabled: yes\n- name: 拷贝到被控主机的/root中并命名xyz.txt\n ^ here\n"}
我已经要崩溃了,这个错误已经改了两个小时了,🤯🤯🤯!!!!!!!!!!
开始找关于ansible when条件的资料,Ansible 系列之条件判断,种种表明我的是根据他的写的,之后也是根据报错改的,但是但是但是但是就是不对
。
濒临放弃的时候将.stdout删除,竟然竟然竟然对了
when: "'node' in ansible_hostname"
第一次时距离答案差一对双引号
,
第二次时距离答案多.stdout
.
绝望,绝望,绝望😱😱😱。
其实到目前我都没有真正的理解为什么是这样的,现在只能归咎为自己关于ansible的when条件的理解,练习,语法不知道。一定要认真学习,认真学习,不要认为自己会点就可以不走心,不同的环境,设备版本都会有区别
不抛弃不放弃哦,加油!
这题的完整解答为
[root@ansible ansible]$ sudo ansible-galaxy init roles/hard
[root@ansible ansible]$ sudo vim roles/hard/templates/xyz.j2
{{ansible_hostname}}-{{ansible_all_ipv4_addresses}}
[root@ansible ansible]$ sudo vim roles/hard/tasks/main.yml
---
# tasks file for roles/hard
- name: install mariadb and mariadb-server
yum:
name: mariadb-server
- name: start and enabled service
service:
name: mariadb
state: started
enabled: yes
- name: 拷贝到被控主机的/root中并命名xyz.txt
template:
src: xyz.j2
dest: /root/xyz.txt
when: "'node' in ansible_hostname"
[alice@ansible ansible]$ sudo vim inst_mariadb_tem.yml
---
- name: homework
hosts: test
roles:
- hard
[alice@ansible ansible]$ sudo ansible-playbook inst_mariadb_tem.yml