Ansible FAQ

Ansible FAQ

Ansible FAQ

AnsibleUndefinedVariable: dict object has no element

我的使用了变量dc,但是后面vars/main.yml又写了dc导致重复 类似这样dc[dc] xxx 解决办法: vars/main.yml变量改成datacenter

{"reason": "We were unable to read either as JSON nor YAML, these are the errors we got from each:\nJSON: Expecting value: line 1 column 1 (char 0)\n\nSyntax Error while loading YAML.\n expected <block end>, but found '<scalar>'\n\nThe error appears to be in '/etc/ansible/roles/consul/tasks/server.yml': line 17, column 17, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n copy:\n src: {{dc}}-server-consul-0-key.pem\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - \"{{ foo }}\"\n"}

{{}}建议都用""包起来,否则可能会报这个错 {{aa}}-afdasfd类似这种可能会报错

ansible快速测试模块或者针对服务器进行批量命令

一种是tmux同步执行,这个好处是所见即所得 ansible -m MODULE -i INVENTORY GROUP -a "PARAMS" ansible -m shell -i xxx all -a "ls /etc" 这里我还是建议优先用shell不要用command 可能会有一些奇怪的问题 因为command很多都不支持

ansible怎么放变量

只有模块需要的变量放到roles/xxx/vars/main.yml, roles/xxx/defaults/main.yml playbook定义的变量可以通过vars: xxx: xxx定义或者include_vars: /where/is/myvars.yml 每个模块都用到的,变量放到group_vars/all/main.yml 模块用到的敏感变量,也可以放到group_vars/all/main.yml

ansible 报ansible debuig mux_client_read_packet: read header failed的一种可能性

become=true改成false,如果没有sudo的需求

ansible如何从远程copy到宿主

使用fetch模块

ModuleNotFoundError: No module named 'setuptools_rust'

1
sudo pip3 install setuptools-rust

ansible copy too slow太慢了

使用synchronize模块

1
2
3
4
5
6
7
8
ansible -i ansible-hosts team -m synchronize -a "src=/test/ dest=/data/ rsync_opts=--delete,--exclude-from=/root/lqx/exclude"
- name: copy file to master hosts
  synchronize:
    src: /data/package/ansible/{{package_name.stdout}}/
    dest: /data/app/
    rsync_opts: 
      - '--delete'
      - '--exclude=".git/*"'

ansible stop playbook

1
2
- meta: end_play
  when: is_continue_param == "no"

ansible加密encrypt资产inventory

1
2
ansible-vault create xxx-hosts
ansible -i xxx-hosts all -m ping --vault-pass=~/.vault_pass

ansible查看role帮助

1
2
# ansible-doc XXXX eg.
ansible-doc shell

ansible批量下发多个命令交互

1
ansible-console -i xxxx

ansible uri模块报错can't concat str to bytes

uri的时候写一下

1
body_format: "json"

ansible里面怎么使用时间变量

1
2
3
4
5
6
---
- hosts: all
  tasks:
  - name: Get 20230505 timestamp
    debug:
      msg: "{{ ansible_date_time.iso8601 | regex_replace('^(\\d{4})-(\\d{2})-(\\d{2}).*$', '\\1\\2\\3') }}"

使用ansible_date_time

ansible里面调用外部命令获得到变量里面

使用pipe和lookup配合 参考ansible up and running p158

1
2
- name: get SHA of recent commit
  debug: msg="{{lookup('pipe', 'git rev-parse HEADE')}}"

ansible获取环境变量

1
2
- name: get env SHELL
  debug: msg="{{lookup('ENV', 'SHELL')}}"

生成一个临时密码

1
2
- name: create test password
  debug: msg="{{lookup('password', 'remote-password.txt')}}"

根据模板生成信息

1
2
3
4
5
# test.j2
This host runs {{ ansible_distribution }}
# yaml
- name: output message from template
  debug: msg="{{lookup('template', 'test.j2')}}"

ansible添加依赖模块

from p178 ansible up and running

meta/main.yml

1
2
dependencies:
  - { role: ntp, ntp_server=ntp.ubuntu.com }

ansible playbook指定从某个task开始

p290 ansible up and running

1
ansible-playbook --start-at-task="install packages" playbook.yml