随笔分类 - 运维工具 / ansible
摘要:示例1 可以理解为: for item in groups.ungrouped: print(item) - hosts: all tasks: - name: debug debug: msg: "{{ item }}" with_items: "{{ groups.ungrouped }}" -
阅读全文
摘要:block 可以将多个task任务组合在一起执行,支持when判断,支持类似python中的try 语句。 python 语法举例: try: print(1/0) except: print(2) finally: print(3) block 举例: rescue 字面意识救援 - hosts:
阅读全文
摘要:failed_when 相当于exit,可以理解为: for i in range(10): if i >5: exit else: print(i) - hosts: all tasks: - name: ansible_os_family debug: msg: "{{ ansible_os_f
阅读全文
摘要:| 参数 | 释义 | | | | | string | 判断字符串 | | upper | 判断全大写 | | lower | 判断全小写 | - hosts: all tasks: - name: test string vars: msg: ABC debug: msg: "{{ msg }}
阅读全文
摘要:特别注意:这些文件的判断是用于判断ansible 运维主机上的文件,而不是远程主机 | 参数 | 释义 | | | | | file | 等同bash 中[ -f filename ] | | directory | 等同bash 中[ -d filename ] | | link | 等同bash
阅读全文
摘要:常常用于判断前一个命令执行的结果. | 参数 | 释义 | 示例 | | | | | | success | 执行状态为成功为真 | when: result is success | | succeeded | | | | failure | 执行状态为失败为真 | when: result is
阅读全文
摘要:| 参数 | 释义 | 示例 | | | | | | defined | 变量已定义 | when: username is defined | | undefind | 变量未定义 | when: username is undfined | | none | 变量已定义未赋值 | when: u
阅读全文
摘要:判断添加用户 - hosts: all vars: - username: test1 tasks: - name: detect whether the user exists shell: id {{ username }} register: result ignore_errors: tru
阅读全文
摘要:| 逻辑运算 | 释义 | | | | | and | 逻辑与 | | or | 逻辑或 | | not | 逻辑取反 | 示例一: 等同于: if ansible_distribution == "CentOS" and ansible_distribution_major_version ==
阅读全文
摘要:| 比较符 | 释义 | | | | | == | 等于 | | != | 不等 | | < | 小于 | | <= | 小于等于 | | > | 大于 | | >= | 大于等于 | 示例一: 需要注意 when 使用变量时不需要 {{}} - hosts: all tasks: - name:
阅读全文
摘要:功能类似于: 列表元素数量上要一一对应 for i in zip([1,2,3],['a','b','c']): print(i) - hosts: all tasks: - name: debug debug: msg: "{{ item }}" with_together: - ["a","b"
阅读全文
摘要:添加索引 - hosts: all tasks: - name: debug debug: msg: "{{ item }}" with_indexed_items: - [1,2,3] - ['a','b','c','d'] - hosts: all tasks: - name: debug de
阅读全文
摘要:with_nested 和with_cartesian 实现的功能相同。嵌套循环可以理解为: nest 英文嵌套 cartesian 英文笛卡尔积 for i in [1,2,3]: for j in ['a','b','c','d']: print(i,j) - hosts: all tasks:
阅读全文
摘要:- hosts: all tasks: - name: debug debug: msg: "{{ item.key }} 的名字叫 {{ item.value.name }}" with_dict: - user01: name: zhangsan age: 30 - user02: name:
阅读全文
摘要:with_list 不展开循环嵌套的列表。功能类似于 for i in zip([1,2,3],['a','b','c']): print(i) - hosts: all tasks: - name: debug debug: msg: "{{ item }}" with_list - ["a","
阅读全文
摘要:在tasks 中注册变量set_fact,该变量可以跨tasks调用 - hosts: 127.0.0.1 tasks: - set_fact: var: "value" - name: debug: msg: "{{ var }}" - hosts: 127.0.0.1 tasks: - name
阅读全文
摘要:可以通过 ansible 127.0.0.1 -m setup查看支持的内置变量 - hosts: 127.0.0.1 tasks: - name: debug debug: msg: "hostname: {{ ansible_fqdn }} ip: {{ ansible_default_ipv4
阅读全文
摘要:每一个task 执行后都有一些返回信息我们可以通过 register 把这些返回信息赋值给变量 - hosts: 127.0.0.1 tasks: - name: mkdir file: path: /tmp/1 state: directory owner: root register: resu
阅读全文
摘要:示例1 - hosts: 127.0.0.1 vars_prompt: - name: host prompt: "please input your hostip" - name: port prompt: "please input your port" tasks: - name: debug
阅读全文
摘要:vars_files 可以把var定义的变量分离出来方便修改和复用 - hosts: 127.0.0.1 vars_files: - ./var1.yaml tasks: - name: debug debug: msg: http://{{ bind.ip }}:{{ bind.port }} #
阅读全文