ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
#### 条件判断when中无法使用变量,因为隐式转换,不需要加 ``` # 错误 when: "{{ master_host }} not in ansible_all_ipv4_addresses" # 正确方式 when: master_host not in ansible_all_ipv4_addresses ``` #### shell命令中,双引号的使用 ``` # 错误(会把双引号识别同一个参数,导致cp报错,缺少目的地址) cp -a "{{ go_src_dir }}/go {{ go_install_dir }}/go{{ go_version }}" # 正确 cp -a "{{ go_src_dir }}/go" "{{ go_install_dir }}/go{{ go_version }}" ``` ``` # 错误 (shell多行命令找不到目录) “touch {{ dir }}“ # 正确 touch ”{{ dir }}“ ``` #### 将所操作服务器的IP,初始化到配置中,而不是从主机获取 ``` "{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}" ``` #### 在模板中使用jinja2的循环,会产生很多空格 ##### 增加中横线[感谢Klaus88](https://my.oschina.net/klausgao/blog/364971)的分享 ``` {%- for server_id,node_ip in zk_cluster -%} {%- if node_ip == zk_hostip -%} {{ server_id }} {%- endif -%} {%- endfor -% ``` ## 直接已知hosts中的ip和主机名 inventory设置 ``` ``` ## 使用远程主机的主机名和ip ``` etcd_hostip: "{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}" etcd_hostname: "{{ hostvars[inventory_hostname]['ansible_hostname'] }}" ```