diff --git a/content/debian-update-with-ansible.md b/content/debian-update-with-ansible.md index 4acf844..5718a6a 100644 --- a/content/debian-update-with-ansible.md +++ b/content/debian-update-with-ansible.md @@ -15,49 +15,62 @@ I figured I'd share the playbook I use to update the Debian packages ! I modified [this gist](https://gist.github.com/maethor/380676f6b1cec8cc7439) to only use apt-get instead of both apt-get and aptitude. ```yaml -- hosts: debian - remote_user: admin - become: yes - become_method: sudo +- hosts: all tasks: + - name: update cache apt: update_cache=yes + - name: list packages to upgrade (1/2) shell: apt-get upgrade --show-upgraded --assume-no | grep -A1 'The following packages will be upgraded:' | sed '1d;s/ //;s/ /\n/g' register: updates changed_when: False + - name: list packages to upgrade (2/2) debug: msg="{{ updates.stdout_lines | count }} packages to upgrade ({{ updates.stdout_lines | join(', ') }})" when: (updates.stdout_lines) + - name: upgrade packages apt: upgrade=dist when: (updates.stdout_lines) + - name: check what the new version is shell: lsb_release -r | awk '{print $2}' changed_when: False register: new_release + - name: notify distribution version upgrade debug: msg="Debian has been upgraded from {{ ansible_lsb.release }} to {{ new_release.stdout }}" when: ansible_lsb.release != new_release.stdout + - name: /wheezy/ install the debian-goodies package if it is missing apt: name=debian-goodies state=present when: ansible_distribution_release == 'wheezy' + - name: /jessie/ install the needrestart package if it is missing apt: name=needrestart state=present default_release=jessie-backports when: ansible_distribution_release == 'jessie' - - name: /wheezy/ list services to restart (1/2) - shell: checkrestart | grep ^service | awk '{print $2}' - register: services + + - name: /wheezy/ list services to restart (1/3) + shell: checkrestart | awk '/^service/{print $2}' + register: wheezy_services changed_when: False when: ansible_distribution_release == 'wheezy' - - name: /jessie/ list services to restart (1/2) - shell: needrestart -bl | grep ^NEEDRESTART-SVC | awk '{print $2}' - register: services + + - name: /jessie/ list services to restart (1/3) + shell: needrestart -bl | awk '/^NEEDRESTART-SVC/{print $2}' + register: jessie_services changed_when: False when: ansible_distribution_release != 'wheezy' - - name: list services to restart (2/2) + + - name: merge services list (2/3) + set_fact: + services: "{{ wheezy_services if ansible_distribution_release == 'wheezy' else jessie_services }}" + + - name: list services to restart (3/3) debug: msg="{{ services.stdout_lines | count }} services to restart ({{ services.stdout_lines | join (', ') }})" when: (services.stdout_lines) + - name: cache cleanup shell: apt-get autoclean ``` diff --git a/output/debian-updates-with-ansible.html b/output/debian-updates-with-ansible.html index bf5d4a5..7e3225b 100644 --- a/output/debian-updates-with-ansible.html +++ b/output/debian-updates-with-ansible.html @@ -121,49 +121,62 @@

I figured I'd share the playbook I use to update the Debian packages !

The playbook

I modified this gist to only use apt-get instead of both apt-get and aptitude.

-
- hosts: debian
-  remote_user: admin
-  become: yes
-  become_method: sudo
+
- hosts: all
   tasks:
+
     - name: update cache
       apt: update_cache=yes
+
     - name: list packages to upgrade (1/2)
       shell: apt-get upgrade --show-upgraded --assume-no | grep -A1 'The following packages will be upgraded:' | sed '1d;s/  //;s/ /\n/g'
       register: updates
       changed_when: False
+
     - name: list packages to upgrade (2/2)
       debug: msg="{{ updates.stdout_lines | count }} packages to upgrade ({{ updates.stdout_lines | join(', ') }})"
       when: (updates.stdout_lines)
+
     - name: upgrade packages
       apt: upgrade=dist
       when: (updates.stdout_lines)
+
     - name: check what the new version is
       shell: lsb_release -r | awk '{print $2}'
       changed_when: False
       register: new_release
+
     - name: notify distribution version upgrade
       debug: msg="Debian has been upgraded from {{ ansible_lsb.release }} to {{ new_release.stdout }}"
       when: ansible_lsb.release != new_release.stdout
+
     - name: /wheezy/ install the debian-goodies package if it is missing
       apt: name=debian-goodies state=present
       when: ansible_distribution_release == 'wheezy'
+
     - name: /jessie/ install the needrestart package if it is missing
       apt: name=needrestart state=present default_release=jessie-backports
       when: ansible_distribution_release == 'jessie'
-    - name: /wheezy/ list services to restart (1/2)
-      shell: checkrestart | grep ^service | awk '{print $2}'
-      register: services
+
+    - name: /wheezy/ list services to restart (1/3)
+      shell: checkrestart | awk '/^service/{print $2}'
+      register: wheezy_services
       changed_when: False
       when: ansible_distribution_release == 'wheezy'
-    - name: /jessie/ list services to restart (1/2)
-      shell: needrestart -bl | grep ^NEEDRESTART-SVC | awk '{print $2}'
-      register: services
+
+    - name: /jessie/ list services to restart (1/3)
+      shell: needrestart -bl | awk '/^NEEDRESTART-SVC/{print $2}'
+      register: jessie_services
       changed_when: False
       when: ansible_distribution_release != 'wheezy'
-    - name: list services to restart (2/2)
+
+    - name: merge services list (2/3)
+      set_fact:
+        services: "{{ wheezy_services if ansible_distribution_release == 'wheezy' else jessie_services }}"
+
+    - name: list services to restart (3/3)
       debug: msg="{{ services.stdout_lines | count }} services to restart ({{ services.stdout_lines | join (', ') }})"
       when: (services.stdout_lines)
+
     - name: cache cleanup
       shell: apt-get autoclean
 
diff --git a/output/rss.xml b/output/rss.xml index a403a60..b8a9830 100644 --- a/output/rss.xml +++ b/output/rss.xml @@ -631,49 +631,62 @@ bind &#39;&quot;&#39; choose-window <p>I figured I'd share the playbook I use to update the Debian packages !</p> <h2>The playbook</h2> <p>I modified <a href="https://gist.github.com/maethor/380676f6b1cec8cc7439">this gist</a> to only use apt-get instead of both apt-get and aptitude.</p> -<div class="highlight"><pre><span></span><span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">hosts</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">debian</span> - <span class="l l-Scalar l-Scalar-Plain">remote_user</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">admin</span> - <span class="l l-Scalar l-Scalar-Plain">become</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">yes</span> - <span class="l l-Scalar l-Scalar-Plain">become_method</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">sudo</span> +<div class="highlight"><pre><span></span><span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">hosts</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">all</span> <span class="l l-Scalar l-Scalar-Plain">tasks</span><span class="p p-Indicator">:</span> + <span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">update cache</span> <span class="l l-Scalar l-Scalar-Plain">apt</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">update_cache=yes</span> + <span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">list packages to upgrade (1/2)</span> <span class="l l-Scalar l-Scalar-Plain">shell</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">apt-get upgrade --show-upgraded --assume-no | grep -A1 &#39;The following packages will be upgraded:&#39; | sed &#39;1d;s/ //;s/ /\n/g&#39;</span> <span class="l l-Scalar l-Scalar-Plain">register</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">updates</span> <span class="l l-Scalar l-Scalar-Plain">changed_when</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">False</span> + <span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">list packages to upgrade (2/2)</span> <span class="l l-Scalar l-Scalar-Plain">debug</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">msg=&quot;{{ updates.stdout_lines | count }} packages to upgrade ({{ updates.stdout_lines | join(&#39;, &#39;) }})&quot;</span> <span class="l l-Scalar l-Scalar-Plain">when</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">(updates.stdout_lines)</span> + <span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">upgrade packages</span> <span class="l l-Scalar l-Scalar-Plain">apt</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">upgrade=dist</span> <span class="l l-Scalar l-Scalar-Plain">when</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">(updates.stdout_lines)</span> + <span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">check what the new version is</span> <span class="l l-Scalar l-Scalar-Plain">shell</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">lsb_release -r | awk &#39;{print $2}&#39;</span> <span class="l l-Scalar l-Scalar-Plain">changed_when</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">False</span> <span class="l l-Scalar l-Scalar-Plain">register</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">new_release</span> + <span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">notify distribution version upgrade</span> <span class="l l-Scalar l-Scalar-Plain">debug</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">msg=&quot;Debian has been upgraded from {{ ansible_lsb.release }} to {{ new_release.stdout }}&quot;</span> <span class="l l-Scalar l-Scalar-Plain">when</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">ansible_lsb.release != new_release.stdout</span> + <span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">/wheezy/ install the debian-goodies package if it is missing</span> <span class="l l-Scalar l-Scalar-Plain">apt</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">name=debian-goodies state=present</span> <span class="l l-Scalar l-Scalar-Plain">when</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">ansible_distribution_release == &#39;wheezy&#39;</span> + <span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">/jessie/ install the needrestart package if it is missing</span> <span class="l l-Scalar l-Scalar-Plain">apt</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">name=needrestart state=present default_release=jessie-backports</span> <span class="l l-Scalar l-Scalar-Plain">when</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">ansible_distribution_release == &#39;jessie&#39;</span> - <span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">/wheezy/ list services to restart (1/2)</span> - <span class="l l-Scalar l-Scalar-Plain">shell</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">checkrestart | grep ^service | awk &#39;{print $2}&#39;</span> - <span class="l l-Scalar l-Scalar-Plain">register</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">services</span> + + <span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">/wheezy/ list services to restart (1/3)</span> + <span class="l l-Scalar l-Scalar-Plain">shell</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">checkrestart | awk &#39;/^service/{print $2}&#39;</span> + <span class="l l-Scalar l-Scalar-Plain">register</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">wheezy_services</span> <span class="l l-Scalar l-Scalar-Plain">changed_when</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">False</span> <span class="l l-Scalar l-Scalar-Plain">when</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">ansible_distribution_release == &#39;wheezy&#39;</span> - <span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">/jessie/ list services to restart (1/2)</span> - <span class="l l-Scalar l-Scalar-Plain">shell</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">needrestart -bl | grep ^NEEDRESTART-SVC | awk &#39;{print $2}&#39;</span> - <span class="l l-Scalar l-Scalar-Plain">register</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">services</span> + + <span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">/jessie/ list services to restart (1/3)</span> + <span class="l l-Scalar l-Scalar-Plain">shell</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">needrestart -bl | awk &#39;/^NEEDRESTART-SVC/{print $2}&#39;</span> + <span class="l l-Scalar l-Scalar-Plain">register</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">jessie_services</span> <span class="l l-Scalar l-Scalar-Plain">changed_when</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">False</span> <span class="l l-Scalar l-Scalar-Plain">when</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">ansible_distribution_release != &#39;wheezy&#39;</span> - <span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">list services to restart (2/2)</span> + + <span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">merge services list (2/3)</span> + <span class="l l-Scalar l-Scalar-Plain">set_fact</span><span class="p p-Indicator">:</span> + <span class="l l-Scalar l-Scalar-Plain">services</span><span class="p p-Indicator">:</span> <span class="s">&quot;{{</span><span class="nv"> </span><span class="s">wheezy_services</span><span class="nv"> </span><span class="s">if</span><span class="nv"> </span><span class="s">ansible_distribution_release</span><span class="nv"> </span><span class="s">==</span><span class="nv"> </span><span class="s">&#39;wheezy&#39;</span><span class="nv"> </span><span class="s">else</span><span class="nv"> </span><span class="s">jessie_services</span><span class="nv"> </span><span class="s">}}&quot;</span> + + <span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">list services to restart (3/3)</span> <span class="l l-Scalar l-Scalar-Plain">debug</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">msg=&quot;{{ services.stdout_lines | count }} services to restart ({{ services.stdout_lines | join (&#39;, &#39;) }})&quot;</span> <span class="l l-Scalar l-Scalar-Plain">when</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">(services.stdout_lines)</span> + <span class="p p-Indicator">-</span> <span class="l l-Scalar l-Scalar-Plain">name</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">cache cleanup</span> <span class="l l-Scalar l-Scalar-Plain">shell</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">apt-get autoclean</span> </pre></div>