ansible plugin属于ansible 的高阶用法,他与ansible module不同,我们普通的module ,或者我们自己编写的module更多的是在目标主机运行,而ansible action plugin 则是在ansible的执行机器运行(也就是本机执行) 另外一个是ansible会先执行action plugin 然……
标签目录:ansible
以下是与标签 “ansible” 相关联的文章ansible -i /home/ansible/inv all -m shell -a “sudo netstat -nltp | grep 8009″
通过crontab来执行ansible-playbook 自动加载ssh-key的问题
使用 Packer、Ansible 和 Terraform 构建不可变的基础设施
ansible 邮件通知
ansible playbook 常用filter selectattr
随着ansilbe playbook 用的越来越多,我们也开始接触许多高级filter 例如selectattr 举个例子:
1 2 3 4 5 6 7 8 9 |
--- users: - name: john email: john@example.com - name: jane email: jane@example.com - name: fred email: fred@example.com password: 123!abc |
如果我们执行如下的task
1 2 |
- set_fact: emails: "{{ users | selectattr('password', 'undefined') | map(attribute='email') | list }}" |
首先,我们定义的users会被传递给selectatt……
ansible-playbook shell模块 转义大括号 {{}} 单引号
今天发现要在shell命令中使用大括号{{}} 刚开始的playbook如下:
1 2 3 4 5 6 7 8 9 10 11 12 |
#!/usr/bin/ansible-playbook ############################# - hosts: localhost remote_user: root gather_facts: false tasks: - name: "{{ check_command }}" shell: /usr/bin/oc get secrets -n namespace secrentname --template '{{ index .data "abc.crt" }}' | base64 -d | openssl x509 -noout -text|grep After register: ssl_out - debug: var=ssl_out |
发现有两个问题,单引号需要转义,大括号需要转义 ansible 的shell模块中对于单引号的转义很简单”,两个连续的单引号就可以了 但是对于大括号则需要:{{ ‘{{&……
ansible register 无视when 条件执行
先来看一段ansible代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
- name: test hosts: localhost gather_facts: no vars: test_name: 'real_name' flag: false tasks: - name: retrieve node's hostname shell: "hostname" register: test_name when: flag | bool - debug: var: test_name |
正常来说,我们认为会输出结果会是’real_name’,因为第一个任务因为when的条件判断并没有执行 但是,结果是什么呢?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
ansible-playbook 1.yaml [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [test] ************************************************************************************************************************************************************************* TASK [retrieve node's hostname] ***************************************************************************************************************************************************** skipping: [localhost] TASK [debug] ************************************************************************************************************************************************************************ ok: [localhost] => { "test_name": { "changed": false, "skip_reason": "Conditional result was False", "skipped": true } } PLAY RECAP ************************************************************************************************************************************************************************** localhost : ok=1 changed=0 unreachable=0 failed=0 |
并没有,难道是skip了?……
ansible 是什么?ansible简介
如何创建自己的ansible filter
ansible提供给我们了很多常用的fitler,官方文档地址: http://docs.ansible.com/ansible/latest/playbooks_filters.html 例子:
1 |
<span class="cp">{{</span> <span class="nv">list1</span> <span class="o">|</span> <span class="nf">min</span> <span class="cp">}}</span> |
1 |
<span class="s">"</span><span class="cp">{{</span> <span class="o">[</span><span class="s1">'a'</span><span class="o">,</span><span class="s1">'b'</span><span class="o">,</span><span class="s1">'c'</span><span class="o">]|</span><span class="nf">random</span> <span class="cp">}}</span><span class="s">"</span> |
[cray……