OSDN Git Service

Regular updates
[twpd/master.git] / _inactive / ansible.md
1 ---
2 title: Ansible
3 category: Ruby
4 ---
5
6 ## Looping
7
8 ### Array (with_items)
9
10 ```yaml
11 vars:
12   security_groups:
13     - name: 'hello'
14       desc: 'world'
15
16     - name: 'hola'
17       desc: 'mundo'
18
19 tasks:
20   - name: Create required security groups
21     ec2_group:
22       name: "{{ item.name }}"
23       description: "{{ item.desc }}"
24     with_items: "{{ security_groups }}"
25 ```
26
27 ### Object (with_dict)
28
29 ```yaml
30 tasks:
31   - name: Print phone records
32     debug: msg="User {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})"
33     with_dict: "{{ users }}"
34 ```
35
36 ## with_file
37
38 ```yaml
39 - name: "Send key"
40   ec2_key:
41     key_material: "{{ item }}"
42   with_file: ./keys/sshkey.pub
43
44   # or
45   with_fileglob: ./keys/*.pub
46 ```
47
48 ### Conditionals
49
50 ```yml
51 - include: setup-debian.yml
52   when: ansible_os_family == 'Debian'
53
54   when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "6") or
55         (ansible_distribution == "Debian" and ansible_distribution_major_version == "7")
56
57
58   # Just like "and"
59   when:
60     - ansible_distribution == "CentOS"
61     - ansible_distribution_major_version == "6"
62 ```
63
64 ## Expressions
65
66 ```
67 {{ item }}
68 {{ item.name }}
69 {{ item[0].name }}
70
71 {{ item | default('latest') }}
72 ```
73
74 ## Includes
75
76 ```
77 tasks:
78   - include: wordpress.yml
79     vars:
80       wp_user: timmy
81 ```