我有以下 Ansible 主机文件:
# Contains the host mappings
[master]
m1.my-host.com ansible_host=192.168.0.100
[node]
n1.my-host.com ansible_host=192.168.0.102
n2.my-host.com ansible_host=192.168.0.103
[k3s_cluster:children]
master
node
[all:vars]
ansible_python_interpreter=/usr/bin/python3
Run Code Online (Sandbox Code Playgroud)
在我的剧本中,我说我需要像这样的有序执行:
---
- name: Setup hostname, users and groups
hosts: all
order: sorted
gather_facts: true
remote_user: myuser
become: yes
roles:
- {role: common, tags: ['host', 'system']}
Run Code Online (Sandbox Code Playgroud)
但当我运行它时,我看到以下内容:
TASK [Gathering Facts] **********************************************************************************************************************************************************************************************
ok: [n1.my-host.com]
ok: [n2.my-host.com]
ok: [m1.my-host.com]
Run Code Online (Sandbox Code Playgroud)
我宁愿期望它是 m1、n1,然后是 n2。是否知道为什么不遵守排序顺序?
在 ansible 中,我使用用 jinja2 编写的模板,我有一个内部 for 循环,它会自动向我的配置文件添加空间,但我不希望这样做。
stick store-response payload_lv(43,1) if serverhello
option ssl-hello-chk
{% set count = 1 %}
{% for ip in sv.ips %}
server server{{ count }} {{ ip }}:443 check
{% set count = count + 1 %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
结果是
stick store-response payload_lv(43,1) if serverhello
option ssl-hello-chk
server server1 10.2.0.16:443 check
server server2 10.2.0.20:443 check
Run Code Online (Sandbox Code Playgroud) hostvars[item]['ansible_nodename']我们有几个使用主机item别名的剧本。它通常有效。但有时不行,出现以下错误:
'dict object' has no attribute 'ansible_nodename`
Run Code Online (Sandbox Code Playgroud)
我打印了内容hostvars,但没有看到这个属性。
我找不到文档来hostvars了解我们可以安全地使用什么ansible_nodename。所以,问题是:
ansible_nodename为吗ansible_host?hostvars在哪里可以找到其创建内容和算法的描述?我正在使用以下site.yml剧本,并通过
ansible-playbook site.yml
- hosts: some_hosts
vars:
pip_install_packages:
- name: docker
- tasks:
- name: Conditionally include bar vars
include_vars:
file: bar_vars.yml
when: some_condition == "bar"
- name: Conditionally include foo vars
include_vars:
file: foo_vars.yml
when: some_condition == "foo"
roles:
- role1
- role2
environment:
SOME_ENV_VAR: "{{ vault_some_env_var }}"
Run Code Online (Sandbox Code Playgroud)
呼叫失败,如下所示:
错误!字段“主机”为必填项,但未设置
但显而易见的是上述情况,hosts现场已被设置!
有什么建议么?
有人知道如何在ansible中对循环中的数字求和并设置为变量吗?
下列的:
- set_fact:
total: "{{ sum(item | int) }}" <--- it's not work!!!
loop:
- 1
- 4
- 3
- debug: var=total
Run Code Online (Sandbox Code Playgroud)
谢谢
我在tasks/main.yml中有以下代码
---
- name: Check if service exists.
shell: "systemctl status {{ service }}"
ignore_errors: yes
register: service_exists
- name: Enable service and start if it exists.
systemd:
name: "{{ service }}"
state: started
enabled: true
when: "could not be found" not in service_exists.stderr' or '"Failed to get properties" not in service_exists.stderr' or '"not-found" not in service_exists.stderr'
Run Code Online (Sandbox Code Playgroud)
我在configure-services.yml中有以下代码
---
- hosts: localhost
become: true
gather_facts: yes
tasks:
- include: tasks/main.yml
with_items: "{{ services }}"
loop_control:
loop_var: service
Run Code Online (Sandbox Code Playgroud)
但是,运行剧本时出现以下错误。 …
yaml conditional-statements ansible ansible-awx ansible-tower
这是输出。
"result.containers":[
{
"Image":"ca.docker/webproxy:1.0.0",
"Names":[
"/customer1"
]
},
{
"Image":"docker.local/egacustomer:1.0.1",
"Names":[
"/registrator"
]
}
]
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 jinja2 获得以下输出
"containerlist"=>{
"webproxy": {
"name": "customer1"
},
"egacustomer": {
"name": "registrator"
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的 jinja2 代码。
- set_fact:
containerlist: |
{
{% for item in result.containers %}
{{ item.Image.split('/')[-1].split(':')[0] | replace('\n', '') }}
name : {{ item.Names[0][1:] | replace('\n', '') }}
{% endfor %}
}
Run Code Online (Sandbox Code Playgroud)
我得到以下输出。
"containerlist": "{\nwebproxy\n name : customer1\negacustome\n name : registrator\n}"
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我获得所需的输出吗?任何帮助将不胜感激
我有一个正在构建的 HA 设置。我正在使用 Ansible 2.9.11。我需要搜索主机名,如果是 b 端,则将备份配置复制过来。我已经尝试了以下代码,但它不起作用。或者有没有不同的方法来实现这一点?
[警告]:条件语句不应包含 jinja2 模板分隔符,例如 {{ }} 或 {% %}。发现:{{inventory_hostname}} 中的 'b' 致命:[dev-sca02b]:失败!=> {"msg": "{{inventory_hostname }}' 中的条件检查 ''b' 失败。错误是:评估条件时出错({{inventory_hostname }} 中的'b'):'dev' 未定义\ n\n错误似乎在“/Users/user1/Documents/Ansible/sca_fusion.yaml”:第 134 行,第 5 列,但可能\n在文件中的其他地方,具体取决于确切的语法问题。\n\n违规行似乎是:\n\n\n - 名称:“将 keepalived 更改为备份”\n ^ 此处\n"}
- name: "Change keepalived to backup"
replace:
path: /etc/keepalived/keepalived.conf
regexp: "MASTER"
replace: "BACKUP"
when: "'b' in {{ inventory_hostname }}"
Run Code Online (Sandbox Code Playgroud) 我想使用 Ansible-k8s 模块创建 k8s 秘密。虽然我可以使用 kubectl Secret 命令创建秘密,但我需要使用 Ansible-k8s 模块创建。
请求你帮助我。提前致谢
我试图htop使用以下命令在 debian 服务器上安装:
ansible debian -m apt -a "name=htop state=present"
但我收到此错误:
ubuntu-20.lab | FAILED! => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"msg": "'/usr/bin/apt-mark manual htop' failed: E: Could not create temporary file for /var/lib/apt/extended_states - mkstemp (13: Permission denied)\nE: Failed to write temporary StateFile /var/lib/apt/extended_states\n",
"rc": 100,
"stderr": "E: Could not create temporary file for /var/lib/apt/extended_states - mkstemp (13: Permission denied)\nE: Failed to write temporary StateFile /var/lib/apt/extended_states\n",
"stderr_lines": [
"E: Could not create temporary file for …Run Code Online (Sandbox Code Playgroud) ansible ×10
ansible-2.x ×2
jinja2 ×2
ansible-awx ×1
hosts ×1
kubernetes ×1
permissions ×1
sum ×1
yaml ×1