一、获取系统可用参数
# show os facts
---
- hosts: localhost
gather_facts: true
tasks:
- name: show os facts
debug:
var: ansible_facts
二、用 IP 进行/etc/hosts 的设置
# set-hostname.yaml
---
- hosts: localhost
gather_facts: true
vars:
host_name: 55.qiweioa.cn
tasks:
- name: set hostname
hostname: 'name={{ host_name }}'
- name: "add the line to the tail of the file"
lineinfile:
dest: /etc/hosts
line: "{{ ansible_default_ipv4.address }} {{ host_name }}"
run_once: True
- name: Replace a localhost entry with our own
lineinfile:
path: /etc/hosts
regexp: '^127\.0\.0\.1'
line: 127.0.0.1 localhost hostname example.com
owner: root
group: root
mode: '0644'
三、一个实用的 playbook 例子
---
- hosts: k8s-cluster
any_errors_fatal: "{{ any_errors_fatal | default(true) }}"
vars:
dns_domain: cluster.local
tasks:
- name: populate inventory into hosts file
blockinfile:
path: /etc/hosts
block: |-
{% for item in (groups['k8s-cluster'] + groups['etcd'] + groups['calico-rr']|default([]))|unique -%}
{{ hostvars[item]['ip'] }} {{ item }}.{{ dns_domain }} {{ item }}
{% endfor %}
state: present
create: yes
backup: yes
unsafe_writes: yes
marker: "# Ansible inventory hosts {mark}"
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于