with_items question

Hello,

I am kinda new to Ansible, I would appreciate if you could assist me with a question, I am trying to do the following in a role in a playbook

1- Copy 3 zip files to a specific destination
2- Unzip only the first
3- Rename a directory in the unzipped location
4- Unzip the remaining 2 files

I mainly have 2 problems, the first is that only the first file is copied and second the when condition is apparently not written correctly

This is my list in the role’s default directory

installer_archives:

  • { name: V978971-01.zip, dest: “{{oracle_home_gi}}” }
  • { name: p28828717_180000_Linux-x86-64.zip, dest: “{{oracle_stage}}” }
  • { name: p6880880_121010_Linux-x86-64.zip, dest: “{{oracle_home_gi}}” }

This is my role:

==> Copy installer files

  • name: Copy installer files
    copy:
    src: ‘{{ item }}’
    dest: ‘{{ oracle_stage }}/’
    owner: ‘{{ grid_install_user }}’
    mode: 0644
    group: ‘{{ oracle_group }}’
    with_items: ‘{{ installer_archives }}’
    when: ansible_hostname==groups[group_names | first] | first

  • name: install-home-gi | Extract files to ORACLE_HOME (gi)
    unarchive:
    src: ‘{{ oracle_stage }}/{{ item }}’
    dest: ‘{{ item.dest }}’
    copy: no
    owner: ‘{{ grid_install_user }}’
    group: ‘{{ oracle_group }}’
    with_items: ‘{{installer_archives}}’
    args:
    creates: ‘{{ oracle_home_gi }}/root.sh’
    when: ansible_hostname==groups[group_names | first] | first and “{{installer_archives}}” | first

  • name: install-home-gi | Move old Opatch directory
    shell: “mv {{ oracle_home_gi }}/OPatch {{ oracle_home_gi }}/OPatch.old”

  • name: install-home-gi | Extract Opatch and BP
    unarchive:
    src: ‘{{ oracle_stage }}/{{ item.name }}’
    dest: ‘{{ item.dest }}’
    copy: no
    owner: ‘{{ grid_install_user }}’

group: ‘{{ oracle_group }}’
with_items: ‘{{installer_archives}}’
when: ansible_hostname==groups[group_names | first] | first and not {{installer_archives}} | first

Thank you

Hello, Any help please?

Hello, Any help please?

A lot of fundamental problems. I'll try to address them all.

Hello,

I am kinda new to Ansible, I would appreciate if you could assist me with
a question, I am trying to do the following in a role in a playbook

1- Copy 3 zip files to a specific destination
2- Unzip only the first
3- Rename a directory in the unzipped location
4- Unzip the remaining 2 files

I mainly have 2 problems, the first is that only the first file is copied
and second the when condition is apparently not written correctly

This is not very helpful and probably the reason you haven't gotten an answer (plus is a weekend).
You should always provide the full output of the run, very hard to help without since it takes a lot more time.

This is my list in the role's default directory
  installer_archives:
  - { name: V978971-01.zip, dest: "{{oracle_home_gi}}" }
  - { name: p28828717_180000_Linux-x86-64.zip, dest: "{{oracle_stage}}" }
  - { name: p6880880_121010_Linux-x86-64.zip, dest: "{{oracle_home_gi}}" }

This is my role:
# ==> Copy installer files
- name: Copy installer files
  copy:
    src: '{{ item }}'
    dest: '{{ oracle_stage }}/'
    owner: '{{ grid_install_user }}'
    mode: 0644
    group: '{{ oracle_group }}'
  with_items: '{{ installer_archives }}'
  when: ansible_hostname==groups[group_names | first] | first

Every item will contain a dict with two keys, item.name and item.dest.
You are here using item that will contain both key and value.
I guess you are after item.name in your src: and not both of them since that make no sense.

- name: install-home-gi | Extract files to ORACLE_HOME (gi)
  unarchive:
    src: '{{ oracle_stage }}/{{ item }}'
    dest: '{{ item.dest }}'
    copy: no
    owner: '{{ grid_install_user }}'
    group: '{{ oracle_group }}'
  with_items: '{{installer_archives}}'
  args:
    creates: '{{ oracle_home_gi }}/root.sh'
  when: ansible_hostname==groups[group_names | first] | first and
"{{installer_archives}}" | first

Same here, your item is a dict and your are probably looking for item.name and not just item.

- name: install-home-gi | Move old Opatch directory
  shell: "mv {{ oracle_home_gi }}/OPatch {{ oracle_home_gi }}/OPatch.old"

- name: install-home-gi | Extract Opatch and BP
  unarchive:
    src: '{{ oracle_stage }}/{{ item.name }}'
    dest: '{{ item.dest }}'
    copy: no
    owner: '{{ grid_install_user }}'
    group: '{{ oracle_group }}'
  with_items: '{{installer_archives}}'
  when: ansible_hostname==groups[group_names | first] | first and not
{{installer_archives}} | first

Here you actually have item.name and this is probably the only task that works.

Have you run your playbook with -vvvvv to see what is actually happening?

Thank you for all the help, this is the output of the first task with -vvvvv when I try item.name

TASK [oraswgi-install : Copy installer files] *****************************************************************************************
task path: /root/MY_RAC/roles/oraswgi-install/tasks/main.yml:25
Read vars_file ‘infra-vars.yml’
Read vars_file ‘db-vars.yml’
Read vars_file ‘secrets.yml’
fatal: [oradb1]: FAILED! => {
“msg”: “The task includes an option with an undefined variable. The error was: ‘ansible.utils.unsafe_proxy.AnsibleUnsafeText object’ has no attribute ‘name’\n\nThe error appears to have been in ‘/root/MY_RAC/roles/oraswgi-install/tasks/main.yml’: line 25, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n# ==> Copy installer files\n- name: Copy installer files\n ^ here\n”
}
skipping: [oradb2] => (item=V978971-01.zip) => {
“changed”: false,
“item”: “V978971-01.zip”,
“skip_reason”: “Conditional result was False”
}
Read vars_file ‘infra-vars.yml’
Read vars_file ‘db-vars.yml’
Read vars_file ‘secrets.yml’

The modified task is:

==> Copy installer files

  • name: Copy installer files
    copy:
    src: ‘{{ item.name }}’
    dest: ‘{{ oracle_stage }}/’
    owner: ‘{{ grid_install_user }}’
    mode: 0644
    group: ‘{{ oracle_group }}’
    with_items: ‘{{ installer_archives }}’
    when: ansible_hostname==groups[group_names | first] | first