我正在管理多个集群,并且希望将多个清单文件合并到一个清单中,该清单实际上如下所示:
all:
children:
cluster_one:
children:
controller:
hosts:
host1:
host2:
host3:
compute:
hosts:
host4:
host5:
host6:
cluster_two:
children:
controller:
hosts:
host11:
host12:
host13:
Run Code Online (Sandbox Code Playgroud)
我期待这最终会像这样解析:
[cluster_one]
host1
host2
host3
host4
host5
host6
[cluster_two]
host11
host12
host13
[controller]
host1
host2
host3
host11
host12
host13
[compute]
host4
host5
host6
Run Code Online (Sandbox Code Playgroud)
通过这种结构,我将能够使用主机模式请求“cluster_one 中的控制器” cluster_one:&controller,或者如果我想要所有集群中的所有控制器,我可以只请求
controller. 方便的!
不幸的是,它实际上是这样解析的:
[cluster_one]
host1
host2
host3
host4
host5
host6
[cluster_two]
host11
host12
host13
[controller]
host1
host2
host3
host11
host12
host13
[compute]
host4
host5
host6
[cluster_one:children]
controller
compute
[cluster_two:children]
controller …Run Code Online (Sandbox Code Playgroud) 我想在 Ansible 中无需 ssh 的情况下在 localhost 上运行我的 playbook。我怎样才能做到这一点?我有一个虚拟机。我希望它在那台机器上本地运行。
是否有一种DRY方法可以为多个组中的主机指定主机变量?例如:
all:
hosts:
mail.example.com:
ansible_host: 10.10.10.33
children:
webservers:
hosts:
foo.example.com:
ansible_host: 10.10.10.17
bar.example.com:
ansible_host: 10.10.10.18
dbservers:
hosts:
one.example.com:
ansible_host: 10.10.10.1
two.example.com:
ansible_host: 10.10.10.2
three.example.com:
ansible_host: 10.10.10.3
test:
hosts:
bar.example.com:
ansible_host: 10.10.10.18 # Don't like this being defined in a 2nd place
three.example.com:
ansible_host: 10.10.10.3 # =( Same
Run Code Online (Sandbox Code Playgroud)
是否可以将主机变量(如ansible_host示例(或软件许可证)中那样)仅定义在一个位置?我无法在 ansible 文档中找到任何合适的内容,因此占位符解决方案是使用YAML 锚点:
all:
hosts:
mail.example.com:
ansible_host: 10.10.10.33
children:
webservers:
hosts:
foo.example.com:
ansible_host: 10.10.10.17
bar.example.com: &bar
ansible_host: 10.10.10.18
dbservers:
hosts:
one.example.com:
ansible_host: 10.10.10.1
two.example.com:
ansible_host: …Run Code Online (Sandbox Code Playgroud) 库存文件(inventory/k8s.yaml):
plugin: kubernetes.core.k8s
connections:
- kubeconfig: ~/.kube/config
context: 'cluster-2'
Run Code Online (Sandbox Code Playgroud)
任务文件(roles/common/tasks/main.yaml):
# Method 1: Using `kubernetes.core` plugin to list the pod names:
- name: Get a list of all pods from any namespace
kubernetes.core.k8s_info:
kind: Pod
register: pod_list
- name: Print pod names
debug:
msg: "pod_list: {{ pod_list | json_query('resources[*].metadata.name') }} "
# Method 2: Using `shell` command to list the pod names:
- name: Get node names
shell: kubectl get pods
register: pod_list2
- name: Print pod names
debug:
msg: …Run Code Online (Sandbox Code Playgroud) 我正在使用ec2.py动态库存来配置ansible.我放置了ec2.py在/etc/ansible/hosts文件和可执行其标记.我也有ec2.ini文件/etc/ansible/hosts.
[ec2]
regions = us-west-2
regions_exclude = us-gov-west-1,cn-north-1
destination_variable = public_dns_name
vpc_destination_variable = ip_address
route53 = False
all_instances = True
all_rds_instances = False
cache_path = ~/.ansible/tmp
cache_max_age = 0
nested_groups = False
group_by_instance_id = True
group_by_region = True
group_by_availability_zone = True
group_by_ami_id = True
group_by_instance_type = True
group_by_key_pair = True
group_by_vpc_id = True
group_by_security_group = True
group_by_tag_keys = True
group_by_tag_none = True
group_by_route53_names = True
group_by_rds_engine = True
group_by_rds_parameter_group = …Run Code Online (Sandbox Code Playgroud) amazon-ec2 amazon-web-services ansible ansible-playbook ansible-inventory
Ansible版本:1.9.2
我正在运行一个将启动EC2实例的剧本.我有所有变量的site.yml并使用角色来启动EC2实例.这是ec2_launch的main.yml
---
- name: create EC2 instance
ec2:
key_name: "{{ keypair }}"
group_id: "{{ security_group }}"
instance_type: "{{ instance_type }}"
image: "{{ image }}"
region: "{{ region }}"
vpc_subnet_id: "{{ vpc_subnet_id }}"
assign_public_ip: yes
wait: True
instance_tags:
Name: "{{ instance_tag }}"
Environment: "{{ instance_tag2 }}"
exact_count: 1
count_tag:
Name: "{{ instance_tag }}"
Environment: "{{ instance_tag2 }}"
monitoring: yes
register: ec2
- name: add ec2 instance to hosts
add_host: name={{ item.private_ip }} groups=group_name
with_items: ec2.instances
- name: wait for SSH …Run Code Online (Sandbox Code Playgroud) 使用ansible-playbook命令,我们可以为库存中的所有主机执行playbook,并且我有一个从web服务器中删除缓存的playbook.但我不应该删除特定计算机上的缓存,除非开发人员要求这样做.
但通过使用ansible-playbook我无法从库存中挑选一个特定的主机.但我可以使用一些语法,ansible-playbook somebook.play -i hostname,但这会查找DNS条目并执行相应但不检查库存文件.
另一种方法是为每个主机创建单独的库存文件,并按开发人员按请求的主机调用它们,我不知道这是多少推荐的方式.
最后一个方法AFAIK ansible.runner通过Python使用并在hostname上使用模式并调用python程序.
感谢您阅读我的长篇文章.
除此之外,您是否知道在执行剧本时从库存中选择特定主机/主机组的其他方法?
我想知道任何其他方式,但只使用ansible-playbook和库存文件.
我正在尝试创建仅在特定组(称为pi)中的框上运行的任务。
我正在使用Ansible版本:
ansible 2.3.2.0
config file = /Users/user/Development/raspbian-homeassistant/ansible.cfg
configured module search path = Default w/o overrides
python version = 3.6.3 (default, Dec 3 2017, 10:37:53) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.38)]
Run Code Online (Sandbox Code Playgroud)
这是我当前的代码:
- name: Set up zwave USB dongle
when: inventory_hostname in groups['pi']
blockinfile:
path: /var/local/homeassistant/.homeassistant/configuration.yaml
marker: "# {mark} ANSIBLE MANAGED BLOCK #"
insertafter: "# zwave dongle"
content: |2
zwave:
usb_path: /dev/ttyACM0
tags:
- config
- hass
Run Code Online (Sandbox Code Playgroud)
当主机名在组中时,它似乎可以正常工作,而在主机名不在时,则抛出错误。
这是我在无业游民的盒子(在组中vagrant)上运行时遇到的错误:
fatal: [192.168.33.123]: FAILED! => {"failed": true, …Run Code Online (Sandbox Code Playgroud) 我正在尝试开始使用Ansible和aws_ec2插件。
我的./ansible.cfg文件中包含以下内容:
[inventory]
enable_plugins = aws_ec2
Run Code Online (Sandbox Code Playgroud)
以及我./inventory.yml文件中的以下内容:
plugin: aws_ec2
aws_access_key_id: **********
aws_secret_access_key: **********
regions:
- us-east-2
Run Code Online (Sandbox Code Playgroud)
当我运行时ansible-inventory -i inventory.yml --graph,出现以下错误:
inventory.yml did not meet aws_ec2 requirements, check plugin documentation if this is unexpected
我需要将库存中的特定主机作为参数传递给角色。主机是组的一部分,但由其他主机都没有的变量划分。
片段:hosts.yml
dbservers:
hosts:
pg01:
ansible_host: pg01.domain.com
master_slave: master
pg02:
ansible_host: pg02.domain.com
master_slave: slave
Run Code Online (Sandbox Code Playgroud)
我希望能够根据变量 master_slave 设置为“master”来解析 pg01,这样我就可以调用这样的角色:
- name: Do something
include_role:
name: a.database.role.to.run.on.master
vars:
master_database_host: {{ something that resolves to pg01 }}
Run Code Online (Sandbox Code Playgroud)
如何从清单中解析合适的主机?
ansible ×10
amazon-ec2 ×3
ansible-2.x ×1
azure-aks ×1
kubernetes ×1
localhost ×1
ssh ×1
yaml ×1