Ansible First Steps with apt-get playbook

71 views
Skip to first unread message

Anatol

unread,
Feb 1, 2015, 2:01:04 PM2/1/15
to ansible...@googlegroups.com
Hi there,

I´m starting learning ansible and I can say I´m getting addicted! Great Software!

May one look at my first try for a playbook and tell me if this is the way to go:

Given is following bash code to run only security upgrades on a linux box

TMP=/tmp/security.sources.list
grep security
/etc/apt/sources.list > $TMP
apt
-get upgrade -oDir::Etc::Sourcelist=$TMP "$@"
rm $TMP


Which I would transfer into a playbook like this:

---
- hosts: webservers

  tasks
:

   
- name: update apt Cache
      apt
: update_cache=true
      sudo
: yes

   
- name: Set TMP var
      command
: TMP=/tmp/security.sources.list

   
- name: grep security sources
      command
: grep security /etc/apt/sources.list > $TMP

   
- name: run upgrade
      command
: apt-get upgrade -oDir::Etc::Sourcelist=$TMP "$@"
      sudo
: yes

   
- name: Remove TMP File
      command
: rm $TMP


Thanks for your advise!

- Anatol

Brian Coca

unread,
Feb 2, 2015, 10:01:02 AM2/2/15
to ansible...@googlegroups.com
Each ansible task runs under it's own connection, so setting TMP in
one will not be inherited by the others.

try this:

- hosts: webservers
vars:
tmpfile: /tmp/security.sources.list
tasks:

- name: update apt Cache
apt: update_cache=true
sudo: yes

- name: grep security sources
command: grep security /etc/apt/sources.list > {{tmpfile}}

- name: run upgrade
command: apt-get upgrade -oDir::Etc::Sourcelist={{tmpfile}} "$@"
sudo: yes

- name: Remove TMP File
file: path={{tmpfile}} state=absent



--
Brian Coca

Anatol

unread,
Feb 2, 2015, 12:25:41 PM2/2/15
to ansible...@googlegroups.com
Thanks – this makes sense.

cheers,

Anatol

Brian Coca

unread,
Feb 2, 2015, 12:40:04 PM2/2/15
to ansible...@googlegroups.com
also, when using pipes or redirection use shell module instead of command:

shell: grep security /etc/apt/sources.list > {{tmpfile}}
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/f51571c9-d4b2-4fec-853c-4e632ca19783%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca
Reply all
Reply to author
Forward
0 new messages