Ansible Playbook with multiple lists in the variables with multiple with_items calls in the tasks
The sample playbook looks like this. This has four lists defined in the vars: section.
First is the list of the packages to be installed name of the list being "packages" in the playbook.
Second list having name as "stopservice" is the list of the services to be stopped
Third list is the list of the services to be started also enabled with the name of the list being as "startservice"
As can be seen in the playbook the call to each of the items of a list is in the form of
"{{item.name}}"
with_items takes the values as
"{{packages}}" or "{{startservice}}" or "{{stopservice}}" as the name of the list
The fourth list is the list of the files to be got from a URL and to be copied to a certain location on the servers
The list name is "urls"
The name is the identifier
url is the URL wherefrom the file can be downloaded.
dest is the location on the server on where the file has to be copied to while being received using the get_url module of ansible
here the important values to be passed to the get_url module are "url" and "dest"
the "url" and "dest" are different for each item in the list "urls"
The pairs of url and dest are passed to the get_url module as
get_url:
url: "{{item.url}}"
dest: "{{item.dest}}"
with_items: "{{urls}}"
Also note the use of the "force" option of the get_url to ensure that the target file is overwritten when the contents at the source url change.
Also note the use of the "force" option of the get_url to ensure that the target file is overwritten when the contents at the source url change.
Here is the example playbook
[devops@ansible playbooks]$ cat firstplaybook.yml
---
- name: install packages
hosts: all
vars:
- packages:
- name: chrony
- name: bash-completion
- name: yum-utils
- name: bind-utils
- name: net-tools
- name: ntp
- name: firewalld
- name: wget
- name: curl
- name: unzip
- stopservice:
- name: firewalld
- startservice:
- name: chronyd
- name: ntpd
- urls:
- name: hosts
url: http://ansible.lab.example.com/config/hosts
dest: /etc/hosts
- name: nsswitch.conf
url: http://ansible.lab.example.com/config/nsswitch.conf
dest: /etc/nsswitch.conf
- name: profile
url: http://ansible.lab.example.com/config/profile
dest: /etc/profile
- name: resolv.conf
url: http://ansible.lab.example.com/config/resolv.conf
dest: /etc/resolv.conf
- name: selinuxconfig
url: http://ansible.lab.example.com/config/selinuxconfig
dest: /etc/selinux/config
- name: yum.conf
url: http://ansible.lab.example.com/config/yum.conf
dest: /etc/yum.conf
tasks:
- name: install package
yum:
name: "{{item.name}}"
state: latest
with_items:
- "{{packages}}"
- name: stop the services
service:
state: stopped
enabled: false
name: "{{item.name}}"
with_items:
- "{{stopservice}}"
- name: start and enable services
service:
name: "{{item.name}}"
state: started
enabled: true
with_items:
- "{{startservice}}"
- name: copy configuration files from the URLs
get_url:
url: "{{item.url}}"
dest: "{{item.dest}}"
use_proxy: no
force: yes
with_items:
- "{{urls}}"
[devops@ansible playbooks]$
---
- name: install packages
hosts: all
vars:
- packages:
- name: chrony
- name: bash-completion
- name: yum-utils
- name: bind-utils
- name: net-tools
- name: ntp
- name: firewalld
- name: wget
- name: curl
- name: unzip
- stopservice:
- name: firewalld
- startservice:
- name: chronyd
- name: ntpd
- urls:
- name: hosts
url: http://ansible.lab.example.com/config/hosts
dest: /etc/hosts
- name: nsswitch.conf
url: http://ansible.lab.example.com/config/nsswitch.conf
dest: /etc/nsswitch.conf
- name: profile
url: http://ansible.lab.example.com/config/profile
dest: /etc/profile
- name: resolv.conf
url: http://ansible.lab.example.com/config/resolv.conf
dest: /etc/resolv.conf
- name: selinuxconfig
url: http://ansible.lab.example.com/config/selinuxconfig
dest: /etc/selinux/config
- name: yum.conf
url: http://ansible.lab.example.com/config/yum.conf
dest: /etc/yum.conf
tasks:
- name: install package
yum:
name: "{{item.name}}"
state: latest
with_items:
- "{{packages}}"
- name: stop the services
service:
state: stopped
enabled: false
name: "{{item.name}}"
with_items:
- "{{stopservice}}"
- name: start and enable services
service:
name: "{{item.name}}"
state: started
enabled: true
with_items:
- "{{startservice}}"
- name: copy configuration files from the URLs
get_url:
url: "{{item.url}}"
dest: "{{item.dest}}"
use_proxy: no
force: yes
with_items:
- "{{urls}}"
[devops@ansible playbooks]$
------------------------------ ----
Run of the Playbook
------------------------------ ----
[devops@ansible playbooks]$ ansible-playbook firstplaybook.yml
PLAY [install packages] ********************************************************************************************
TASK [Gathering Facts] *********************************************************************************************
ok: [ansible.lab.example.com]
ok: [server2.lab.example.com]
ok: [server1.lab.example.com]
ok: [server3.lab.example.com]
TASK [install package] *********************************************************************************************
ok: [server1.lab.example.com] => (item={u'name': u'chrony'})
ok: [server2.lab.example.com] => (item={u'name': u'chrony'})
ok: [ansible.lab.example.com] => (item={u'name': u'chrony'})
ok: [server3.lab.example.com] => (item={u'name': u'chrony'})
ok: [server1.lab.example.com] => (item={u'name': u'bash-completion'})
ok: [ansible.lab.example.com] => (item={u'name': u'bash-completion'})
ok: [server2.lab.example.com] => (item={u'name': u'bash-completion'})
ok: [server3.lab.example.com] => (item={u'name': u'bash-completion'})
ok: [server1.lab.example.com] => (item={u'name': u'yum-utils'})
ok: [server2.lab.example.com] => (item={u'name': u'yum-utils'})
ok: [ansible.lab.example.com] => (item={u'name': u'yum-utils'})
ok: [server3.lab.example.com] => (item={u'name': u'yum-utils'})
ok: [server1.lab.example.com] => (item={u'name': u'bind-utils'})
ok: [server2.lab.example.com] => (item={u'name': u'bind-utils'})
ok: [ansible.lab.example.com] => (item={u'name': u'bind-utils'})
ok: [server3.lab.example.com] => (item={u'name': u'bind-utils'})
ok: [server2.lab.example.com] => (item={u'name': u'net-tools'})
ok: [server1.lab.example.com] => (item={u'name': u'net-tools'})
ok: [ansible.lab.example.com] => (item={u'name': u'net-tools'})
ok: [server3.lab.example.com] => (item={u'name': u'net-tools'})
ok: [server1.lab.example.com] => (item={u'name': u'ntp'})
ok: [server2.lab.example.com] => (item={u'name': u'ntp'})
ok: [ansible.lab.example.com] => (item={u'name': u'ntp'})
ok: [ansible.lab.example.com] => (item={u'name': u'firewalld'})
ok: [server3.lab.example.com] => (item={u'name': u'ntp'})
ok: [server1.lab.example.com] => (item={u'name': u'firewalld'})
ok: [server2.lab.example.com] => (item={u'name': u'firewalld'})
ok: [ansible.lab.example.com] => (item={u'name': u'wget'})
ok: [server3.lab.example.com] => (item={u'name': u'firewalld'})
ok: [server1.lab.example.com] => (item={u'name': u'wget'})
ok: [server2.lab.example.com] => (item={u'name': u'wget'})
ok: [ansible.lab.example.com] => (item={u'name': u'curl'})
ok: [server1.lab.example.com] => (item={u'name': u'curl'})
ok: [server3.lab.example.com] => (item={u'name': u'wget'})
ok: [ansible.lab.example.com] => (item={u'name': u'unzip'})
ok: [server2.lab.example.com] => (item={u'name': u'curl'})
ok: [server1.lab.example.com] => (item={u'name': u'unzip'})
ok: [server3.lab.example.com] => (item={u'name': u'curl'})
ok: [server2.lab.example.com] => (item={u'name': u'unzip'})
ok: [server3.lab.example.com] => (item={u'name': u'unzip'})
TASK [stop the services] *******************************************************************************************
ok: [ansible.lab.example.com] => (item={u'name': u'firewalld'})
ok: [server2.lab.example.com] => (item={u'name': u'firewalld'})
ok: [server3.lab.example.com] => (item={u'name': u'firewalld'})
ok: [server1.lab.example.com] => (item={u'name': u'firewalld'})
TASK [start and enable services] ***********************************************************************************
changed: [ansible.lab.example.com] => (item={u'name': u'chronyd'})
changed: [ansible.lab.example.com] => (item={u'name': u'ntpd'})
changed: [server2.lab.example.com] => (item={u'name': u'chronyd'})
changed: [server3.lab.example.com] => (item={u'name': u'chronyd'})
changed: [server1.lab.example.com] => (item={u'name': u'chronyd'})
changed: [server1.lab.example.com] => (item={u'name': u'ntpd'})
changed: [server3.lab.example.com] => (item={u'name': u'ntpd'})
changed: [server2.lab.example.com] => (item={u'name': u'ntpd'})
TASK [copy configuration files from the URLs] **********************************************************************
ok: [ansible.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/hosts', u'dest': u'/etc/hosts', u'name': u'hosts'})
ok: [ansible.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/nsswitch.conf', u'dest': u'/etc/nsswitch.conf', u'name': u'nsswitch.conf'})
ok: [ansible.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/profile', u'dest': u'/etc/profile', u'name': u'profile'})
ok: [server3.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/hosts', u'dest': u'/etc/hosts', u'name': u'hosts'})
ok: [server1.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/hosts', u'dest': u'/etc/hosts', u'name': u'hosts'})
ok: [server2.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/hosts', u'dest': u'/etc/hosts', u'name': u'hosts'})
ok: [ansible.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/resolv.conf', u'dest': u'/etc/resolv.conf', u'name': u'resolv.conf'})
ok: [ansible.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/selinuxconfig', u'dest': u'/etc/selinux/config', u'name': u'selinuxconfig'})
ok: [ansible.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/yum.conf', u'dest': u'/etc/yum.conf', u'name': u'yum.conf'})
ok: [server1.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/nsswitch.conf', u'dest': u'/etc/nsswitch.conf', u'name': u'nsswitch.conf'})
ok: [server3.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/nsswitch.conf', u'dest': u'/etc/nsswitch.conf', u'name': u'nsswitch.conf'})
ok: [server2.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/nsswitch.conf', u'dest': u'/etc/nsswitch.conf', u'name': u'nsswitch.conf'})
changed: [server3.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/profile', u'dest': u'/etc/profile', u'name': u'profile'})
changed: [server2.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/profile', u'dest': u'/etc/profile', u'name': u'profile'})
changed: [server1.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/profile', u'dest': u'/etc/profile', u'name': u'profile'})
ok: [server3.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/resolv.conf', u'dest': u'/etc/resolv.conf', u'name': u'resolv.conf'})
ok: [server1.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/resolv.conf', u'dest': u'/etc/resolv.conf', u'name': u'resolv.conf'})
ok: [server2.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/resolv.conf', u'dest': u'/etc/resolv.conf', u'name': u'resolv.conf'})
ok: [server1.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/selinuxconfig', u'dest': u'/etc/selinux/config', u'name': u'selinuxconfig'})
ok: [server3.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/selinuxconfig', u'dest': u'/etc/selinux/config', u'name': u'selinuxconfig'})
ok: [server2.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/selinuxconfig', u'dest': u'/etc/selinux/config', u'name': u'selinuxconfig'})
ok: [server1.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/yum.conf', u'dest': u'/etc/yum.conf', u'name': u'yum.conf'})
ok: [server2.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/yum.conf', u'dest': u'/etc/yum.conf', u'name': u'yum.conf'})
ok: [server3.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/yum.conf', u'dest': u'/etc/yum.conf', u'name': u'yum.conf'})
PLAY RECAP *********************************************************************************************************
ansible.lab.example.com : ok=5 changed=1 unreachable=0 failed=0
server1.lab.example.com : ok=5 changed=2 unreachable=0 failed=0
server2.lab.example.com : ok=5 changed=2 unreachable=0 failed=0
server3.lab.example.com : ok=5 changed=2 unreachable=0 failed=0
PLAY [install packages] ********************************************************************************************
TASK [Gathering Facts] *********************************************************************************************
ok: [ansible.lab.example.com]
ok: [server2.lab.example.com]
ok: [server1.lab.example.com]
ok: [server3.lab.example.com]
TASK [install package] *********************************************************************************************
ok: [server1.lab.example.com] => (item={u'name': u'chrony'})
ok: [server2.lab.example.com] => (item={u'name': u'chrony'})
ok: [ansible.lab.example.com] => (item={u'name': u'chrony'})
ok: [server3.lab.example.com] => (item={u'name': u'chrony'})
ok: [server1.lab.example.com] => (item={u'name': u'bash-completion'})
ok: [ansible.lab.example.com] => (item={u'name': u'bash-completion'})
ok: [server2.lab.example.com] => (item={u'name': u'bash-completion'})
ok: [server3.lab.example.com] => (item={u'name': u'bash-completion'})
ok: [server1.lab.example.com] => (item={u'name': u'yum-utils'})
ok: [server2.lab.example.com] => (item={u'name': u'yum-utils'})
ok: [ansible.lab.example.com] => (item={u'name': u'yum-utils'})
ok: [server3.lab.example.com] => (item={u'name': u'yum-utils'})
ok: [server1.lab.example.com] => (item={u'name': u'bind-utils'})
ok: [server2.lab.example.com] => (item={u'name': u'bind-utils'})
ok: [ansible.lab.example.com] => (item={u'name': u'bind-utils'})
ok: [server3.lab.example.com] => (item={u'name': u'bind-utils'})
ok: [server2.lab.example.com] => (item={u'name': u'net-tools'})
ok: [server1.lab.example.com] => (item={u'name': u'net-tools'})
ok: [ansible.lab.example.com] => (item={u'name': u'net-tools'})
ok: [server3.lab.example.com] => (item={u'name': u'net-tools'})
ok: [server1.lab.example.com] => (item={u'name': u'ntp'})
ok: [server2.lab.example.com] => (item={u'name': u'ntp'})
ok: [ansible.lab.example.com] => (item={u'name': u'ntp'})
ok: [ansible.lab.example.com] => (item={u'name': u'firewalld'})
ok: [server3.lab.example.com] => (item={u'name': u'ntp'})
ok: [server1.lab.example.com] => (item={u'name': u'firewalld'})
ok: [server2.lab.example.com] => (item={u'name': u'firewalld'})
ok: [ansible.lab.example.com] => (item={u'name': u'wget'})
ok: [server3.lab.example.com] => (item={u'name': u'firewalld'})
ok: [server1.lab.example.com] => (item={u'name': u'wget'})
ok: [server2.lab.example.com] => (item={u'name': u'wget'})
ok: [ansible.lab.example.com] => (item={u'name': u'curl'})
ok: [server1.lab.example.com] => (item={u'name': u'curl'})
ok: [server3.lab.example.com] => (item={u'name': u'wget'})
ok: [ansible.lab.example.com] => (item={u'name': u'unzip'})
ok: [server2.lab.example.com] => (item={u'name': u'curl'})
ok: [server1.lab.example.com] => (item={u'name': u'unzip'})
ok: [server3.lab.example.com] => (item={u'name': u'curl'})
ok: [server2.lab.example.com] => (item={u'name': u'unzip'})
ok: [server3.lab.example.com] => (item={u'name': u'unzip'})
TASK [stop the services] *******************************************************************************************
ok: [ansible.lab.example.com] => (item={u'name': u'firewalld'})
ok: [server2.lab.example.com] => (item={u'name': u'firewalld'})
ok: [server3.lab.example.com] => (item={u'name': u'firewalld'})
ok: [server1.lab.example.com] => (item={u'name': u'firewalld'})
TASK [start and enable services] ***********************************************************************************
changed: [ansible.lab.example.com] => (item={u'name': u'chronyd'})
changed: [ansible.lab.example.com] => (item={u'name': u'ntpd'})
changed: [server2.lab.example.com] => (item={u'name': u'chronyd'})
changed: [server3.lab.example.com] => (item={u'name': u'chronyd'})
changed: [server1.lab.example.com] => (item={u'name': u'chronyd'})
changed: [server1.lab.example.com] => (item={u'name': u'ntpd'})
changed: [server3.lab.example.com] => (item={u'name': u'ntpd'})
changed: [server2.lab.example.com] => (item={u'name': u'ntpd'})
TASK [copy configuration files from the URLs] **********************************************************************
ok: [ansible.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/hosts', u'dest': u'/etc/hosts', u'name': u'hosts'})
ok: [ansible.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/nsswitch.conf', u'dest': u'/etc/nsswitch.conf', u'name': u'nsswitch.conf'})
ok: [ansible.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/profile', u'dest': u'/etc/profile', u'name': u'profile'})
ok: [server3.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/hosts', u'dest': u'/etc/hosts', u'name': u'hosts'})
ok: [server1.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/hosts', u'dest': u'/etc/hosts', u'name': u'hosts'})
ok: [server2.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/hosts', u'dest': u'/etc/hosts', u'name': u'hosts'})
ok: [ansible.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/resolv.conf', u'dest': u'/etc/resolv.conf', u'name': u'resolv.conf'})
ok: [ansible.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/selinuxconfig', u'dest': u'/etc/selinux/config', u'name': u'selinuxconfig'})
ok: [ansible.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/yum.conf', u'dest': u'/etc/yum.conf', u'name': u'yum.conf'})
ok: [server1.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/nsswitch.conf', u'dest': u'/etc/nsswitch.conf', u'name': u'nsswitch.conf'})
ok: [server3.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/nsswitch.conf', u'dest': u'/etc/nsswitch.conf', u'name': u'nsswitch.conf'})
ok: [server2.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/nsswitch.conf', u'dest': u'/etc/nsswitch.conf', u'name': u'nsswitch.conf'})
changed: [server3.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/profile', u'dest': u'/etc/profile', u'name': u'profile'})
changed: [server2.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/profile', u'dest': u'/etc/profile', u'name': u'profile'})
changed: [server1.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/profile', u'dest': u'/etc/profile', u'name': u'profile'})
ok: [server3.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/resolv.conf', u'dest': u'/etc/resolv.conf', u'name': u'resolv.conf'})
ok: [server1.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/resolv.conf', u'dest': u'/etc/resolv.conf', u'name': u'resolv.conf'})
ok: [server2.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/resolv.conf', u'dest': u'/etc/resolv.conf', u'name': u'resolv.conf'})
ok: [server1.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/selinuxconfig', u'dest': u'/etc/selinux/config', u'name': u'selinuxconfig'})
ok: [server3.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/selinuxconfig', u'dest': u'/etc/selinux/config', u'name': u'selinuxconfig'})
ok: [server2.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/selinuxconfig', u'dest': u'/etc/selinux/config', u'name': u'selinuxconfig'})
ok: [server1.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/yum.conf', u'dest': u'/etc/yum.conf', u'name': u'yum.conf'})
ok: [server2.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/yum.conf', u'dest': u'/etc/yum.conf', u'name': u'yum.conf'})
ok: [server3.lab.example.com] => (item={u'url': u'http://ansible.lab.example.com/config/yum.conf', u'dest': u'/etc/yum.conf', u'name': u'yum.conf'})
PLAY RECAP *********************************************************************************************************
ansible.lab.example.com : ok=5 changed=1 unreachable=0 failed=0
server1.lab.example.com : ok=5 changed=2 unreachable=0 failed=0
server2.lab.example.com : ok=5 changed=2 unreachable=0 failed=0
server3.lab.example.com : ok=5 changed=2 unreachable=0 failed=0