[ansible-project] How to append the new group to the existing AllowGroups line in /etc/ssh/sshd_config

2021-10-28 Thread Suresh Karpurapu
Hi Experts,

I would like to append the new group to the existing AllowGroups in 
/etc/ssh/sshd_config file without disturbing the existing group. I tried 
with lineinfile module but unfortunately its failing or overwriting 
existing groups. Can anyone please provide the clue on logic?

existing setup:
# grep AllowGroups /etc/ssh/sshd_config   
AllowGroups x y z

required setup:  let's assume "a" is new group, then i am looking as below
# grep AllowGroups /etc/ssh/sshd_config   
AllowGroups x y z a <-group "a" needs to be added at 
the end of the line

Once again thanks for your help

Regards,
Suresh

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/5d596003-6bfc-46a6-be9c-875d669cf0cen%40googlegroups.com.


Re: [ansible-project] How to append the new group to the existing AllowGroups line in /etc/ssh/sshd_config

2021-10-28 Thread Suresh Karpurapu
Thank you so much Vladimir,

How can we loop if we need to append multiple groups.

Regards,
Suresh


On Thu, 28 Oct 2021, 6:21 pm Vladimir Botka,  wrote:

> On Thu, 28 Oct 2021 05:28:27 -0700 (PDT)
> Suresh Karpurapu  wrote:
>
> > # grep AllowGroups /etc/ssh/sshd_config
> > AllowGroups x y z
> >
> > # grep AllowGroups /etc/ssh/sshd_config
> > AllowGroups x y z a <-group "a" needs to be added at
> > the end of the line
>
> Enable *backrefs* and create non-greedy capture group in front of the
> potentially existent "a" group
>
> - lineinfile:
> path: /etc/ssh/sshd_config
> backrefs: true
> regexp: '^\s*AllowGroups\s+(.*?)(\s+a)*$'
> line: 'AllowGroups \1 a'
>
> The task is idempotent. Quoting from *regexp*
>
> https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html#parameter-regexp
>
>   "When modifying a line the regexp should typically match both the
>   initial state of the line as well as its state after replacement by
>   line to ensure idempotence."
>
> --
> Vladimir Botka
>

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAHedzhJ2WgKYtRyPb3GMEAKRRimCJEsMpSwm9D8O5GXUxvwzuQ%40mail.gmail.com.


Re: [ansible-project] How to append the new group to the existing AllowGroups line in /etc/ssh/sshd_config

2021-11-09 Thread Suresh Karpurapu
thank you so much Vladimir and Jorge

Regards,
Suresh

On Fri, Oct 29, 2021 at 3:11 AM Vladimir Botka  wrote:

> On Fri, 29 Oct 2021 01:57:17 +0530
> Suresh Karpurapu  wrote:
>
> > > > # grep AllowGroups /etc/ssh/sshd_config
> > > > AllowGroups x y z
> > > >
> > > > # grep AllowGroups /etc/ssh/sshd_config
> > > > AllowGroups x y z a <-group "a" needs to be
> added at
> > > > the end of the line
>
> > > Enable *backrefs* and create non-greedy capture group in front of the
> > > potentially existent "a" group
> > >
> > > - lineinfile:
> > > path: /etc/ssh/sshd_config
> > > backrefs: true
> > > regexp: '^\s*AllowGroups\s+(.*?)(\s+a)*$'
> > > line: 'AllowGroups \1 a'
> > >
> > > The task is idempotent. Quoting from *regexp*
> > >
> > >
> https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html#parameter-regexp
> > >
> > >   "When modifying a line the regexp should typically match both the
> > >   initial state of the line as well as its state after replacement by
> > >   line to ensure idempotence."
>
> > How can we loop if we need to append multiple groups.
>
> Put them into a variable, e.g.
>
> - lineinfile:
> path: /etc/ssh/sshd_config
> backrefs: true
> regexp: '^\s*AllowGroups\s+(.*?)(\s+{{ add_groups }})*$'
> line: 'AllowGroups \1 {{ add_groups }}'
>
> --
> Vladimir Botka
>

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAHedzhLP%3DKRUe26pTFNFgNJmQcvYk%3DfM_6TxJdjtMcXRioN4WQ%40mail.gmail.com.


[ansible-project] How mount the volume without changing the mount options

2020-03-13 Thread Suresh Karpurapu
we have the multiple nfs volumes which have different mount options. Hence, 
I don't want to change the mount options when we are doing the nfs volume 
migration. Can you please suggest if there any option to ignore to change 
existing mount options when we are mounting new volume with mount module?

nfsvers=3,rw,bg,hard,rsize=32768,wsize=32768,nointr,tcp,timeo=600,suid
nfsvers=3,rw,bg,hard,rsize=65536,wsize=65536,nointr,proto=tcp

Regards,
Suresh

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/0a943ccb-1c30-4d75-8356-e051bb87d617%40googlegroups.com.


Re: [ansible-project] Need to pass variable and hostname to a playbook from CSV file.

2020-04-08 Thread Suresh Karpurapu
thanks Vladimir, your suggestion helped a lot. Still, i am facing one
challenge while using add_host. I have the csv file with the same server
has different volumes. When i run the playbook, it is displaying last entry
associated to the same but i would like to display all the entries. Can you
suggest on the same?

# cat mounts.csv
host,remote_path,mnt_path
host1,nfsvol1,mount1
host1,nfsvol2,mount2
host1,nfsvol3,mount3
host2,nfsvol1,mount1
host2,nfsvol2,mount2
host2,nfsvol3,mount3

# cat mounts.yml
---
- name: mount the nfsshare in client side
  hosts: localhost
  gather_facts: false
  become: yes
  tasks:
- name: reading volume info from csv
  read_csv:
path: "{{ playbook_dir }}/mounts.csv"
  register: sources
  delegate_to: localhost
- name: Grouping host and volume information
  add_host:
name: "{{ item.host }}"
groups: nfsgroup
var1: "{{ item.remote_path }}"
var2: "{{ item.mnt_path }}"
  loop: "{{ sources.list }}"
- name: list volume details
  hosts: nfsgroup
  become: yes
  gather_facts: false
  tasks:
- debug:
msg:
  - "{{ inventory_hostname }}"
  - "{{ var1 }}"
  - "{{ var2 }}"
...

Output:
==
PLAY [list volume details]
**

TASK [debug]

ok: [host1] => {
"msg": [
"host1",
"nfsvol3",
"mount3"
]
}
ok: [host2] => {
"msg": [
"host2",
"nfsvol3",
"mount3"
]
}

PLAY RECAP
**
localhost  : ok=2changed=1unreachable=0failed=0
   skipped=0rescued=0ignored=0
host1   : ok=1changed=0unreachable=0failed=0skipped=0
 rescued=0ignored=0
host2   : ok=1changed=0unreachable=0failed=0skipped=0
 rescued=0ignored=0


On Sat, Mar 21, 2020 at 3:58 AM Vladimir Botka  wrote:

> On Wed, 18 Mar 2020 07:28:27 -0700 (PDT)
> Pandu jh  wrote:
>
> > I have a CSV file that contains the IP's and it's variables.
> > I need to use the details to log in to the host and use the respective
> > variable's value of the specfic IP's.
> >
> > # cat AnsibleTest.csv
> > ip,var1,var2
> > 10.1.1.1,rchantif1,mechlab
> > 10.1.1.2,rchans01,contlab
>
> Read the the file and create new group of hosts in the first play. The use
> the group in the second play. For example
>
> shell> cat playbook.yml
> - hosts: localhost
>   gather_facts: false
>   tasks:
> - read_csv:
> path: AnsibleTest.csv
>   register: myhosts
> - add_host:
> name: '{{ item.ip }}'
> groups: test_01
> var1: "{{ item.var1 }}"
> var2: "{{ item.var2 }}"
>   loop: "{{ myhosts.list }}"
>
> - hosts: test_01
>   gather_facts: false
>   tasks:
> - debug:
> msg:
>   - "{{ inventory_hostname }}"
>   - "{{ var1 }}"
>   - "{{ var2 }}"
>
> shell> ansible-playbook playbook.yml
> ...
> ok: [10.1.1.1] => {
> "msg": [
> "10.1.1.1",
> "rchantif1",
> "mechlab"
> ]
> }
> ok: [10.1.1.2] => {
> "msg": [
> "10.1.1.2",
> "rchans01",
> "contlab"
> ]
> }
>
> HTH,
>
> -vlado
>
> --
> 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-project+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/20200320232753.5d772d73%40gmail.com
> .
>

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAHedzhKGSj%2Bdc%2BG_GCvC-xvVja2a4zt46-%2BXV-oHuW_wHTLLEw%40mail.gmail.com.


Re: [ansible-project] Re: Need to pass variable and hostname to a playbook from CSV file.

2020-04-08 Thread Suresh Karpurapu
Hi Pandu,

the playbook is working if the csv has single entry associated to the host,
but same playbook is failing if the same host has multiple variables
entries. I tried many ways by using inventory but no luck. Finally, i
choose read_csv and add_host.  If you have working example with inventory,
can you please share with me?

# cat mounts.csv[ working as expected]
host,remote_path,mnt_path
host1,nfsvol1,mount1
host2,nfsvol2,mount2
host3,nfsvol3,mount3


# cat mounts.csv[ working, but it is considering last entry only]
host,remote_path,mnt_path
host1,nfsvol1,mount1
host1,nfsvol2,mount2
host1,nfsvol3,mount3
host2,nfsvol1,mount1
host2,nfsvol2,mount2
host2,nfsvol3,mount3

Regards,
Suresh



On Wed, Apr 8, 2020 at 8:09 PM Pandu jh  wrote:

> Can you try to format your csv data to inventory as below.
>
>  # cat mounts
> host,remote_path,mnt_path
> host1,nfsvol1,mount1
> host1,nfsvol2,mount2
> host1,nfsvol3,mount3
> host2,nfsvol1,mount1
> host2,nfsvol2,mount2
> host2,nfsvol3,mount3
>
> On Saturday, 21 March 2020 00:35:05 UTC+5:30, J wrote:
>>
>>
>> My csv file looks like this and its called ip.csv:
>>
>>
>>
>> ip,username,host
>>
>> 10.1.1.1,rchantif1,mechlab
>>
>> 10.1.1.2,rchans01,contlab
>>
>> 192.168.2.142,vagrant
>>
>>
>>
>>
>>
>> I’m using the read_csv: module to access the data
>>
>>
>>
>> What I did is create a playbook that prints what you are looking for the
>> then I followed up on another task an example of logging into a host with
>> the username and IP. I’m not 100% sure what you want to do but hopefully
>> with the info I provided you have a start to your playbook/tasks.
>>
>>
>>
>> ---
>>
>> - name: read items from csv and store as vars
>>
>>   hosts: localhost
>>
>>   become: false
>>
>>   tasks:
>>
>> - name: read_csv
>>
>>   read_csv:
>>
>> path: ip.csv
>>
>>   register: ip
>>
>> - name: print csv items
>>
>>   debug:
>>
>> msg: "{{ip.list.2.ip}} {{ip.list.2.username}} {{ip.list.2.host}}"
>>
>> - name:
>>
>>   shell: "ssh {{ip.list.2.username}}@{{ip.list.2.ip}} 'ls -la'"
>>
>>
>>
>>
>>
>>
>>
>>
>> On Wednesday, March 18, 2020 at 10:28:27 AM UTC-4, Pandu jh wrote:
>>
>>> I have a CSV file that contains the IP's and it's variables.
>>> I need to use the details to log in to the host and use the respective
>>> variable's value of the specfic IP's.
>>>
>>> Please help me out with this.
>>>
>>> # cat AnsibleTest.csv
>>> ip,var1,var2
>>> 10.1.1.1,rchantif1,mechlab
>>> 10.1.1.2,rchans01,contlab
>>>
>>
>> On Wednesday, March 18, 2020 at 10:28:27 AM UTC-4, Pandu jh wrote:
>>>
>>> I have a CSV file that contains the IP's and it's variables.
>>> I need to use the details to log in to the host and use the respective
>>> variable's value of the specfic IP's.
>>>
>>> Please help me out with this.
>>>
>>> # cat AnsibleTest.csv
>>> ip,var1,var2
>>> 10.1.1.1,rchantif1,mechlab
>>> 10.1.1.2,rchans01,contlab
>>>
>>
>> On Wednesday, March 18, 2020 at 10:28:27 AM UTC-4, Pandu jh wrote:
>>>
>>> I have a CSV file that contains the IP's and it's variables.
>>> I need to use the details to log in to the host and use the respective
>>> variable's value of the specfic IP's.
>>>
>>> Please help me out with this.
>>>
>>> # cat AnsibleTest.csv
>>> ip,var1,var2
>>> 10.1.1.1,rchantif1,mechlab
>>> 10.1.1.2,rchans01,contlab
>>>
>> --
> 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-project+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/c586db4f-82f6-4560-b567-9ca1f125e342%40googlegroups.com
> 
> .
>

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAHedzhJsQV0SZG_LPpQ%3DESh9eMhvcBE1LnWhtdMbM6wQjG_DAA%40mail.gmail.com.


Re: [ansible-project] Need to pass variable and hostname to a playbook from CSV file.

2020-04-08 Thread Suresh Karpurapu
Initially, i tried your approach but engineer has to do more work to make
the format as we are planning to use playbook for nfs volume migration. we
have 1000's of servers with 100's volumes. Hence, the reason, we chosen the
csv format. I have been using inventory based host connection but it is
connecting even if volume is not required to mount/unmount on target host.

# cat nfsvolmigration/roles/volmount/tasks/volmount.yml
---
  - name: reading volume info from csv
read_csv:
  path: "{{ playbook_dir }}/roles/volmount/files/sources.csv"
register: sources
delegate_to: localhost
  - name: check the volume in fstab
shell: "grep {{ source.remote_path }} /etc/fstab|awk '{print $4}'"
loop: "{{ sources.list }}"
loop_control:
  loop_var: source
register: source
  - name: mounting the volume in the fstab file
mount:
  fstype: nfs
  opts: "{{ item.stdout_lines[0] }}"
  dump: "{{ fs_dump }}"
  passno: "{{ fsck_opt }}"
  src: "{{ item.source.remote_path }}"
  path: "{{ item.source.mnt_path }}"
  state: mounted
when: item.stdout != ""
loop: "{{ source.results }}"
...
# cat nfsvolmigration/roles/volunmount/tasks/volunmount.yml
---
  - name: "reading volume info from csv file"
read_csv:
  path: "{{ playbook_dir }}/roles/volunmount/files/sources.csv"
register: sources
delegate_to: localhost
  - name: check if the volumes are mounted
shell: mount -t nfs | grep "{{ source.remote_path }}" | awk '{print $1}'
args:
  warn: false
loop: "{{ sources.list }}"
loop_control:
  loop_var: source
register: mounts
  - name: unmount nfs_shares
shell: /bin/umount -lf {{ item.source.mnt_path }}
when: item.stdout != ""
loop: "{{ mounts.results }}"
  - name: unmount nfs_shares if there any
mount:
  path: "{{ item.source.mnt_path }}"
  src: "{{ item.source.remote_path }}"
  state: unmounted
when: item.stdout != ""
loop: "{{ mounts.results }}"
...

Regards,
Suresh


On Wed, Apr 8, 2020 at 8:48 PM Pandu jh  wrote:

>  format your csv data file to inventory file as below. Then the variables
> can be used to the respective hosts directly.
> Hope this helps.
>
> Use sed and awk for formatting the inventory file from CSV file
>
> sed 's/,/ /g' mounts
> awk -F' ' -vOFS=' ' '{ $2 = "remote_path=" $2  }2' mounts
> awk -F' ' -vOFS=' ' '{ $3 = "mnt_path=" $3  }3' /tmp/mounts
>
>
>  # cat mounts
> host1 remote_path=nfsvol1 mnt_path=mount1
> host1 remote_path=nfsvol2 mnt_path=mount2
> host1 remote_path=nfsvol3 mnt_path=mount3
> host2 remote_path=nfsvol1 mnt_path=mount1
> host2 remote_path=nfsvol2 mnt_path=mount2
> host2 remote_path=nfsvol3 mnt_path=mount3
>
>
>
> On Wednesday, 8 April 2020 20:07:27 UTC+5:30, Suresh Karpurapu wrote:
>>
>> thanks Vladimir, your suggestion helped a lot. Still, i am facing one
>> challenge while using add_host. I have the csv file with the same server
>> has different volumes. When i run the playbook, it is displaying last entry
>> associated to the same but i would like to display all the entries. Can you
>> suggest on the same?
>>
>> # cat mounts.csv
>> host,remote_path,mnt_path
>> host1,nfsvol1,mount1
>> host1,nfsvol2,mount2
>> host1,nfsvol3,mount3
>> host2,nfsvol1,mount1
>> host2,nfsvol2,mount2
>> host2,nfsvol3,mount3
>>
>> # cat mounts.yml
>> ---
>> - name: mount the nfsshare in client side
>>   hosts: localhost
>>   gather_facts: false
>>   become: yes
>>   tasks:
>> - name: reading volume info from csv
>>   read_csv:
>> path: "{{ playbook_dir }}/mounts.csv"
>>   register: sources
>>   delegate_to: localhost
>> - name: Grouping host and volume information
>>   add_host:
>> name: "{{ item.host }}"
>> groups: nfsgroup
>> var1: "{{ item.remote_path }}"
>> var2: "{{ item.mnt_path }}"
>>   loop: "{{ sources.list }}"
>> - name: list volume details
>>   hosts: nfsgroup
>>   become: yes
>>   gather_facts: false
>>   tasks:
>> - debug:
>> msg:
>>   - "{{ inventory_hostname }}"
>>   - "{{ var1 }}"
>>   - "{{ var2 }}"
>> ...
>>
>> Output:
>> ==
>> PLAY [list volume details]
>> 

Re: [ansible-project] Need to pass variable and hostname to a playbook from CSV file.

2020-04-08 Thread Suresh Karpurapu
awesome, it really helped for my requirement. thank you so much Vladimir,

Regards,
Suresh

On Wed, Apr 8, 2020 at 9:53 PM Vladimir Botka  wrote:

> On Wed, 8 Apr 2020 19:55:04 +0530
> Suresh Karpurapu  wrote:
>
> > ... i would like to display all the entries.
> >
> > # cat mounts.csv
> > host,remote_path,mnt_path
> > host1,nfsvol1,mount1
> > host1,nfsvol2,mount2
> > host1,nfsvol3,mount3
> > host2,nfsvol1,mount1
> > host2,nfsvol2,mount2
> > host2,nfsvol3,mount3
> >
> > # cat mounts.yml
> > ---
> > - name: mount the nfsshare in client side
> >   hosts: localhost
> >   gather_facts: false
> >   become: yes
> >   tasks:
> > - name: reading volume info from csv
> >   read_csv:
> > path: "{{ playbook_dir }}/mounts.csv"
> >   register: sources
> >   delegate_to: localhost
> > - name: Grouping host and volume information
> >   add_host:
> > name: "{{ item.host }}"
> > groups: nfsgroup
> > var1: "{{ item.remote_path }}"
> > var2: "{{ item.mnt_path }}"
> >   loop: "{{ sources.list }}"
>
> Use "groupby" filter. For example
> https://jinja.palletsprojects.com/en/master/templates/#groupby
>
>   add_host:
> name: "{{ item.0 }}"
> groups: nfsgroup
> var1: "{{ item.1|json_query('[].remote_path') }}"
> var2: "{{ item.1|json_query('[].mnt_path') }}"
>   loop: "{{ sources.list|groupby('host') }}"
>
> > - name: list volume details
> >   hosts: nfsgroup
> >   become: yes
> >   gather_facts: false
> >   tasks:
> > - debug:
> > msg:
> >   - "{{ inventory_hostname }}"
> >   - "{{ var1 }}"
> >   - "{{ var2 }}"
>
> You should receive lists of "remote_path" and "mnt_path" in var1 and var2
> respectively.
>
> ok: [host1] => {
> "msg": [
> "host1",
> [
> "nfsvol1",
> "nfsvol2",
> "nfsvol3"
> ],
> [
> "mount1",
> "mount2",
> "mount3"
> ]
> ]
> }
> ok: [host2] => {
> "msg": [
> "host2",
> [
> "nfsvol1",
> "nfsvol2",
> "nfsvol3"
> ],
> [
> "mount1",
> "mount2",
> "mount3"
> ]
> ]
> }
>
> HTH,
>
> -vlado
>

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAHedzh%2BvLkXFdEqyikyXnGSYWSTqopd6rgfhU0r%2BPu16biQsAg%40mail.gmail.com.


Re: [ansible-project] Need to pass variable and hostname to a playbook from CSV file.

2020-04-09 Thread Suresh Karpurapu
Hi Vladimir,

I have one more last question on the same request as we have different
python interpreter on some hosts. Is it possible to read the python
interpreter variable from CSV file while connecting the particular?
Prior to your solution, i use to use inventory to pass the variable to the
host as below. Now, i want to omit inventory file as i would like to use
your solution completely for host inventory. Can you please suggest same to
overcome this problem other than declaring variables host_vars?

host4 ansible_python_interpreter=/usr/bin/python2.6

Regards,
Suresh


On Wed, Apr 8, 2020 at 11:15 PM Suresh Karpurapu 
wrote:

> awesome, it really helped for my requirement. thank you so much Vladimir,
>
> Regards,
> Suresh
>
> On Wed, Apr 8, 2020 at 9:53 PM Vladimir Botka  wrote:
>
>> On Wed, 8 Apr 2020 19:55:04 +0530
>> Suresh Karpurapu  wrote:
>>
>> > ... i would like to display all the entries.
>> >
>> > # cat mounts.csv
>> > host,remote_path,mnt_path
>> > host1,nfsvol1,mount1
>> > host1,nfsvol2,mount2
>> > host1,nfsvol3,mount3
>> > host2,nfsvol1,mount1
>> > host2,nfsvol2,mount2
>> > host2,nfsvol3,mount3
>> >
>> > # cat mounts.yml
>> > ---
>> > - name: mount the nfsshare in client side
>> >   hosts: localhost
>> >   gather_facts: false
>> >   become: yes
>> >   tasks:
>> > - name: reading volume info from csv
>> >   read_csv:
>> > path: "{{ playbook_dir }}/mounts.csv"
>> >   register: sources
>> >   delegate_to: localhost
>> > - name: Grouping host and volume information
>> >   add_host:
>> > name: "{{ item.host }}"
>> > groups: nfsgroup
>> > var1: "{{ item.remote_path }}"
>> > var2: "{{ item.mnt_path }}"
>> >   loop: "{{ sources.list }}"
>>
>> Use "groupby" filter. For example
>> https://jinja.palletsprojects.com/en/master/templates/#groupby
>>
>>   add_host:
>> name: "{{ item.0 }}"
>> groups: nfsgroup
>> var1: "{{ item.1|json_query('[].remote_path') }}"
>> var2: "{{ item.1|json_query('[].mnt_path') }}"
>>   loop: "{{ sources.list|groupby('host') }}"
>>
>> > - name: list volume details
>> >   hosts: nfsgroup
>> >   become: yes
>> >   gather_facts: false
>> >   tasks:
>> > - debug:
>> > msg:
>> >   - "{{ inventory_hostname }}"
>> >   - "{{ var1 }}"
>> >   - "{{ var2 }}"
>>
>> You should receive lists of "remote_path" and "mnt_path" in var1 and var2
>> respectively.
>>
>> ok: [host1] => {
>> "msg": [
>> "host1",
>> [
>> "nfsvol1",
>> "nfsvol2",
>> "nfsvol3"
>> ],
>> [
>> "mount1",
>> "mount2",
>> "mount3"
>> ]
>> ]
>> }
>> ok: [host2] => {
>> "msg": [
>> "host2",
>> [
>> "nfsvol1",
>> "nfsvol2",
>> "nfsvol3"
>> ],
>> [
>> "mount1",
>> "mount2",
>> "mount3"
>> ]
>> ]
>> }
>>
>> HTH,
>>
>> -vlado
>>
>

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAHedzh%2BLUoy9VZxG61AuLVa9qQV0Vkyp_Zr48iAHgwGgg7ZFSg%40mail.gmail.com.


Re: [ansible-project] Need to pass variable and hostname to a playbook from CSV file.

2020-04-14 Thread Suresh Karpurapu
**
task path: /suresh/suresh_playbooks/mounts.yml:11
creating host via 'add_host': hostname=host1
changed: [localhost] => (item=[u'host1', [{u'python':
u'/usr/bin/python2.6', u'host': u'host1', u'mnt_path': u'/myasup/stg/data',
u'remote_path': u'nfsflr01:/volahcstg_www_masup_stg_data_01'}]]) => {
"add_host": {
"groups": [
"nfsgroup"
],
"host_name": "host1",
"host_vars": {
"ansible_python_interpreter": "/usr/bin/python2.6",
"var1": [
"nfsflr01:/volahcstg_www_masup_stg_data_01"
],
"var2": [
"/myasup/stg/data"
]
}
},
"ansible_loop_var": "item",
"changed": true,
"item": [
"host1",
[
{
"host": "host1",
"mnt_path": "/myasup/stg/data",
"python": "/usr/bin/python2.6",
"remote_path": "nfsflr01:/volahcstg_www_masup_stg_data_01"
}
]
]
}
creating host via 'add_host': hostname=host2
changed: [localhost] => (item=[u'host2', [{u'python':
u'/usr/bin/python2.6', u'host': u'host2', u'mnt_path': u'/myasup/stg/data',
u'remote_path': u'nfsflr01:/volahcstg_www_masup_stg_data_01'}]]) => {
"add_host": {
"groups": [
"nfsgroup"
],
"host_name": "host2",
"host_vars": {
"ansible_python_interpreter": "/usr/bin/python2.6",
"var1": [
"nfsflr01:/volahcstg_www_masup_stg_data_01"
],
"var2": [
"/myasup/stg/data"
]
}
},
"ansible_loop_var": "item",
"changed": true,
"item": [
"host2",
[
{
"host": "host2",
"mnt_path": "/myasup/stg/data",
"python": "/usr/bin/python2.6",
"remote_path": "nfsflr01:/volahcstg_www_masup_stg_data_01"
}
]
]
}
META: ran handlers
META: ran handlers

PLAY [list the volumes]
*
META: ran handlers

TASK [debug output]
*
task path: /suresh/suresh_playbooks/mounts.yml:24
ok: [host2] => {
"msg": [
"host2",
[
"nfsflr01:/volahcstg_www_masup_stg_data_01"
],
[
"/myasup/stg/data"
]
]
}
ok: [host1] => {
"msg": [
"host1",
[
"nfsflr01:/volahcstg_www_masup_stg_data_01"
],
[
"/myasup/stg/data"
]
]
}
META: ran handlers
META: ran handlers

PLAY [mounting the volume in the fstab file]

META: ran handlers

TASK [mounting the volume in the fstab file] ***

 (0, '', '')
fatal: [host2]: FAILED! => {
"changed": false,
"invocation": {
"module_args": {
"backup": false,
"boot": true,
"dump": "0",
"fstab": null,
"fstype": "nfs",
"opts":
"rw,bg,hard,rsize=65536,wsize=65536,vers=3,actimeo=0,nointr,suid,timeo=600,tcp",
"passno": "0",
"path": "['/myasup/stg/data']",
"src": "['nfsflr01:/volahcstg_www_masup_stg_data_01']",
"state": "mounted"
}
},
"msg": "Error mounting ['/myasup/stg/data']: mount: can't get address
for ['nfsflr01\n"

fatal: [host1]: FAILED! => {
"changed": false,
"invocation": {
"module_args": {
   

Re: [ansible-project] Need to pass variable and hostname to a playbook from CSV file.

2020-04-14 Thread Suresh Karpurapu
Would anyone please help me with this?

Regards,
Suresh

On Tue, 14 Apr 2020, 7:32 pm Suresh Karpurapu, 
wrote:

> Hi Vladimir, thanks for your help all the time.
>
> I tried using the logic, and it worked when apply the values to debug but
> the same data is passing to mount module with additional quotes and
> brackets which is causing the errors. Any suggestions please?
>
> Playbook and CSV details:
> =
>
> # cat mounts.csv
> host,remote_path,mnt_path,python
>
> host1,nfsflr01:/volahcstg_www_masup_stg_data_01,/myasup/stg/data,/usr/bin/python2.6
>
> host2,nfsflr01:/volahcstg_www_masup_stg_data_01,/myasup/stg/data,/usr/bin/python2.6
> # cat mounts.yml
> ---
> - name: mount the nfsshare in client side
>   hosts: localhost
>   gather_facts: false
>   become: yes
>   tasks:
> - name: reading volume info from csv
>   read_csv:
> path: "{{ playbook_dir }}/mounts.csv"
>   register: sources
> - name: Grouping host and volume information
>   add_host:
> name: "{{ item.0 }}"
> groups: nfsgroup
> var1: "{{ item.1|json_query('[].remote_path') }}"
> var2: "{{ item.1|json_query('[].mnt_path') }}"
> ansible_python_interpreter: "{{ item.1.0.python }}"
>   loop: "{{ sources.list|groupby('host') }}"
> - name: list the volumes
>   hosts: nfsgroup
>   become: yes
>   gather_facts: false
>   tasks:
> - name: debug output
>   debug:
> msg:
>   - "{{ inventory_hostname }}"
>   - "{{ var1 }}"
>   - "{{ var2 }}"
> - name: mounting the volume in the fstab file
>   hosts: nfsgroup
>   gather_facts: false
>   become: yes
>   tasks:
> - name: mounting the volume in the fstab file
>   mount:
> fstype: nfs
> opts:
> "rw,bg,hard,rsize=65536,wsize=65536,vers=3,actimeo=0,nointr,suid,timeo=600,tcp"
> dump: "0"
> passno: "0"
> src: "{{ var1 }}"
> path: "{{ var2 }}"
> state: mounted
>   delegate_to: "{{ inventory_hostname }}"
> ...
>
>
> 
> Playbook Verbose Output:
>
> 
>
> TASK [reading volume info from csv]
> *
> task path: /suresh/suresh_playbooks/mounts.yml:7
> <127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: root
> <127.0.0.1> EXEC /bin/sh -c 'echo ~root && sleep 0'
> <127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo
> /root/.ansible/tmp/ansible-tmp-1586795492.17-47707210108532 `" && echo
> ansible-tmp-1586795492.17-47707210108532="` echo
> /root/.ansible/tmp/ansible-tmp-1586795492.17-47707210108532 `" ) && sleep 0'
> Using module file
> /usr/lib/python2.7/site-packages/ansible/modules/files/read_csv.py
> <127.0.0.1> PUT /root/.ansible/tmp/ansible-local-26007iAx4cI/tmpxiOrdU TO
> /root/.ansible/tmp/ansible-tmp-1586795492.17-47707210108532/AnsiballZ_read_csv.py
> <127.0.0.1> EXEC /bin/sh -c 'chmod u+x
> /root/.ansible/tmp/ansible-tmp-1586795492.17-47707210108532/
> /root/.ansible/tmp/ansible-tmp-1586795492.17-47707210108532/AnsiballZ_read_csv.py
> && sleep 0'
> <127.0.0.1> EXEC /bin/sh -c '/usr/bin/python2
> /root/.ansible/tmp/ansible-tmp-1586795492.17-47707210108532/AnsiballZ_read_csv.py
> && sleep 0'
> <127.0.0.1> EXEC /bin/sh -c 'rm -f -r
> /root/.ansible/tmp/ansible-tmp-1586795492.17-47707210108532/ > /dev/null
> 2>&1 && sleep 0'
> ok: [localhost] => {
> "changed": false,
> "dict": {},
> "invocation": {
> "module_args": {
> "delimiter": null,
> "dialect": "excel",
> "fieldnames": null,
> "key": null,
> "path": "/suresh/suresh_playbooks/mounts.csv",
> "skipinitialspace": null,
> "strict": null,
> "unique": true
> }
> },
> "list": [
> {
> 

[ansible-project] Need help on json query call in ansible

2020-04-15 Thread Suresh Karpurapu
Hi Ansible Guru's

I am using the CSV file as below for nfs volume migration as we have 1000 
servers with 100 volumes. Hence, i would like to make to host inventory 
using add_host file instead using inventory file. However, the loop is 
failing when i use add_host module. Hence, i tried using json_query as 
suggested by vladimar, and it is working for debug output but same is not 
working for mount module as i would like to update /etc/fstab and mount the 
volumes using this playbook. would you suggest the logic?

Playbook and CSV details:
=

# cat mounts.csv
host,remote_path,mnt_path,python
host1,nfsflr01:/volahcstg_www_masup_stg_data_01,/myasup/stg/data,/usr/bin/python2.6
host2,nfsflr01:/volahcstg_www_masup_stg_data_01,/myasup/stg/data,/usr/bin/python2.6
# cat mounts.yml
---
- name: mount the nfsshare in client side
  hosts: localhost
  gather_facts: false
  become: yes
  tasks:
- name: reading volume info from csv
  read_csv:
path: "{{ playbook_dir }}/mounts.csv"
  register: sources
- name: Grouping host and volume information
  add_host:
name: "{{ item.0 }}"
groups: nfsgroup
var1: "{{ item.1|json_query('[].remote_path') }}"
var2: "{{ item.1|json_query('[].mnt_path') }}"
ansible_python_interpreter: "{{ item.1.0.python }}"
  loop: "{{ sources.list|groupby('host') }}"
- name: list the volumes
  hosts: nfsgroup
  become: yes
  gather_facts: false
  tasks:
- name: debug output
  debug:
msg:
  - "{{ inventory_hostname }}"
  - "{{ var1 }}"
  - "{{ var2 }}"
- name: mounting the volume in the fstab file
  hosts: nfsgroup
  gather_facts: false
  become: yes
  tasks:
- name: mounting the volume in the fstab file
  mount:
fstype: nfs
opts: 
"rw,bg,hard,rsize=65536,wsize=65536,vers=3,actimeo=0,nointr,suid,timeo=600,tcp"
dump: "0"
passno: "0"
src: "{{ var1 }}"
path: "{{ var2 }}"
state: mounted
  delegate_to: "{{ inventory_hostname }}"
...


Playbook Verbose Output:

"host": "host1",
"mnt_path": "/myasup/stg/data",
"python": "/usr/bin/python2.6",
"remote_path": "nfsflr01:/volahcstg_www_masup_stg_data_01"
},
{
"host": "host2",
"mnt_path": "/myasup/stg/data",
"python": "/usr/bin/python2.6",
"remote_path": "nfsflr01:/volahcstg_www_masup_stg_data_01"
}
]
}

TASK [Grouping host and volume information] 
*
task path: /suresh/suresh_playbooks/mounts.yml:11
creating host via 'add_host': hostname=host1
changed: [localhost] => (item=[u'host1', [{u'python': 
u'/usr/bin/python2.6', u'host': u'host1', u'mnt_path': u'/myasup/stg/data', 
u'remote_path': u'nfsflr01:/volahcstg_www_masup_stg_data_01'}]]) => {
"add_host": {
"groups": [
"nfsgroup"
],
"host_name": "host1",
"host_vars": {
"ansible_python_interpreter": "/usr/bin/python2.6",
"var1": [
"nfsflr01:/volahcstg_www_masup_stg_data_01"
],
"var2": [
"/myasup/stg/data"
]
}
},
"ansible_loop_var": "item",
"changed": true,
"item": [
"host1",
[
{
"host": "host1",
"mnt_path": "/myasup/stg/data",
"python": "/usr/bin/python2.6",
"remote_path": "nfsflr01:/volahcstg_www_masup_stg_data_01"
}
]
]
}
creating host via 'add_host': hostname=host2
changed: [localhost] => (item=[u'host2', [{u'python': 
u'/usr/bin/python2.6', u'host': u'host2', u'mnt_path': u'/myasup/stg/data', 
u'remote_path': u'nfsflr01:/volahcstg_www_masup_stg_data_01'}]]) => {
"add_host": {
"groups": [
"nfsgroup"
],
"host_name": "host2",
"host_vars": {
"ansible_python_interpreter": "/usr/bin/python2.6",
"var1": [
"nfsflr01:/volahcstg_www_masup_stg_data_01"
],
"var2": [
"/myasup/stg/data"
]
}
},
"ansible_loop_var": "item",
"changed": true,
"item": [
"host2",
[
{
"host": "host2",
"mnt_path": "/myasup/stg/data",
"python": "/usr/bin/python2.6",
"remote_path": "nfsflr01:/volahcstg_www_masup_stg_data_01"
}
]
]
}
META: ran handlers
META: ran handlers

PLAY [list

Re: [ansible-project] Need help on json query call in ansible

2020-04-16 Thread Suresh Karpurapu
laybooks/mounts.yml:25
ok: [host1] => {
"msg": [
"host1",
[
"nfsflr01:/volahcstg_www_masup_stg_data_01",
"nfsflr02:/volahcstg_www_masup_stg_app_01"
],
[
"/myasup/stg/data",
"/myasup/stg/app"
]
]
}
ok: [host2] => {
"msg": [
"host2",
[
"nfsflr01:/volahcstg_www_masup_stg_data_01",
"nfsflr02:/volahcstg_www_masup_stg_app_01"
],
[
"/myasup/stg/data",
"/myasup/stg/app"
]
]
}

TASK [mounting the volume in the fstab file]

changed: [host1 -> host1] => {
"changed": true,
"dump": "0",
"fstab": "/etc/fstab",
"fstype": "nfs",
"invocation": {
"module_args": {
"backup": false,
"boot": true,
"dump": "0",
"fstab": null,
"fstype": "nfs",
"opts":
"vers=3,rw,nosuid,bg,hard,rsize=32768,wsize=32768,nointr,tcp,timeo=600",
"passno": "0",
"path": "/myasup/stg/data",
"src": "nfsflr01:/volahcstg_www_masup_stg_data_01",
"state": "mounted"
}
},
"name": "/myasup/stg/data",
"opts":
"vers=3,rw,nosuid,bg,hard,rsize=32768,wsize=32768,nointr,tcp,timeo=600",
"passno": "0",
"src": "nfsflr01:/volahcstg_www_masup_stg_data_01"
}
 (0, '', '')
changed: [host2 -> host2] => {
"changed": true,
"dump": "0",
"fstab": "/etc/fstab",
"fstype": "nfs",
"invocation": {
"module_args": {
"backup": false,
"boot": true,
"dump": "0",
"fstab": null,
"fstype": "nfs",
"opts":
"vers=3,rw,nosuid,bg,hard,rsize=32768,wsize=32768,nointr,tcp,timeo=600",
"passno": "0",
"path": "/myasup/stg/data",
"src": "nfsflr01:/volahcstg_www_masup_stg_data_01",
"state": "mounted"
}
},
"name": "/myasup/stg/data",
"opts":
"vers=3,rw,nosuid,bg,hard,rsize=32768,wsize=32768,nointr,tcp,timeo=600",
"passno": "0",
"src": "nfsflr01:/volahcstg_www_masup_stg_data_01"
}
META: ran handlers
META: ran handlers

PLAY RECAP
**
localhost: ok=2changed=1unreachable=0failed=0
 skipped=0rescued=0ignored=0
host1: ok=2changed=1unreachable=0failed=0
 skipped=0rescued=0ignored=0
host2: ok=2changed=1unreachable=0failed=0
 skipped=0rescued=0ignored=0

On Thu, Apr 16, 2020 at 12:12 AM Stefan Hornburg (Racke) 
wrote:

> On 4/15/20 3:52 PM, Suresh Karpurapu wrote:
> > Hi Ansible Guru's
> >
> > I am using the CSV file as below for nfs volume migration as we have
> 1000 servers with 100 volumes. Hence, i would like
> > to make to host inventory using add_host file instead using inventory
> file. However, the loop is failing when i use
> > add_host module. Hence, i tried using json_query as suggested by
> vladimar, and it is working for debug output but same
> > is not working for mount module as i would like to update /etc/fstab and
> mount the volumes using this playbook. would
> > you suggest the logic?
> >
>
> var1 and var2 are lists, and not strings which are expected by the mount
> module.
>
> Try var1[0] and var2[0] instead.
>
> Regards
>  Racke
>
> > Playbook and CSV details:
> > =
> >
> > # cat mounts.csv
> > host,remote_path,mnt_path,python
> >
> host1,nfsflr01:/volahcstg_www_masup_stg_data_01,/myasup/stg/data,/usr/bin/python2.6
> >
> host2,nfsflr01:/volahcstg_www_masup_stg_data_01,/myasup/stg/data,/usr/bin/python2.6
> > # cat mounts.yml
> > ---
> > - name: mount the nfsshare in client side
&g

Re: [ansible-project] Need help on json query call in ansible

2020-04-17 Thread Suresh Karpurapu
Can anyone help me on this request or please suggest if there any
alternative solution for my requirement?

Regards,
Suresh

On Thu, 16 Apr 2020, 4:10 pm Suresh Karpurapu, 
wrote:

> Thanks Racke, it's working for if host has single volume but same is
> failing if the host has multiple volumes. Basically, loop is getting
> failed. Please suggest if there any alternative ways
>
> # cat mounts.csv
> host,remote_path,mnt_path,python
>
> host1,nfsflr01:/volahcstg_www_masup_stg_data_01,/myasup/stg/data,/usr/bin/python2.6
>
> host1,nfsflr02:/volahcstg_www_masup_stg_app_01,/myasup/stg/app,/usr/bin/python2.6
>
> host2,nfsflr01:/volahcstg_www_masup_stg_data_01,/myasup/stg/data,/usr/bin/python2.6
>
> host2,nfsflr02:/volahcstg_www_masup_stg_app_01,/myasup/stg/app,/usr/bin/python2.6
>
>
> Result:
>
> TASK [Grouping host and volume information]
> *
> task path: /suresh/suresh_playbooks/mounts.yml:12
> creating host via 'add_host': hostname=host1
> changed: [localhost] => (item=[u'host1', [{u'python':
> u'/usr/bin/python2.6', u'host': u'host1', u'mnt_path': u'/myasup/stg/data',
> u'remote_path': u'nfsflr01:/volahcstg_www_masup_stg_data_01'}, {u'python':
> u'/usr/bin/python2.6', u'host': u'host1', u'mnt_path': u'/myasup/stg/app',
> u'remote_path': u'nfsflr02:/volahcstg_www_masup_stg_app_01'}]]) => {
> "add_host": {
> "groups": [
> "nfsgroup"
> ],
> "host_name": "host1",
> "host_vars": {
> "ansible_python_interpreter": "/usr/bin/python2.6",
> "var1": [
> "nfsflr01:/volahcstg_www_masup_stg_data_01",
> "nfsflr02:/volahcstg_www_masup_stg_app_01"
> ],
> "var2": [
> "/myasup/stg/data",
> "/myasup/stg/app"
> ]
> }
> },
> "ansible_loop_var": "item",
> "changed": true,
> "item": [
> "host1",
> [
> {
> "host": "host1",
> "mnt_path": "/myasup/stg/data",
> "python": "/usr/bin/python2.6",
> "remote_path": "nfsflr01:/volahcstg_www_masup_stg_data_01"
> },
> {
> "host": "host1",
> "mnt_path": "/myasup/stg/app",
> "python": "/usr/bin/python2.6",
> "remote_path": "nfsflr02:/volahcstg_www_masup_stg_app_01"
> }
> ]
> ]
> }
> creating host via 'add_host': hostname=host2
> changed: [localhost] => (item=[u'host2', [{u'python':
> u'/usr/bin/python2.6', u'host': u'host2', u'mnt_path': u'/myasup/stg/data',
> u'remote_path': u'nfsflr01:/volahcstg_www_masup_stg_data_01'}, {u'python':
> u'/usr/bin/python2.6', u'host': u'host2', u'mnt_path': u'/myasup/stg/app',
> u'remote_path': u'nfsflr02:/volahcstg_www_masup_stg_app_01'}]]) => {
> "add_host": {
> "groups": [
> "nfsgroup"
> ],
> "host_name": "host2",
> "host_vars": {
> "ansible_python_interpreter": "/usr/bin/python2.6",
> "var1": [
> "nfsflr01:/volahcstg_www_masup_stg_data_01",
> "nfsflr02:/volahcstg_www_masup_stg_app_01"
> ],
> "var2": [
> "/myasup/stg/data",
> "/myasup/stg/app"
> ]
> }
> },
> "ansible_loop_var": "item",
> "changed": true,
> "item": [
> "host2",
> [
> {
> "host": "host2",
> "mnt_path": "/myasup/stg/data",
> "python": "/usr/bin/python2.6",
>  

[ansible-project] Loop functionality is failing when i use add_host module inventory in other playbook

2020-04-17 Thread Suresh Karpurapu
Hi Ansible team,

I am using the CSV file as below for nfs volume migration as we have 1000 
servers with 100 volumes. Hence, i would like to make to host inventory 
using add_host file instead using inventory file. However, the loop is 
failing when i use add_host module, if the server has more than one volume . 
would anyone please help me on this request as i would like to mount all 
the volumes on respective servers as per CSV input?

# cat mounts.csv
host,remote_path,mnt_path,python
host1,nfsflr01:/volahcstg_www_masup_stg_data_01,/myasup/stg/data,/usr/bin/python2.6
host1,nfsflr02:/volahcstg_www_masup_stg_app_01,/myasup/stg/app,/usr/bin/python2.6
host2,nfsflr01:/volahcstg_www_masup_stg_data_01,/myasup/stg/data,/usr/bin/python2.6
host2,nfsflr02:/volahcstg_www_masup_stg_app_01,/myasup/stg/app,/usr/bin/python2.6


# cat mounts.yml
---
- name: mount the nfsshare in client side
  hosts: localhost
  gather_facts: false
  become: yes
  tasks:
- name: reading volume info from csv
  read_csv:
path: "{{ playbook_dir }}/mounts.csv"
  register: sources
- name: Grouping host and volume information
  add_host:
name: "{{ item.0 }}"
groups: nfsgroup
var1: "{{ item.1|json_query('[].remote_path') }}"
var2: "{{ item.1|json_query('[].mnt_path') }}"
ansible_python_interpreter: "{{ item.1.0.python }}"
  loop: "{{ sources.list|groupby('host') }}"
- name: list the volumes
  hosts: nfsgroup
  become: yes
  gather_facts: false
  tasks:
- name: debug output
  debug:
msg:
  - "{{ inventory_hostname }}"
  - "{{ var1 }}"
  - "{{ var2 }}"
- name: mounting the volume in the fstab file
  hosts: nfsgroup
  gather_facts: false
  become: yes
  tasks:
- name: mounting the volume in the fstab file
  mount:
fstype: nfs
opts: 
"rw,bg,hard,rsize=65536,wsize=65536,vers=3,actimeo=0,nointr,suid,timeo=600,tcp"
dump: "0"
passno: "0"
src: "{{ var1[0] }}"
path: "{{ var2[0] }}"
state: mounted
  delegate_to: "{{ inventory_hostname }}"
...


Result:


TASK [Grouping host and volume information] 
*
task path: /suresh/suresh_playbooks/mounts.yml:12

creating host via 'add_host': hostname=host1
changed: [localhost] => (item=[u'host1', [{u'python': 
u'/usr/bin/python2.6', u'host': u'host1', u'mnt_path': u'/myasup/stg/data', 
u'remote_path': u'nfsflr01:/volahcstg_www_masup_stg_data_01'}, {u'python': 
u'/usr/bin/python2.6', u'host': u'host1', u'mnt_path': u'/myasup/stg/app', 
u'remote_path': u'nfsflr02:/volahcstg_www_masup_stg_app_01'}]]) => {

"add_host": {
"groups": [
"nfsgroup"
],
"host_name": "host1",
"host_vars": {
"ansible_python_interpreter": "/usr/bin/python2.6",
"var1": [
"nfsflr01:/volahcstg_www_masup_stg_data_01",
"nfsflr02:/volahcstg_www_masup_stg_app_01"
],
"var2": [
"/myasup/stg/data",
"/myasup/stg/app"
]

}
},
"ansible_loop_var": "item",
"changed": true,
"item": [
"host1",
[
{
"host": "host1",
"mnt_path": "/myasup/stg/data",
"python": "/usr/bin/python2.6",
"remote_path": "nfsflr01:/volahcstg_www_masup_stg_data_01"
},
{
"host": "host1",
"mnt_path": "/myasup/stg/app",
"python": "/usr/bin/python2.6",
"remote_path": "nfsflr02:/volahcstg_www_masup_stg_app_01"
}
]
]

}
creating host via 'add_host': hostname=host2
changed: [localhost] => (item=[u'host2', [{u'python': 
u'/usr/bin/python2.6', u'host': u'host2', u'mnt_path': u'/myasup/stg/data', 
u'remote_path': u'nfsflr01:/volahcstg_www_masup_stg_data_01'}, {u'python': 
u'/usr/bin/python2.6', u'host': u'host2', u'mnt_path': u'/myasup/stg/app', 
u'remote_path': u'nfsflr02:/volahcstg_www_masup_stg_app_01'}]]) => {

"add_host": {
"groups": [
"nfsgroup"
],
"host_name": "host2",
"host_vars": {
"ansible_python_interpreter": "/usr/bin/python2.6",
"var1": [
"nfsflr01:/volahcstg_www_masup_stg_data_01",
"nfsflr02:/volahcstg_www_masup_stg_app_01"
],
"var2": [
"/myasup/stg/data",
"/myasup/stg/app"
]

}
},
"ansible_loop_var": "item",
"changed": true,
"item": [
"host2",
[
{
"host": "host2",
"mnt_path": "/myasup/stg/data",
"python": "/usr/bin/python2.6",
"remote_path": "nfsflr01:/volahcstg_www_masup_stg_data_01"
},
{
   

[ansible-project] Re: Loop functionality is failing when i use add_host module inventory in other playbook

2020-04-20 Thread Suresh Karpurapu
I hope that it might be design of the add_host module. Is there any ways to 
disable the BYPASS_HOST_LOOP = True parameter in add_host module to enable 
the iteration or please suggest me if there any other ways to achieve my 
requirement

Regards,
Suresh

On Friday, April 17, 2020 at 7:36:37 PM UTC+5:30, Suresh Karpurapu wrote:
>
> Hi Ansible team,
>
> I am using the CSV file as below for nfs volume migration as we have 1000 
> servers with 100 volumes. Hence, i would like to make to host inventory 
> using add_host file instead using inventory file. However, the loop is 
> failing when i use add_host module, if the server has more than one 
> volume . would anyone please help me on this request as i would like to 
> mount all the volumes on respective servers as per CSV input?
>
> # cat mounts.csv
> host,remote_path,mnt_path,python
>
> host1,nfsflr01:/volahcstg_www_masup_stg_data_01,/myasup/stg/data,/usr/bin/python2.6
>
> host1,nfsflr02:/volahcstg_www_masup_stg_app_01,/myasup/stg/app,/usr/bin/python2.6
>
> host2,nfsflr01:/volahcstg_www_masup_stg_data_01,/myasup/stg/data,/usr/bin/python2.6
>
> host2,nfsflr02:/volahcstg_www_masup_stg_app_01,/myasup/stg/app,/usr/bin/python2.6
>
>
> # cat mounts.yml
> ---
> - name: mount the nfsshare in client side
>   hosts: localhost
>   gather_facts: false
>   become: yes
>   tasks:
> - name: reading volume info from csv
>   read_csv:
> path: "{{ playbook_dir }}/mounts.csv"
>   register: sources
> - name: Grouping host and volume information
>   add_host:
> name: "{{ item.0 }}"
> groups: nfsgroup
> var1: "{{ item.1|json_query('[].remote_path') }}"
> var2: "{{ item.1|json_query('[].mnt_path') }}"
> ansible_python_interpreter: "{{ item.1.0.python }}"
>   loop: "{{ sources.list|groupby('host') }}"
> - name: list the volumes
>   hosts: nfsgroup
>   become: yes
>   gather_facts: false
>   tasks:
> - name: debug output
>   debug:
> msg:
>   - "{{ inventory_hostname }}"
>   - "{{ var1 }}"
>   - "{{ var2 }}"
> - name: mounting the volume in the fstab file
>   hosts: nfsgroup
>   gather_facts: false
>   become: yes
>   tasks:
> - name: mounting the volume in the fstab file
>   mount:
> fstype: nfs
> opts: 
> "rw,bg,hard,rsize=65536,wsize=65536,vers=3,actimeo=0,nointr,suid,timeo=600,tcp"
> dump: "0"
> passno: "0"
> src: "{{ var1[0] }}"
> path: "{{ var2[0] }}"
> state: mounted
>   delegate_to: "{{ inventory_hostname }}"
> ...
>
>
> Result:
>
>
> TASK [Grouping host and volume information] 
> *
> task path: /suresh/suresh_playbooks/mounts.yml:12
>
> creating host via 'add_host': hostname=host1
> changed: [localhost] => (item=[u'host1', [{u'python': 
> u'/usr/bin/python2.6', u'host': u'host1', u'mnt_path': u'/myasup/stg/data', 
> u'remote_path': u'nfsflr01:/volahcstg_www_masup_stg_data_01'}, {u'python': 
> u'/usr/bin/python2.6', u'host': u'host1', u'mnt_path': u'/myasup/stg/app', 
> u'remote_path': u'nfsflr02:/volahcstg_www_masup_stg_app_01'}]]) => {
>
> "add_host": {
> "groups": [
> "nfsgroup"
> ],
> "host_name": "host1",
> "host_vars": {
> "ansible_python_interpreter": "/usr/bin/python2.6",
> "var1": [
> "nfsflr01:/volahcstg_www_masup_stg_data_01",
> "nfsflr02:/volahcstg_www_masup_stg_app_01"
> ],
> "var2": [
> "/myasup/stg/data",
> "/myasup/stg/app"
> ]
>
> }
> },
> "ansible_loop_var": "item",
> "changed": true,
> "item": [
> "host1",
> [
> {
> "host": "host1",
> "mnt_path": "/myasup/stg/data",
> "python": "/usr/bin/python2.6",
> "remote_path"

[ansible-project] How to exclude the task from role

2020-04-24 Thread Suresh Karpurapu
Hi Team,

I have the role which has 10 tasks. I would like to exclude 1 task out of 
10 as the task already executed in previous play.. Is there anyway to do

---
  - name: Required Checks for the migration
hosts: localhost
gather_facts: no
become: yes
tasks:
  - include_role:
  name: validation
  tasks_from: sshcheck
  - name: check ssh connectivity
hosts: sshgroup
gather_facts: no
become: yes
any_errors_fatal: false
tasks:
  - name: Checking SSH connectivity
wait_for:
  host: "{{ inventory_hostname }}"
  port: 22
  - name: stale and mount permissions check
hosts: localhost
gather_facts: no
become: yes
roles:
  - { name: validation, sshcheck: false }
...

Regards,
Suresh

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/dd1e851f-2a2a-4fd3-8d75-2b57a99eb7fe%40googlegroups.com.


Re: [ansible-project] Need to pass variable and hostname to a playbook from CSV file.

2020-04-27 Thread Suresh Karpurapu
Awesome Vladimir, i just tried the solution that you have suggested. It's
working as per my requirement. Once again thank you so much.

Regards,
Suresh

On Mon, Apr 27, 2020 at 1:16 PM Vladimir Botka  wrote:

> On Mon, 20 Apr 2020 21:58:34 +0530
> Suresh Karpurapu  wrote:
>
> > ... to achieve my goal as iteration[*have multiple nfs volume in
> > CSV for each host*] is failing with add_host module due to
> *BYPASS_HOST_LOOP
> > = True* in module destination. ...
> > [...]
> > - name: list the volumes
> >   hosts: nfsgroup
> >   become: yes
> >   gather_facts: false
> >   tasks:
> > - name: debug output
> >   debug:
> > msg:
> >   - "{{ inventory_hostname }}"
> >   - "{{ var1 }}"
> >   - "{{ var2 }}"
> > [...]
> > - name: mounting the volume in the fstab file
> >   hosts: nfsgroup
> >   gather_facts: false
> >   become: yes
> >   tasks:
> > - name: mounting the volume in the fstab file
> >   mount:
> > fstype: nfs
> > opts:
> >
> "rw,bg,hard,rsize=65536,wsize=65536,vers=3,actimeo=0,nointr,suid,timeo=600,tcp"
> > dump: "0"
> > passno: "0"
> > src: "{{ var1[0] }}"
> > path: "{{ var2[0] }}"
> > state: mounted
> >   delegate_to: "{{ inventory_hostname }}"
> > ...
>
> delegate_to: "{{ inventory_hostname }}" is redundant.
>
> Try "with_together"
>
> https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#with-together
>
> - name: mounting the volume in the fstab file
>   hosts: nfsgroup
>   gather_facts: false
>   become: yes
>   tasks:
> - name: mounting the volume in the fstab file
>   mount:
> fstype: nfs
> ...
> src: "{{ item.1 }}"
> path: "{{ item.2 }}"
> state: mounted
>   with_together:
> - "{{ var1 }}"
> - "{{ var2 }}"
>
>
> > *Result:*
> > TASK [list the volumes]
> > ok: [host1] => {
> > "msg": [
> > "host1",
> > [
> > "nfsflr01:/volahcstg_www_masup_stg_data_01",
> > "nfsflr02:/volahcstg_www_masup_stg_app_01"
> > ],
> > [
> > "/myasup/stg/data",
> > "/myasup/stg/app"
> > ]
> > ]
>

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAHedzhL46XVzu4aP5c540VY48v7j%2BkHL75AaH8GRXt3bCD9mHA%40mail.gmail.com.


[ansible-project] Jinja template error while using with copy module

2020-04-28 Thread Suresh Karpurapu
Hi All,

I would like to use jinja templates for conditions instead when. Is there 
anyways to fix the below errors with copy module?

- name: makeing file backup as vfstab.bkp in /tmp
  copy: |-
{% if ansible_os_family == "Solaris" %}
src=/etc/vfstab dest=/tmp/vfstab.bkp remote_src=yes
{%- else %}
src=/etc/fstab dest=/tmp/fstab.bkp remote_src=yes
{%- endif %}
  when:
- item.changed == true
- item.stdout != ""
  with_items: "{{ mounts.results }}"

Error Details:
fatal: [ngzerpandb01-dev]: FAILED! => {
"msg": "template error while templating string: Encountered unknown tag 
'else'.. String: {%- else %}\n{%- endif %}"
}
fatal: [vmwopsapp04-stg]: FAILED! => {
"msg": "template error while templating string: Encountered unknown tag 
'else'.. String: {%- else %}\n{%- endif %}"
}
fatal: [vmwopsapp12-prd]: FAILED! => {
"msg": "template error while templating string: Encountered unknown tag 
'else'.. String: {%- else %}\n{%- endif %}"
}


Regards,
Suresh

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/d39bb1ee-3ae6-4a79-8004-ae9c8e8c861f%40googlegroups.com.


[ansible-project] Re: Jinja template error while using with copy module

2020-04-28 Thread Suresh Karpurapu
sorry guys, it worked for me in this way.

- name: makeing file backup as fstab.bkp in /tmp
copy: 
  src: |-
{% if ansible_os_family == "Solaris" %}
/etc/vfstab
{%- else %}
/etc/fstab
{%- endif %}
  dest: /tmp/fstab.bkp 
  remote_src: yes
when:
  - item.changed == true
  - item.stdout != ""
with_items: "{{ mounts.results }}"

On Wednesday, April 29, 2020 at 1:15:14 AM UTC+5:30, Suresh Karpurapu wrote:
>
> Hi All,
>
> I would like to use jinja templates for conditions instead when. Is there 
> anyways to fix the below errors with copy module?
>
> - name: makeing file backup as vfstab.bkp in /tmp
>   copy: |-
> {% if ansible_os_family == "Solaris" %}
> src=/etc/vfstab dest=/tmp/vfstab.bkp remote_src=yes
> {%- else %}
> src=/etc/fstab dest=/tmp/fstab.bkp remote_src=yes
> {%- endif %}
>   when:
> - item.changed == true
> - item.stdout != ""
>   with_items: "{{ mounts.results }}"
>
> Error Details:
> fatal: [ngzerpandb01-dev]: FAILED! => {
> "msg": "template error while templating string: Encountered unknown 
> tag 'else'.. String: {%- else %}\n{%- endif %}"
> }
> fatal: [vmwopsapp04-stg]: FAILED! => {
> "msg": "template error while templating string: Encountered unknown 
> tag 'else'.. String: {%- else %}\n{%- endif %}"
> }
> fatal: [vmwopsapp12-prd]: FAILED! => {
> "msg": "template error while templating string: Encountered unknown 
> tag 'else'.. String: {%- else %}\n{%- endif %}"
> }
>
>
> Regards,
> Suresh
>

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/737ed776-6568-4e31-9994-01edc338eac0%40googlegroups.com.


[ansible-project] Need help on formating stdout_lines output

2020-04-29 Thread Suresh Karpurapu
Hi All,

Would anyone please help me formatting mountchk.result. I am able to get 
the required output with 
item.stdout_lines[0],item.stdout_lines[1],item.stdout_lines[2] but host3 
has 6 entries. So, please help me in filtering 


mountchk.results:
=
host1:
 "stdout": "nfsflr01:/app01\n/app\n/etc/fstab",
 "stdout_lines": [
"nfsflr01:/app01",
"/app",
"/etc/fstab"

host2: 
"stdout": "nfsflr02:/app02\n/app02\n/etc/fstab",
"stdout_lines": [
"nfsflr02:/app02",
"/app02",
"/etc/fstab"
host3:
 "stdout": 
"nfsflr03:/app03\n/app03\n/etc/fstab\nnfsflr03:/app03\n/app03\n/etc/rc.willows",
 "stdout_lines": [
 "nfsflr03:/app03",
 "/app03",
 "/etc/fstab",
 "nfsflr03:/app03",
 "/app03",
 "/etc/rc.willows"

Required filtering:
==
host1,nfsflr01:/app01,/app,/etc/fstab
host2,nfsflr02:/app02,/app02,/etc/fstab
host3,nfsflr03:/app03,/app03,/etc/fstab
host3,nfsflr03:/app03,/app03,/etc/rc.willows

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/e931b86e-2c7d-4293-a280-6690818bc94e%40googlegroups.com.


Re: [ansible-project] Need help on formating stdout_lines output

2020-04-30 Thread Suresh Karpurapu
Can anyone help me on this request?

Regards,
Suresh

On Thu, Apr 30, 2020 at 4:14 AM Suresh Karpurapu 
wrote:

> Hi All,
>
> Would anyone please help me formatting mountchk.result. I am able to get
> the required output with
> item.stdout_lines[0],item.stdout_lines[1],item.stdout_lines[2] but host3
> has 6 entries. So, please help me in filtering
>
>
> mountchk.results:
> =
> host1:
>  "stdout": "nfsflr01:/app01\n/app\n/etc/fstab",
>  "stdout_lines": [
> "nfsflr01:/app01",
> "/app",
> "/etc/fstab"
>
> host2:
> "stdout": "nfsflr02:/app02\n/app02\n/etc/fstab",
> "stdout_lines": [
> "nfsflr02:/app02",
> "/app02",
> "/etc/fstab"
> host3:
>  "stdout":
> "nfsflr03:/app03\n/app03\n/etc/fstab\nnfsflr03:/app03\n/app03\n/etc/rc.willows",
>  "stdout_lines": [
>  "nfsflr03:/app03",
>  "/app03",
>  "/etc/fstab",
>  "nfsflr03:/app03",
>  "/app03",
>  "/etc/rc.willows"
>
> Required filtering:
> ==
> host1,nfsflr01:/app01,/app,/etc/fstab
> host2,nfsflr02:/app02,/app02,/etc/fstab
> host3,nfsflr03:/app03,/app03,/etc/fstab
> host3,nfsflr03:/app03,/app03,/etc/rc.willows
>
> --
> 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-project+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/e931b86e-2c7d-4293-a280-6690818bc94e%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/e931b86e-2c7d-4293-a280-6690818bc94e%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAHedzhJ4VBMQhYKBFhMcpu73Kebvr8zO_a884xtBkdWFMdnTnw%40mail.gmail.com.


Re: [ansible-project] Need help on formating stdout_lines output

2020-05-04 Thread Suresh Karpurapu
Hi Sai Stian,

This syntax would be helpful but i may be using in wrong way as i am
getting below errors. Please help on fixing the issue.

  - name: debug with with sequence
debug:
  msg:"{{ volchk[item.stdout | int] }},{{ volchk[item.stdout | int + 1]
}},{{ volchk[item.stdout | int + 2] }}"
with_sequence: start=0 end={{ (volchk | length)-1 }} stride=3
*error:*
FAILED! => {"msg": "The task includes an option with an undefined variable.
The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeBytes object' has
no attribute 'stdout'\n\nThe error appears to be in
'/suresh/suresh_playbooks/nfsvolmigration/roles/mountcheck/tasks/volcheck.yml':
line 44, column 5, but may\nbe elsewhere in the file depending on the exact
syntax problem.\n\nThe offending line appears to be:\n\nignore_errors:
true\n  - name: debug with with sequence\n^ here\n"}

Debug "volchk.result" output:
===
host1:
 "stdout": "nfsflr01:/app01\n/app\n/etc/fstab",
 "stdout_lines": [
"nfsflr01:/app01",
"/app",
"/etc/fstab"

host2:
"stdout": "nfsflr02:/app02\n/app02\n/etc/fstab",
"stdout_lines": [
"nfsflr02:/app02",
"/app02",
"/etc/fstab"
host3:
 "stdout":
"nfsflr03:/app03\n/app03\n/etc/fstab\nnfsflr03:/app03\n/app03\n/etc/rc.willows",
 "stdout_lines": [
 "nfsflr03:/app03",
 "/app03",
 "/etc/fstab",
 "nfsflr03:/app03",
     "/app03",
 "/etc/rc.willows"

Regards,
Suresh



FAILED! => {"msg": "'dict object' has no attribute 'stdout'"}

On Sat, May 2, 2020 at 2:32 PM Kai Stian Olstad <
ansible-project+l...@olstad.com> wrote:

> On Wed, Apr 29, 2020 at 03:44:14PM -0700, Suresh Karpurapu wrote:
> > Would anyone please help me formatting mountchk.result. I am able to get
> > the required output with
> > item.stdout_lines[0],item.stdout_lines[1],item.stdout_lines[2] but host3
> > has 6 entries. So, please help me in filtering
>
> You can use with_sequence or range in a for loop.
>
>   vars:
> myvar:
>   - one
>   - two
>   - three
>   - four
>   - five
>   - six
>   tasks:
> - debug: msg="{{ myvar[item | int] }},{{ myvar[item | int + 1] }},{{
> myvar[item | int + 2] }}"
>   with_sequence: start=0 end={{ (myvar | length) - 1 }} stride=3
>
> - debug:
> msg: |
>   {% for i in range(0, myvar | length, 3) %}
>   {{ myvar[i] }},{{ myvar[i + 1] }},{{ myvar[i + 2] }}
>   {% endfor %}
>
> --
> Kai Stian Olstad
>
> --
> 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-project+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/20200502090224.zygkso3iqpjok2bk%40olstad.com
> .
>

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAHedzhKyABrV%2BS5N9w8vHF6fZtfb-WH7%2B7%3D41HuwieNqS41jVQ%40mail.gmail.com.


Re: [ansible-project] Need help on formating stdout_lines output

2020-05-04 Thread Suresh Karpurapu
Here is my playbook, i would like to display all lines in stdout_lines in
below format without miss. I am able to display only first 3 lines but
host03 has more than 3 lines even it may be more lines for other hosts.
Would anyone please help me?

Required output fle:
==
host1,nfsflr01:/app01,/app,na,/etc/fstab
host2,nfsflr02:/app02,/app02,na,/etc/fstab
host3,nfsflr03:/app03,/app03,na,/etc/fstab
host3,nfsflr03:/app03,/app03,na,/etc/rc.willows
host4,nfsflr04:/app04,na,na,na


---
  - name: nullify the ansible log file
local_action: copy content="" dest="{{ playbook_dir
}}/logs/volmigration.log"
ignore_errors: true
run_once: True
  - name: nullify the file content
local_action: copy content="" dest="{{ playbook_dir
}}/logs/volcheck.txt"
ignore_errors: true
run_once: True
  - name: Making the txt header information to volcheck.txt file
local_action:
  module: lineinfile
  path: "{{ playbook_dir }}/logs/volcheck.txt"
  line: "host,python,old_path,mnt_path,new_path,map_path,csv_path"
  state: present
  create: yes
run_once: True
  - name: Fetch the volume mounted status
shell: |-
  {% if ansible_os_family == "Solaris" %}
  grep -ws "{{ item.0 }}" /etc/vfstab|awk '{print $1}'
  grep -ws "{{ item.0 }}" /etc/vfstab|awk '{print $3}'
  grep -wls "{{ item.0 }}" /etc/vfstab
  grep -wls "{{ item.0 }}" /etc/auto*|head -1|xargs grep -w "{{ item.0
}}"|awk '{print $2}'
  grep -wls "{{ item.0 }}" /etc/auto*|head -1|xargs grep -w "{{ item.0
}}"|awk '{print $1}'
  grep -wls "{{ item.0 }}" /etc/auto*|head -1
  {%- else %}
  grep -rws "{{ item.0 }}" /etc/fstab|awk '{print $1}'
  grep -rws "{{ item.0 }}" /etc/fstab|awk '{print $2}'
  grep -wls "{{ item.0 }}" /etc/fstab
  grep -rls "{{ item.0 }}" /etc/auto*|head -1|xargs grep -w "{{ item.0
}}"|awk '{print $3}'
  grep -rls "{{ item.0 }}" /etc/auto*|head -1|xargs grep -w "{{ item.0
}}"|awk '{print $1}'
  grep -rwls "{{ item.0 }}" /etc/auto*|head -1
  grep -rwls "{{ item.0 }}" /etc/rc*|head -1|xargs grep -w "{{ item.0
}}"|awk '{print $6}'
  grep -rwls "{{ item.0 }}" /etc/rc*|head -1|xargs grep -w "{{ item.0
}}"|awk '{print $7}'
  grep -rwls "{{ item.0 }}" /etc/rc*|head -1
  {%- endif %}
with_together:
  - "{{ var1 }}"
register: volchk
args:
  warn: false
ignore_errors: true
  - name: Making the final volcheck.txt file
local_action:
  module: lineinfile
  path: "{{ playbook_dir }}/logs/volcheck.txt"
  line: |-
{% if item.stdout|length>0 %}
{{ inventory_hostname }},{{ ansible_python_interpreter }},{{
item.stdout_lines[0] }},{{ item.stdout_lines[1] }},NA,{{
item.stdout_lines[2] }},{{ item.item.0 }},
{%- else %}
{{ inventory_hostname }},{{ ansible_python_interpreter
}},NA,NA,NA,NA,{{ item.item.0 }},
{%- endif %}
  insertafter: EOF
  state: present
  create: yes
with_items: "{{ volchk.results }}"
  - name: mark the entry of /etc/fstab and /etc/vfstab as fstab
replace:
  path: "{{ playbook_dir }}/logs/volcheck.txt"
  regexp: "{{ item.stdout_lines[2] }}"
  replace: "fstab"
when: item.stdout|length>0 and (item.stdout_lines[2] |
regex_search("/etc/fstab") or item.stdout_lines[2] |
regex_search("/etc/vfstab"))
with_items: "{{ volchk.results }}"
delegate_to: localhost
  - debug:
  msg: "Final volume check file is located at {{ playbook_dir
}}/logs/volcheck.txt"
delegate_to: localhost
run_once: true
  - name: Send the final report email
local_action:
  module: mail
  host: host10.google.com
  port: 25
  to: sur...@gmail.com
  subject: Ansible volume pre-check report
  attach: "{{ playbook_dir }}/logs/volcheck.txt"
run_once: true
...

Debug:
Debug "volchk.result" output:
===
host1:
 "stdout": "nfsflr01:/app01\n/app\n/etc/fstab",
 "stdout_lines": [
        "nfsflr01:/app01",
"/app",
"/etc/fstab"

host2:
"stdout": "nfsflr02:/app02\n/app02\n/etc/fstab",
"stdout_lines": [
"nfsflr02:/app02",
"/app02",
"/etc/fstab"
host3:
 "stdout":
"nfsflr03:/app03\n/app03\n/etc/fstab\nnfsflr03:/app03\n/app03\n/etc/rc.willows",
 "stdout_lines": [
 "nfsflr

Re: [ansible-project] Need help on formating stdout_lines output

2020-05-05 Thread Suresh Karpurapu
would anyone please help me on this request?

Regards,
Suresh

On Tue, May 5, 2020 at 2:45 AM Suresh Karpurapu 
wrote:

> Here is my playbook, i would like to display all lines in stdout_lines in
> below format without miss. I am able to display only first 3 lines but
> host03 has more than 3 lines even it may be more lines for other hosts.
> Would anyone please help me?
>
> Required output fle:
> ==
> host1,nfsflr01:/app01,/app,na,/etc/fstab
> host2,nfsflr02:/app02,/app02,na,/etc/fstab
> host3,nfsflr03:/app03,/app03,na,/etc/fstab
> host3,nfsflr03:/app03,/app03,na,/etc/rc.willows
> host4,nfsflr04:/app04,na,na,na
>
>
> ---
>   - name: nullify the ansible log file
> local_action: copy content="" dest="{{ playbook_dir
> }}/logs/volmigration.log"
> ignore_errors: true
> run_once: True
>   - name: nullify the file content
> local_action: copy content="" dest="{{ playbook_dir
> }}/logs/volcheck.txt"
> ignore_errors: true
> run_once: True
>   - name: Making the txt header information to volcheck.txt file
> local_action:
>   module: lineinfile
>   path: "{{ playbook_dir }}/logs/volcheck.txt"
>   line: "host,python,old_path,mnt_path,new_path,map_path,csv_path"
>   state: present
>   create: yes
> run_once: True
>   - name: Fetch the volume mounted status
> shell: |-
>   {% if ansible_os_family == "Solaris" %}
>   grep -ws "{{ item.0 }}" /etc/vfstab|awk '{print $1}'
>   grep -ws "{{ item.0 }}" /etc/vfstab|awk '{print $3}'
>   grep -wls "{{ item.0 }}" /etc/vfstab
>   grep -wls "{{ item.0 }}" /etc/auto*|head -1|xargs grep -w "{{ item.0
> }}"|awk '{print $2}'
>   grep -wls "{{ item.0 }}" /etc/auto*|head -1|xargs grep -w "{{ item.0
> }}"|awk '{print $1}'
>   grep -wls "{{ item.0 }}" /etc/auto*|head -1
>   {%- else %}
>   grep -rws "{{ item.0 }}" /etc/fstab|awk '{print $1}'
>   grep -rws "{{ item.0 }}" /etc/fstab|awk '{print $2}'
>   grep -wls "{{ item.0 }}" /etc/fstab
>   grep -rls "{{ item.0 }}" /etc/auto*|head -1|xargs grep -w "{{ item.0
> }}"|awk '{print $3}'
>   grep -rls "{{ item.0 }}" /etc/auto*|head -1|xargs grep -w "{{ item.0
> }}"|awk '{print $1}'
>   grep -rwls "{{ item.0 }}" /etc/auto*|head -1
>   grep -rwls "{{ item.0 }}" /etc/rc*|head -1|xargs grep -w "{{ item.0
> }}"|awk '{print $6}'
>   grep -rwls "{{ item.0 }}" /etc/rc*|head -1|xargs grep -w "{{ item.0
> }}"|awk '{print $7}'
>   grep -rwls "{{ item.0 }}" /etc/rc*|head -1
>   {%- endif %}
> with_together:
>   - "{{ var1 }}"
> register: volchk
> args:
>   warn: false
> ignore_errors: true
>   - name: Making the final volcheck.txt file
> local_action:
>   module: lineinfile
>   path: "{{ playbook_dir }}/logs/volcheck.txt"
>   line: |-
> {% if item.stdout|length>0 %}
> {{ inventory_hostname }},{{ ansible_python_interpreter }},{{
> item.stdout_lines[0] }},{{ item.stdout_lines[1] }},NA,{{
> item.stdout_lines[2] }},{{ item.item.0 }},
> {%- else %}
> {{ inventory_hostname }},{{ ansible_python_interpreter
> }},NA,NA,NA,NA,{{ item.item.0 }},
> {%- endif %}
>   insertafter: EOF
>   state: present
>   create: yes
> with_items: "{{ volchk.results }}"
>   - name: mark the entry of /etc/fstab and /etc/vfstab as fstab
> replace:
>   path: "{{ playbook_dir }}/logs/volcheck.txt"
>   regexp: "{{ item.stdout_lines[2] }}"
>   replace: "fstab"
> when: item.stdout|length>0 and (item.stdout_lines[2] |
> regex_search("/etc/fstab") or item.stdout_lines[2] |
> regex_search("/etc/vfstab"))
> with_items: "{{ volchk.results }}"
> delegate_to: localhost
>   - debug:
>   msg: "Final volume check file is located at {{ playbook_dir
> }}/logs/volcheck.txt"
> delegate_to: localhost
> run_once: true
>   - name: Send the final report email
> local_action:
>   module: mail
>   host: host10.google.com
>   port: 25
>   to: sur...@gmail.com
>   subject: Ansible volume pre-check report
>   attach: "{{ playbook_dir }}/logs/volcheck.txt"
> run_once: true
> ...
>
> Debug:
> Debug 

Re: [ansible-project] Need help on formating stdout_lines output

2020-05-06 Thread Suresh Karpurapu
thanks Neha, Kai

I could fix the issue by using range with stdout.split(). Even, it is
working with stdout_lines as well

Once again thanks to all for helping me on this request.

Regards,
Suresh

On Wed, May 6, 2020 at 5:30 PM Neha Pithadiya 
wrote:

> Can you paste the content of "volchk" list .. Also try using
> item.stdout_lines instead of item.stdout in your debug task.
>
> On Wednesday, May 6, 2020 at 12:26:11 AM UTC+5:30, Suresh Karpurapu wrote:
>>
>> would anyone please help me on this request?
>>
>> Regards,
>> Suresh
>>
>> On Tue, May 5, 2020 at 2:45 AM Suresh Karpurapu 
>> wrote:
>>
>>> Here is my playbook, i would like to display all lines in stdout_lines
>>> in below format without miss. I am able to display only first 3 lines but
>>> host03 has more than 3 lines even it may be more lines for other hosts.
>>> Would anyone please help me?
>>>
>>> Required output fle:
>>> ==
>>> host1,nfsflr01:/app01,/app,na,/etc/fstab
>>> host2,nfsflr02:/app02,/app02,na,/etc/fstab
>>> host3,nfsflr03:/app03,/app03,na,/etc/fstab
>>> host3,nfsflr03:/app03,/app03,na,/etc/rc.willows
>>> host4,nfsflr04:/app04,na,na,na
>>>
>>>
>>> ---
>>>   - name: nullify the ansible log file
>>> local_action: copy content="" dest="{{ playbook_dir
>>> }}/logs/volmigration.log"
>>> ignore_errors: true
>>> run_once: True
>>>   - name: nullify the file content
>>> local_action: copy content="" dest="{{ playbook_dir
>>> }}/logs/volcheck.txt"
>>> ignore_errors: true
>>> run_once: True
>>>   - name: Making the txt header information to volcheck.txt file
>>> local_action:
>>>   module: lineinfile
>>>   path: "{{ playbook_dir }}/logs/volcheck.txt"
>>>   line: "host,python,old_path,mnt_path,new_path,map_path,csv_path"
>>>   state: present
>>>   create: yes
>>> run_once: True
>>>   - name: Fetch the volume mounted status
>>> shell: |-
>>>   {% if ansible_os_family == "Solaris" %}
>>>   grep -ws "{{ item.0 }}" /etc/vfstab|awk '{print $1}'
>>>   grep -ws "{{ item.0 }}" /etc/vfstab|awk '{print $3}'
>>>   grep -wls "{{ item.0 }}" /etc/vfstab
>>>   grep -wls "{{ item.0 }}" /etc/auto*|head -1|xargs grep -w "{{
>>> item.0 }}"|awk '{print $2}'
>>>   grep -wls "{{ item.0 }}" /etc/auto*|head -1|xargs grep -w "{{
>>> item.0 }}"|awk '{print $1}'
>>>   grep -wls "{{ item.0 }}" /etc/auto*|head -1
>>>   {%- else %}
>>>   grep -rws "{{ item.0 }}" /etc/fstab|awk '{print $1}'
>>>   grep -rws "{{ item.0 }}" /etc/fstab|awk '{print $2}'
>>>   grep -wls "{{ item.0 }}" /etc/fstab
>>>   grep -rls "{{ item.0 }}" /etc/auto*|head -1|xargs grep -w "{{
>>> item.0 }}"|awk '{print $3}'
>>>   grep -rls "{{ item.0 }}" /etc/auto*|head -1|xargs grep -w "{{
>>> item.0 }}"|awk '{print $1}'
>>>   grep -rwls "{{ item.0 }}" /etc/auto*|head -1
>>>   grep -rwls "{{ item.0 }}" /etc/rc*|head -1|xargs grep -w "{{
>>> item.0 }}"|awk '{print $6}'
>>>   grep -rwls "{{ item.0 }}" /etc/rc*|head -1|xargs grep -w "{{
>>> item.0 }}"|awk '{print $7}'
>>>   grep -rwls "{{ item.0 }}" /etc/rc*|head -1
>>>   {%- endif %}
>>> with_together:
>>>   - "{{ var1 }}"
>>> register: volchk
>>> args:
>>>   warn: false
>>> ignore_errors: true
>>>   - name: Making the final volcheck.txt file
>>> local_action:
>>>   module: lineinfile
>>>   path: "{{ playbook_dir }}/logs/volcheck.txt"
>>>   line: |-
>>> {% if item.stdout|length>0 %}
>>> {{ inventory_hostname }},{{ ansible_python_interpreter }},{{
>>> item.stdout_lines[0] }},{{ item.stdout_lines[1] }},NA,{{
>>> item.stdout_lines[2] }},{{ item.item.0 }},
>>> {%- else %}
>>> {{ inventory_hostname }},{{ ansible_python_interpreter
>>> }},NA,NA,NA,NA,{{ item.item.0 }},
>>>