趁周末把ansible action plugin 写了一个简单的例子,我们日常工作中可能很少会用到action plugin ,但是如果我们能通过action plugin 做一些初始化的工作,例如设定一些默认值,这样我们的ansible playbook 可能会简介很多 例如我们写了一个module需要传递2个参数,如果我们这个mod……
分类目录:ansible实战
ansible action plugin
解决ansible dry run 报错 undefined variable
ansible set_fact loop 仅保留最后一个值的问题
ansible 中set_fact 支持循环,with_items,但是变量并不是append 例如:第一个set_fact部分用的是with_items,但是,我们打印的结果中只有最后一次循环中的赋值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
--- #source your credential first: export AWS_PROFILE=poc #confirm the origin host have the iam role setting , security group setting #copy the userdata into the j2 file and make necessary change #ansible-playbook create_new_instance_from_old_one.yml -e "origin_host_id=i-095ea1e9b5ea236b0" -e "region=us-east-1" -e "new_host_name=jeremy-test-playbook-instance11" - name: test set fact with hosts: localhost vars: security_group_new_append: [] security_group_dict: - group_id: "groupid_abc_1" group_name: "Name_1" - group_id: "groupid_abc_2" group_name: "Name_2" - group_id: "groupid_abc_3" group_name: "Name_3" tasks: - name: this is a test debug: msg: "this is the test message from localhost" - name: set fact set_fact: security_group_new: "{{ item.group_name }}" with_items: "{{ security_group_dict }}" - name : show me the security_group_new debug: var: security_group_new - name: set fact set_fact: security_group_new_append: "{{ security_group_new_append + [item.group_name] }}" with_items: "{{ security_group_dict }}" - name : show me the security_group_new debug: var: security_group_new_append |
但是如果我们使用第二个task ,就可以在输出结果中打印……
ansible filter (编写你自己的ansible filter)
有时候我们写自己的playbook的时候变量处理真的是特别让人头大,有些变量的操作在python中就很简单,但是放到ansible playbook中就会很麻烦,所以,我们今天来看一下如何编写自己的ansible filter来帮助我们处理一下变量 直接看例子吧:
1 2 3 4 5 6 7 8 9 10 11 |
#!/usr/bin/python class FilterModule(object): def filters(self): return { 'a_filter': self.a_filter, 'another_filter': self.b_filter } def a_filter(self, a_variable): a_new_variable = a_variable + ' CRAZY NEW FILTER' return a_new_variable |
我们来分……
ansible 是什么?ansible简介
如何在ansible role中调用自己的Module和filter
我们自定义了自己的filter和module之后,普通的调用肯定都没问题(在playbooK同级目录下创建对应文件夹) 如何在一个role中增加自己的module和filter? 首先,我们看一个正常的role的目录结构:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
site.yml webservers.yml fooservers.yml roles/ common/ tasks/ handlers/ files/ templates/ vars/ defaults/ meta/ webservers/ tasks/ defaults/ meta/ |
这是一个标准版的例子,如何想加入module和fi……
ansibleplaybook 如何使用tag
在日常的ansible-playbook中使用tags 来灵活控制我们向要执行的具体的task , 有时候能达到意想不到的效果 看例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
--- - hosts: all tasks: - name: My tagged task yum: name: httpd state: latest tags: production - name: Installs postfix yum: name: postfix state: latest |
只执行第一个task:
1 |
ansible-playbook main.yml --tags 'production' |
只执行第二个task: [crayon-648……