ansible中when条件表达式in的错误

项目场景:

创建三台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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OLoyy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值