diff options
Diffstat (limited to '3 resources/ansible')
-rw-r--r-- | 3 resources/ansible/ad-hoc-commands.md | 25 | ||||
-rw-r--r-- | 3 resources/ansible/ansible.md | 1 | ||||
-rw-r--r-- | 3 resources/ansible/build-array-loop.md | 6 | ||||
-rw-r--r-- | 3 resources/ansible/build-list-silences.md | 1 | ||||
-rw-r--r-- | 3 resources/ansible/debug.md | 29 | ||||
-rw-r--r-- | 3 resources/ansible/default-filter.md | 7 | ||||
-rw-r--r-- | 3 resources/ansible/delegate-to.md | 3 | ||||
-rw-r--r-- | 3 resources/ansible/dicts.md | 1 | ||||
-rw-r--r-- | 3 resources/ansible/extra-vars.md | 8 | ||||
-rw-r--r-- | 3 resources/ansible/find.md | 9 | ||||
-rw-r--r-- | 3 resources/ansible/loop-output.md | 1 | ||||
-rw-r--r-- | 3 resources/ansible/loop-register.md | 41 | ||||
-rw-r--r-- | 3 resources/ansible/loop-results-prior-loop.md | 30 | ||||
-rw-r--r-- | 3 resources/ansible/nested-loops.md | 3 | ||||
-rw-r--r-- | 3 resources/ansible/set-facts.md | 1 |
15 files changed, 166 insertions, 0 deletions
diff --git a/3 resources/ansible/ad-hoc-commands.md b/3 resources/ansible/ad-hoc-commands.md new file mode 100644 index 0000000..eda1265 --- /dev/null +++ b/3 resources/ansible/ad-hoc-commands.md @@ -0,0 +1,25 @@ +Using ansible we can perform ad-hoc commands, useful for a one-off thing that won't require writing a full playbook. + +Some examples include: +update apt cache: +``` +ansible <pattern> -m ansible.builtin.apt -a "update_cache=true cache_valid_time=3600" +``` + +update packages: +``` +ansible <pattern> -m ansible.builtin.apt -a "name=* state=latest" +``` + +run puppet: +``` +ansible <pattern> -a "/opt/puppetlabs/bin/puppet agent --test" +``` + +reboot: +``` +ansible <pattern> -m ansible.builtin.reboot -f 1 +``` + +[[ansible]] +#ansible
\ No newline at end of file diff --git a/3 resources/ansible/ansible.md b/3 resources/ansible/ansible.md new file mode 100644 index 0000000..5886288 --- /dev/null +++ b/3 resources/ansible/ansible.md @@ -0,0 +1 @@ +At its heart it's just an abstraction over running commands remotely over SSH. It enables some powerful automation like setting up a server following a predetermined script.
\ No newline at end of file diff --git a/3 resources/ansible/build-array-loop.md b/3 resources/ansible/build-array-loop.md new file mode 100644 index 0000000..15f441f --- /dev/null +++ b/3 resources/ansible/build-array-loop.md @@ -0,0 +1,6 @@ +How to construct an array variable containing results of an iterative ansible task? +Use register. The referenced variable will contain a property results which is a list containing the combined outputs of the loop. + +https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_loops.html#registering-variables-with-a-loop + +[[ansible]]
\ No newline at end of file diff --git a/3 resources/ansible/build-list-silences.md b/3 resources/ansible/build-list-silences.md new file mode 100644 index 0000000..dc8ef61 --- /dev/null +++ b/3 resources/ansible/build-list-silences.md @@ -0,0 +1 @@ +Explain what I did today at work to build a list of silences and then have one task to unsilence all of them.
\ No newline at end of file diff --git a/3 resources/ansible/debug.md b/3 resources/ansible/debug.md new file mode 100644 index 0000000..552af1f --- /dev/null +++ b/3 resources/ansible/debug.md @@ -0,0 +1,29 @@ +``` +- name: Print the gateway for each host when defined + ansible.builtin.debug: + msg: System {{ inventory_hostname }} has gateway {{ ansible_default_ipv4.gateway }} + when: ansible_default_ipv4.gateway is defined + +- name: Get uptime information + ansible.builtin.shell: /usr/bin/uptime + register: result + +- name: Print return information from the previous task + ansible.builtin.debug: + var: result + verbosity: 2 + +- name: Display all variables/facts known for a host + ansible.builtin.debug: + var: hostvars[inventory_hostname] + verbosity: 4 + +- name: Prints two lines of messages, but only if there is an environment value set + ansible.builtin.debug: + msg: + - "Provisioning based on YOUR_KEY which is: {{ lookup('ansible.builtin.env', 'YOUR_KEY') }}" + - "These servers were built using the password of '{{ password_used }}'. Please retain this for later use." +``` + +--- +https://docs.ansible.com/ansible/latest/collections/ansible/builtin/debug_module.html
\ No newline at end of file diff --git a/3 resources/ansible/default-filter.md b/3 resources/ansible/default-filter.md new file mode 100644 index 0000000..2255e71 --- /dev/null +++ b/3 resources/ansible/default-filter.md @@ -0,0 +1,7 @@ +Using the default filter, if we reference an undefined variable we can set a default: `{{ var | default([]) }}`. + +defaults: +- int +- str +- list ([]) +- dict ({})
\ No newline at end of file diff --git a/3 resources/ansible/delegate-to.md b/3 resources/ansible/delegate-to.md new file mode 100644 index 0000000..270ccb1 --- /dev/null +++ b/3 resources/ansible/delegate-to.md @@ -0,0 +1,3 @@ +Through the use of delegate to, we can run a task for certain nodes and delegate work on behalf of those nodes to other hosts. + +A great example of usage is this: diff --git a/3 resources/ansible/dicts.md b/3 resources/ansible/dicts.md new file mode 100644 index 0000000..eac3d1b --- /dev/null +++ b/3 resources/ansible/dicts.md @@ -0,0 +1 @@ +Merged using combine: `{{ dict1 | ansible.builtin.combine(dict2) }}`
\ No newline at end of file diff --git a/3 resources/ansible/extra-vars.md b/3 resources/ansible/extra-vars.md new file mode 100644 index 0000000..07b0764 --- /dev/null +++ b/3 resources/ansible/extra-vars.md @@ -0,0 +1,8 @@ +We can pass variables to the ansible upon invoking it using `--extra-vars` or `-e`. + +``` +--extra-vars "blaat=piet henk=joop" +--extra-vars '{"blaat":"piet","henk":"joop"}' +--extra-vars "@file.json" +--extra-vars "@file.yml" +``` diff --git a/3 resources/ansible/find.md b/3 resources/ansible/find.md new file mode 100644 index 0000000..1f4de51 --- /dev/null +++ b/3 resources/ansible/find.md @@ -0,0 +1,9 @@ +``` +- name: Find file with size >= 50G + ansible.builtin.find: + paths: + - /var/lib/nova/instances/_base + size: 50g +``` +--- +https://docs.ansible.com/ansible/latest/collections/ansible/builtin/find_module.html
\ No newline at end of file diff --git a/3 resources/ansible/loop-output.md b/3 resources/ansible/loop-output.md new file mode 100644 index 0000000..30a0848 --- /dev/null +++ b/3 resources/ansible/loop-output.md @@ -0,0 +1 @@ +Using the label field we can select what to print, instead of the whole item.
\ No newline at end of file diff --git a/3 resources/ansible/loop-register.md b/3 resources/ansible/loop-register.md new file mode 100644 index 0000000..bab52a5 --- /dev/null +++ b/3 resources/ansible/loop-register.md @@ -0,0 +1,41 @@ +When using a loop the registered variable will contain the results of all the responses: +```JSON +{ + "changed": true, + "msg": "All items completed", + "results": [ + { + "changed": true, + "cmd": "echo \"one\" ", + "delta": "0:00:00.003110", + "end": "2013-12-19 12:00:05.187153", + "invocation": { + "module_args": "echo \"one\"", + "module_name": "shell" + }, + "item": "one", + "rc": 0, + "start": "2013-12-19 12:00:05.184043", + "stderr": "", + "stdout": "one" + }, + { + "changed": true, + "cmd": "echo \"two\" ", + "delta": "0:00:00.002920", + "end": "2013-12-19 12:00:05.245502", + "invocation": { + "module_args": "echo \"two\"", + "module_name": "shell" + }, + "item": "two", + "rc": 0, + "start": "2013-12-19 12:00:05.242582", + "stderr": "", + "stdout": "two" + } + ] +} +``` + +Thus we can loop over each results `{{ registered_var.results }}`.
\ No newline at end of file diff --git a/3 resources/ansible/loop-results-prior-loop.md b/3 resources/ansible/loop-results-prior-loop.md new file mode 100644 index 0000000..cbd50a2 --- /dev/null +++ b/3 resources/ansible/loop-results-prior-loop.md @@ -0,0 +1,30 @@ +```YAML +- name: First loop + loop: {{ blaat }} + register: loooped + ... + +- name: Second loop + loop: {{ loooped.results }} + when: item.skipped is not defined +``` + +this loops over results of prior loop only but does not process any skipped results. + +In case the results array contains another array that we want to loop over, for example when we did a shell command in a loop we can use extract: +``` +- name: Print magic + ansible.builtin.debug: + msg: "{{ ['stdout_lines'] | map('extract', item) }}" + loop: "{{ magic.results }}" +``` + +A cool example of this in practice: +``` +- name: Print blockpull command + ansible.builtin.debug: + msg: "virsh blockpull {{ ['stdout_lines'] | map('extract', item) | list | flatten | join(' ') }}" + loop: "{{ domain_and_disk_paths.results }}" + loop_control: + label: "{{ inventory_hostname }}" +```
\ No newline at end of file diff --git a/3 resources/ansible/nested-loops.md b/3 resources/ansible/nested-loops.md new file mode 100644 index 0000000..3a5be97 --- /dev/null +++ b/3 resources/ansible/nested-loops.md @@ -0,0 +1,3 @@ +Nested loops can be done not by nesting loops over two separate lists, but by looping over the [[cartesian-product]] of the two using the `product` filter. + +`{{ list1 | product(list) | list }}`
\ No newline at end of file diff --git a/3 resources/ansible/set-facts.md b/3 resources/ansible/set-facts.md new file mode 100644 index 0000000..e94eeb6 --- /dev/null +++ b/3 resources/ansible/set-facts.md @@ -0,0 +1 @@ +These can be used to set and update variables specific to nodes, at runtime.
\ No newline at end of file |